今日学习总结之–mysql主从同步配置
操作系统:centos 6.7 i386
Mysql版本:5.5.52
首先需要对实验的场景的一个设定:
-Master Host :IP 172.30.82.71
-Slave Host:IP 172.30.82.74
一、首先配置Master Host
1.首先配置mysql数据库的配置文件,位于/etc/my.cnf
.
vim /etc/my.cnf
然后在 [mysql] 段的下面添加一下内容,其中binlog-do-db选项可以设置为你自己喜欢的名字
重启数据库
service mysqld restart
[server-id] : 指定了数据库中主从的id号,此数字为1-(2^32 - 1)之间的一个数值,且不允许重复。
[mysql-bin]:实现主从数据库数据一致的二进制日志文件。
2.对数据库进行操作,注意登陆的用户,这里要以root用户登陆mysql数据库,为了获得足够的操作权限
mysql -u root -p
接下来执行的操作
mysql> GRANT REPLICATION SLAVE ON \*.\* TO 'slave_user'@'%' IDENTIFIED BY \'your\_password\';
mysql> FLUSH PRIVILEGES;
mysql> FLUSH TABLE WITH READ LOCK;
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB|
+------------------+----------+--------------+------------------+
| mysql-bin.000002 | 643 | tecmint | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql> quit;
其中的mysql-bin.000002
和Postion
选项的内容咋后边的slave Host的配置中需要用到,到时候你可到Master 主机上执行show status master
再次查看此两个选项的内容,现在你应该备份一下Master Host上数据库的内容,输入下面的命令:
mysqldump -uroot -p -all-databases --master-data --events > /root/dbdump.db
当你对你的数据库中的所有的表的信息备份完毕之后,那么你现在需要执行的操作就是,连接上你的数据库然后,解锁所有的表:
mysql> UNLOCK TABLES;
mysql> quit;
接下来将你备份好的数据库内容的文件通过scp
命令复制到你的Slave Host的上面,执行下面的命令:
scp /root/dbdump.db root@172.30.82.74:/root/
到这里Master Host就已经配置完毕了。
二、Slave Host的配置
步骤和配置Master Host的步骤有点类似:
首先编辑Mysql的配置文件:
vim /etc/my.cnf
同样的在[mysql] 的下方加入如下的内容:
加注释的几行是为了Slave Host在连接Master Host的时候提供必要的连接信息,但是加在这里重新启动Mysql服务的时候会报错,无法识别这几个变量,原文中是这样写的,但我发现注释掉之后并不会有什么影响
现在把你的dbdump.db,Master Host Mysql 数据库的数据备份文件添加至Slave Host的数据库中,已达到数据库内容的一致性,然后重启数据库,执行如下命令:
mysql -uroot -p < /root/dbdump.db
service mysqld restart
现在连接数据库,并进行如下配置:
mysql> slave stop;
mysql> CHANGE MASTER TO MASTER_HOST='172.30.82.71', MASTER_USER='slave_user', MASTER_PASSWORD='your_password', MASTER_LOG_FILE='mysql-bin.000002', MASTER_LOG_POS=643;
mysql> slave start;
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.30.82.71 --> IP
Master_User: toom --> slave host connection count on master
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002 --> binary_log file
Read_Master_Log_Pos: 643 --> work port
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 789
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: tecmint
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: 643
Relay_Log_Space: 945
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
1 row in set (0.00 sec)
ERROR:
No query specified
现在可以对实验结果进行验证了:
首先在Master Host上执行如下操作:
mysql> create database tecmint;
mysql> use tecmint;
mysql> CREATE TABLE employee (c int);
mysql> INSERT INTO employee (c) VALUES (1);
mysql> SELECT * FROM employee;
+------+
| c |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
然后在Slave Host观察结果:
mysql> use tecmint;
mysql> SELECT * FROM employee;
+------+
| c |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
到这里就完成了所有的工作
原文链接:How to Setup MySQL (Master-Slave) Replication in RHEL, CentOS, Fedora