centos主从复制配置
Centos下MySQL主从同步配置
说明:由于MySQL不同版本之间的(二进制日志)binlog格式可能会不一样,
因此最好的搭配组合是Master的MySQL版本和Slave的版本相同或者更低,
Master的版本肯定不能高于Slave版本。(版本向下兼容)
一、环境
主机: master操作系统:centos 7 64位
IP:10.103.73.85
MySQL版本:5.5.50-MariaDB
从机: slave操作系统:centos 7 64位
IP:10.103.73.127
MySQL版本:5.5.50-MariaDB
二、创建数据库
分别登录master机和slave机的mysql:mysql –u root –p
创建数据库:create database repl;
三、master机和slave机的相关配置
1、修改master机器中mysql配置文件my.cnf,该文件在/etc目录下
[root@localhost ~]# vi /etc/my.cnf
在[mysqld]配置段添加如下字段
server-id=1
log-bin=mysql-bin
log-slave-updates=1
binlog-do-db=repl #需要同步的数据库,如果没有本行表示同步所有的数据库
binlog-ignore-db=mysql #被忽略的数据
在master机上为slave机添加一同步帐号
MariaDB[(none)]>grant replication slave on . to ‘repl’@’10.103.73.127 identified by ‘123456’;
MariaDB[(none)]>flush privileges;
重启master机的mysql服务:service mysqld restart
用show master status 命令看日志情况
MariaDB[(none)]>show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 106 | repl | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
mysql>
2、修改slave机中mysql配置文件
同样在[mysqld]字段下添加如下内容
server-id=2
log-bin= mysql-bin
relay-log= mysql-relay-bin
read-only=1
log-slave-updates=1
replicate-do-db=repl #要同步的数据库,不写本行表示同步所有数据库
然后重启slave机的mysql:service mysqld restart
service mysqld restart
(2)在slave机上验证对主机连接
mysql -h10.103.73.85 -urepl -p123456
创建数据库
create database repl;
(3)设置Slave复制
[root@localhost /]# mysql -uroot
mysql>use mysql
mysql>stop slave;
mysql>CHANGE MASTER TO
MASTER_HOST='10.103.73.85',
MASTER_USER='repl',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=106,
MASTER_CONNECT_RETRY=10;
mysql>start slave;
mysql>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.103.73.85
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 106
Relay_Log_File: mysql-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: repl
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: 106
Relay_Log_Space: 406
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:
1 row in set (0.08 sec)
注意查看Slave_IO_Running: Yes Slave_SQL_Running: Yes 这两项必须为Yes 以及Log_File、Log_Pos要于master状态下的File,Position相同
五、测试主从服务器是否能同步
在主服务器上面新建一个表,必须在repl数据下
mysql> use repl
Database changed
mysql> create table test(id int,name char(10));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into test values(1,'zaq');
Query OK, 1 row affected (0.00 sec)
mysql> insert into test values(1,'xsw');
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+------+------+
| id | name |
+-------+------+
| 1 | zaq |
| 1 | xsw |
+-------+------+
2 rows in set (0.00 sec)
在从服务器查看是否同步过来
mysql> use repl;
Database changed
mysql> select * from test;
+------+------+
| id | name |
+------+------+
| 1 | zaq |
| 1 | xsw |
+------+------+
2 rows in set (0.00 sec)
说明已经配置成功。