说明: 向msql master----->slave1主从复制 中添加slave2节点
1.测试环境
主备 master slave1 slave2
IP: 192.168.56.87 192.168.56.88 192.168.56.89
Mysql-Version : 5.6.23
OS: Red Hat Enterprise Linux Server Release 5.5 (cat /etc/redhat-release)
2. 安装slave2节点的mysql数据库(步骤略)
3.锁定主库的表,备份主库
mysql> flush tables with read lock;
锁定主库表的目的是防止数据备份阶段有数据写入,数据恢复丢失数据
查看正在写哪一个二进制文件和文件位置( POS 值)(用于恢复数据用)
mysql>show master status\G
*************************** 1. row **************************
File: mysql-bin.000005
Position: 1370
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
注意: show master status\G 后面不能加; show master status\G; 前面最好也没有空格否则很容易报意外的错误。
进入到shell命令行备份数据库
/usr/local/mysql/bin/mysqldump -R -u root -p test >/tmp/test.sql
-R 备份函数,存储过程
回到锁表界面,解锁所有表
mysql> unlock tables;
4、备份文件传输到从库上
scp -P4999 ccc.sql 用户名 @172.16.22.2:/tmp/
[root@node1 tmp]# scp test.sql root@192.168.56.89:/tmp/
root@192.168.56.88's password:
test.sql 100% 2044 2.0KB/s 00:00
[root@node1 tmp]#
-P 加端口号,默认的 ssh端口号为 22 ,如果没有修改过,此参数可省略
ccc.sql 要传输的文件名
用户名@ip:/tmp/ 用户 @IP:/目录
5.从库开启ready_log
5.1 创建存目录存放ready_log
mkdir /usr/local/mysql/reay_log
chmod -R 755 /usr/local/mysql/reay_log
chown mysql:dba /usr/local/mysql/reay_log
5.2编辑slave的参数
[root@repli1 ~]# vi /etc/my.cnf
server-id = 3 #指定服务器的ID
log-bin=mysql-bin
relay_log= /usr/local/mysql/reay_log/mysql-relay-bin
log_slave_updates=1
read_only=1
重启服务
service mysql restart
server_id 服务器的标识符,唯一值,不能跟从服务器重复
log-bin=mysql-bin 开启 binlog日志
relay_log 开启复制日志, relay-log 日志记录的是在复制过程中,从服务器 I/O 线程将主服务器的二进制日志读取过来记录到从服务器本地文件,然后 SQL 线程会读取 relay-log日志的内容并应用到从服务器。
6.模拟这段时间主库继续有业务
主库操作
mysql> CREATE TABLE pet1 (name VARCHAR(20), owner VARCHAR(20),
species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
mysql> INSERT INTO pet1 VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
mysql> commit;
Query OK, 0 rows affected (0.00 sec)
7. 从库导入数据
test 数据库本来就有不用再建了,如果没有就要建库。
mysql> create database test character set utf8 collate 'utf8_general_ci';
mysql> use test;
mysql> source /tmp/test.sql;
依次导入其它 DB
8、从库配置同步
mysql> change master to master_host='192.168.56.87',master_user='test',master_password='123',
master_log_file='mysql-bin.000005',master_log_pos=1370;
master_host 主机 IP
master_user 上面创建的复制用户名
master_passowrd 用户密码
master_log_file 从那个二进制文件开始复制 对应上面的file
master_log_pos 从二进制文件的哪个位置开始复制 对应上面的position
mysql> start slave
ERROR 1872 (HY000): Slave failed to initialize relay log info structure from the repository 竟然报错了
mysql> reset slave; 重置slave的参照
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.00 sec) 现在才成功
mysql> show slave status\G;
启动Slave服务器,进入mysql控制台,查看同步状态。
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.56.87
Master_User: test
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 586
Relay_Log_File: mysql-relay-bin.000009
Relay_Log_Pos: 749
Relay_Master_Log_File: mysql-bin.000005
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: 586
Relay_Log_Space: 1294
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: 1
Master_UUID: 84b3bb06-f8a9-11e4-9dad-080027ecb38e
Master_Info_File: /usr/local/mysql/data/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.00 sec)
正常状态下,看到上面的两行加粗处,即Slave_IO_Running和Slave_SQL_Running的参数都为 Yes,就说明主从同步运行正常。
9. 查看pet1 的数据有没有同步过来
mysql> select * from pet1;
+----------+-------+---------+------+------------+-------+
| name | owner| species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane| hamster| f | 1999-03-30 | NULL|
+----------+-------+---------+------+------------+-------+
pet1 也被同步过来了;
10、同步测试
10.1 在master上写入数据:
mysql> CREATE TABLE pet3 (name VARCHAR(20), owner VARCHAR(20),species VARCHAR(20), sex CHAR(1), birth DATE, death DATE);
mysql> INSERT INTO pet3 VALUES ('Puffball','Diane','hamster','f','1999-03-30',NULL);
mysql> commit;
10.2 在slave1上查看结果:
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| pet |
| pet1 |
| pet3 |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from pet3;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)
10.2 在slave2上查看结果:
mysql> use test
Database changed
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| pet |
| pet1 |
| pet3 |
+----------------+
3 rows in set (0.00 sec)
mysql> select * from pet3;
+----------+-------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f | 1999-03-30 | NULL |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)
可以看到数据同步到slave1和 slave2 同步成功