这次实验使用docker 环境搭建,方便又快捷而且利用资源效率比较高,环境都是无菌的(无毒无害不可食用 (ˉ﹃ˉ))
172.17.0.2 master 主库
172.17.0.3 slave 备库
环境都是新的,暂时没有数据
-----------master 端-------------
编辑 mysql 配置文件添加下面内容,重启 mysql
log_bin = mysql-bin //2进制日志文件名字
server_id = 10 //server-id 这个id 最好有意义且是唯一的
然后在matser 端添加一个有复制权限的账户
mysql> GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO ‘slave‘@‘172.17.0.%‘ IDENTIFIED BY ‘123456‘;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
------------slave 端--------------
编辑 mysql 配置文件添加下面的内容,然后重启 mysql
log_bin = mysql-bin
server_id = 20
relay_log = /var/lib/mysql/mysql-relay-bin
log_slave-updates = 1
read_only =1
然后进入mysql 执行如下命令:
mysql> CHANGE MASTER TO
-> MASTER_HOST=‘172.17.0.2‘,
-> MASTER_USER=‘slave‘,
-> MASTER_PASSWORD=‘123456‘,
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE=‘mysql-bin.000002‘, //这个参数在master端执行show master status 获得
-> MASTER_LOG_POS=106; //这个参数在master 端执行show master status 获得
Query OK, 0 rows affected (0.04 sec)
查看slave 状态
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 172.17.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 106
Relay_Log_File: mysql-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: No
Slave_SQL_Running: No
......
现在 IO 进程和 SQL 进程都是没有运行的,接下来启动slave
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)
再查看一次状态,现在 SQL 进程和 IO 进程都已经启动了,并且有了状态信息
mysql> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: slave
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 106
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在master端创建一个表测试是否能够同步都slave端
mysql> CREATE TABLE test_table(id INT(10),name VARCHAR(20));
Query OK, 0 rows affected (0.02 sec)
mysql> INSERT INTO test_table VALUES(23333,‘hello world‘);
Query OK, 1 row affected (0.00 sec)
mysql> SELECT * FROM test_table;
+-------+-------------+
| id | name |
+-------+-------------+
| 23333 | hello world |
+-------+-------------+
1 row in set (0.00 sec
在slave 端查看
mysql> SELECT * FROM test.test_table;
+-------+-------------+
| id | name |
+-------+-------------+
| 23333 | hello world |
+-------+-------------+
1 row in set (0.00 sec)
ok 已经同步完整喽,这个只是一个简单的例子 生产环境请勿模仿
本文出自 “superTaodi” 博客,请务必保留此出处http://supertaodi.blog.51cto.com/7256234/1620149
原文地址:http://supertaodi.blog.51cto.com/7256234/1620149