十五周二次课mysql
17.1mysql主从介绍linux
17.2准备工做web
17.3配置主sql
17.4配置从数据库
17.5测试主从同步vim
17.1mysql主从介绍网络
原理很简单:从会把主上的binlog搞到从上来,从再根据这个binlog生成本身的中继日志,而后再根据中继日志执行相应的更改,最终达到两边的数据一致。dom
17.2准备工做socket
1.首先下载二进制免编译的包,下载到/usr/local/src/目录下ide
2.解压压缩包
3.解压完以后,把解压出来的目录放到 /usr/local/mysql/ 目录下
[root@hanfeng ~]# ls /usr/local/mysql bin data include man my-new.cnf README share support-files COPYING docs lib my.cnf mysql-test scripts sql-bench [root@hanfeng ~]#
4.而后切换到 /usr/local/mysql/ 目录下,进行初始化 命令
5.初始化成功的标志就是两个OK,或者用 echo $? 检查是否初始化成功
6.编辑 /etc/my.cnf 文件——>默认是自带 my.cnf 文件的
在 /etc/my.cnf 文件中 定义 datadir=/data/mysql 定义 socket=/tmp/mysql.sock
7.拷贝启动脚本
8.编辑启动脚本
vim /etc/init.d/mysqld //对如下两行进行指定路径 指定basedir的路径 /usr/local/mysql 指定datadir的路径 /data/mysql
9.以后就能够启动mysql了
10.若是启动失败,能够去查看错误日志
11.建议 :
12.若想开机启动,只须要输入命令
1.在两台机器安装并启动mysql服务后,首先在主上进行操做(主为192.168.23.136,从为192.168.23.130)。
2.修改/etc/my.cnf文件
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=136
log_bin=aminglinux1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
保存退出
3.更改完配置之后,须要重启mysql服务。
[root@localhost ~]# /etc/init.d/mysqld restart
Shutting down MySQL... SUCCESS!
Starting MySQL.......... SUCCESS!
[root@localhost ~]#
若是重启过程当中出现“Plugin 'FEDERATED' is disabled”错误,删除/data目录下除mysql之外的目录便可
4.查看/data/mysql目录下,会生成一些文件
[root@localhost mysql]# ls -lt /data/mysql
total 110656
-rw-rw---- 1 mysql mysql 120 Jun 17 14:23 aminglinux1.000001
-rw-rw---- 1 mysql mysql 21 Jun 17 14:23 aminglinux1.index
-rw-rw---- 1 mysql mysql 56 Jun 5 10:09 auto.cnf
drwxr-xr-x 2 root root 6 Jun 17 14:31 blog
-rw-rw---- 1 mysql mysql 12582912 Jun 17 14:23 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 17 14:23 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 5 10:08 ib_logfile1
-rw-rw---- 1 mysql mysql 36693 Jun 17 14:23 localhost.localdomain.err
-rw-rw---- 1 mysql mysql 5 Jun 17 14:23 localhost.localdomain.pid
drwx------ 2 mysql mysql 4096 Jun 5 10:08 mysql
drwx------ 2 mysql mysql 4096 Jun 17 11:08 mysql2
drwx------ 2 mysql mysql 4096 Jun 5 10:08 performance_schema
drwx------ 2 mysql mysql 6 Jun 5 10:08 test
[root@localhost mysql]#
5.测试,准备作一个数据作演示看
6.首先作一个备份
[root@localhost mysql]# mysqldump -uroot -paminglinux blog > /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
7.建立一个新的数据库
[root@localhost mysql]# mysql -uroot -paminglinux -e "create database aming"
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
8.建立好数据库之后,还须要把数据恢复一下,也就是说作的主从,参考对象是aming数据库
[root@localhost mysql]# mysql -uroot -paminglinux aming < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
9.再次查看/data/mysql目录下的文件
[root@localhost mysql]# ls -l /data/mysql
total 110656
drwx------ 2 mysql mysql 20 Jun 17 14:33 aming
-rw-rw---- 1 mysql mysql 217 Jun 17 14:33 aminglinux1.000001
-rw-rw---- 1 mysql mysql 21 Jun 17 14:23 aminglinux1.index
-rw-rw---- 1 mysql mysql 56 Jun 5 10:09 auto.cnf
drwxr-xr-x 2 root root 6 Jun 17 14:31 blog
-rw-rw---- 1 mysql mysql 12582912 Jun 17 14:23 ibdata1
-rw-rw---- 1 mysql mysql 50331648 Jun 17 14:23 ib_logfile0
-rw-rw---- 1 mysql mysql 50331648 Jun 5 10:08 ib_logfile1
-rw-rw---- 1 mysql mysql 36693 Jun 17 14:23 localhost.localdomain.err
-rw-rw---- 1 mysql mysql 5 Jun 17 14:23 localhost.localdomain.pid
drwx------ 2 mysql mysql 4096 Jun 5 10:08 mysql
drwx------ 2 mysql mysql 4096 Jun 17 11:08 mysql2
drwx------ 2 mysql mysql 4096 Jun 5 10:08 performance_schema
drwx------ 2 mysql mysql 6 Jun 5 10:08 test
[root@localhost mysql]#
10.正常状况下,能看到aminglinux1.000001二进制文件是由增长的,增长的大小是和blog库保持一致的,aminglinux1.000001完整地记录了数据库的过程。建立的库,建立的表,以及表里的内容全都有。
11.下面建立主从相互同步用的用户
12.进入mysql里面去
[root@localhost mysql]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
13.建立用户
mysql> grant replication slave on *.* to 'repl'@'192.168.23.130' identified by 'aminglinux111';
Query OK, 0 rows affected (0.00 sec)
mysql>
14.锁定表,目的是不让表继续写了,数据到此暂停,先把这个状态保持在这儿。由于一下子要作从的机器配置,须要两台机器同步,保证两台机器的数据一致,同步才不会出错
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.10 sec)
mysql>
15.查看一下binlog文件的大小,记住名字和位置
mysql> show master status;
+--------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+--------------------+----------+--------------+------------------+-------------------+
| aminglinux1.000001 | 428 | | | |
+--------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
16.而后退出数据库,作一个数据同步
17.查看/data/mysql下都有哪些库,主上有哪些库,从上就要同步哪些库,意味着数据都有备份过去
[root@localhost mysql]# ls
aming aminglinux1.index blog ib_logfile0 localhost.localdomain.err mysql performance_schema
aminglinux1.000001 auto.cnf ibdata1 ib_logfile1 localhost.localdomain.pid mysql2 test blog
[root@localhost mysql]#
18.备份数据库,除了mysql库,由于mysql库里面有帐号密码,从上的时候不可能把全部权限复制过去,因此mysql不须要备份
[root@localhost mysql]# mysqldump -uroot -paminglinux mysql2 > /tmp/my2.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]# mysqldump -uroot -paminglinux zrlog > /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@localhost mysql]#
19.一下子把/tmp/下.sql文件都拷贝到从上去
[root@localhost mysql]# ls /tmp/*.sql
/tmp/blog.sql /tmp/my2.sql /tmp/mysql2.sql /tmp/mysql_all.sql /tmp/mysqlbak.sql /tmp/user.sql /tmp/zrlog.sql
20.主上操做完成,接下来从上操做
1.首先在从上安装并启动mysql,而后查看my.cnf,配置server-id=130,要求和主不同,在配置文件的log_bin参数就不须要配置的,由于只有主上才须要二进制日志文件
[root@tianqi-01 ~]# vim /etc/my.cnf
[mysqld]
character_set_server = utf8
datadir=/data/mysql
socket=/tmp/mysql.sock
server-id=130
log_bin=aminglinux1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
#!includedir /etc/my.cnf.d
2.重启mysql服务
[root@tianqi-01 ~]# /etc/init.d/mysqld restart
Shutting down MySQL....... [ 肯定 ]
Starting MySQL...................... [ 肯定 ]
[root@tianqi-01 ~]#
3.增长server-id后,对mysql是没有变化的
[root@tianqi-01 ~]# ls /data/mysql
aming aminglinux1.000007 aminglinux1.000014 aminglinux1.000021 aminglinux1.000028 ib_logfile0 tianqi-01.pid
aminglinux1.000001 aminglinux1.000008 aminglinux1.000015 aminglinux1.000022 aminglinux1.000029 ib_logfile1 zabbix
aminglinux1.000002 aminglinux1.000009 aminglinux1.000016 aminglinux1.000023 aminglinux1.000030 mysql zrlog
aminglinux1.000003 aminglinux1.000010 aminglinux1.000017 aminglinux1.000024 aminglinux1.index mysql2
aminglinux1.000004 aminglinux1.000011 aminglinux1.000018 aminglinux1.000025 auto.cnf performance_schema
aminglinux1.000005 aminglinux1.000012 aminglinux1.000019 aminglinux1.000026 db1 test
aminglinux1.000006 aminglinux1.000013 aminglinux1.000020 aminglinux1.000027 ibdata1 tianqi-01.err
[root@tianqi-01 ~]#
4.把主机器上备份的 .sql 数据,拷贝到从机器上,而后作一个数据恢复
[root@tianqi-01 ~]# scp 192.168.23.136:/tmp/*.sql /tmp/
The authenticity of host '192.168.23.136 (192.168.23.136)' can't be established.
ECDSA key fingerprint is SHA256:A62fDLxjGpEeD/g4UPe/2LtbDTOkiS9zpWjO7w9tZGc.
ECDSA key fingerprint is MD5:04:16:78:f6:50:ad:0a:cf:60:57:b2:b0:cb:2e:f8:4c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.23.136' (ECDSA) to the list of known hosts.
root@192.168.23.136's password:
blog.sql 100% 1258 1.1MB/s 00:00
my2.sql 100% 638KB 3.7MB/s 00:00
mysql2.sql 100% 30KB 1.8MB/s 00:00
mysql_all.sql 100% 1276KB 2.7MB/s 00:00
mysqlbak.sql 100% 638KB 9.0MB/s 00:00
user.sql 100% 7026 5.8MB/s 00:00
zrlog.sql 100% 1259 950.7KB/s 00:00
[root@tianqi-01 ~]#
5.建立数据库
mysql> create database aming;
Query OK, 1 row affected (0.00 sec)
mysql> create database zrlog;
Query OK, 1 row affected (0.00 sec)
mysql> create database blog;
Query OK, 1 row affected (0.00 sec)
mysql> create database mysql2;
Query OK, 1 row affected (0.00 sec)
mysql>
6.而后将数据作一个恢复
[root@tianqi-01 ~]# mysql -uroot -paminglinux blog < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]# mysql -uroot -paminglinux zrlog < /tmp/zrlog.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]# mysql -uroot -paminglinux mysql2 < /tmp/mysql2.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]# mysql -uroot -paminglinux aming < /tmp/blog.sql
Warning: Using a password on the command line interface can be insecure.
[root@tianqi-01 ~]#
7.而后查看/data/mysql/目录下的数据是否和主机器上的/data/mysql/目录是否一致
8.开始实现主从
9.在从机器登陆到mysql
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 31
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
10.而后在数据库里面执行命令,中止slave
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
11.进行主机器相关配置
mysql> change master to master_host='192.168.23.136', master_user='repl', master_password='aminglinux111', master_log_file='aminglinux1.000001', master_log_pos=428;
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql>
12.开始slave
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
mysql>
13.这时候经过 show slave status\G 判断主从是否配置成功
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.23.136
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: aminglinux1.000001
Read_Master_Log_Pos: 428
Relay_Log_File: tianqi-01-relay-bin.000002
Relay_Log_Pos: 285
Relay_Master_Log_File: aminglinux1.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 428
Relay_Log_Space: 462
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 136
Master_UUID: 8c4ee77b-6865-11e8-8647-000c2970f861
Master_Info_File: /data/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
1 row in set (0.01 sec)
mysql>
注意:这里可能会出现Slave_IO_Running: Connecting错误,主要缘由有三个:一、网络不通二、密码不对三、pos不对,仔细检查发现这3项没有错误以后,检查一下主的防火墙是或关闭
14.解锁主上的表
mysql> unlock tables;
Query OK, 0 rows affected (0.09 sec)
15.到这里主从搭建就算完成了
1.主进入MySQL,使用aming数据库
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> use aming;
Database changed
2.建立数据表t1
mysql> create table t1(`id` int(4),`name` char(20));
Query OK, 0 rows affected (0.66 sec)
3.查看数据表t1
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.10 sec)
mysql>
4.从上 进入MySQL,使用aming数据库
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@tianqi-01 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2092
Server version: 5.6.35-log MySQL Community Server (GPL)
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| aming |
| blog |
| mysql |
| mysql2 |
| performance_schema |
| test |
| zrlog |
+--------------------+
8 rows in set (0.05 sec)
mysql> use aming;
Database changed
5.查看全部数据表
mysql> show tables;
+-----------------+
| Tables_in_aming |
+-----------------+
| t1 |
+-----------------+
1 row in set (0.00 sec)
6.查看数据表t1的建表语句,结果与主的一致,说明主从一直
mysql> show create table t1;
+-------+---------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+---------------------------------------------------------------------------------------------------------------------+
| t1 | CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+---------------------------------------------------------------------------------------------------------------------+
1 row in set (0.20 sec)
mysql> show create table t1\G;
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int(4) DEFAULT NULL,
`name` char(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>