环境:CentOS 6.7 + MySQL 5.6.30
主节点:192.168.56.102
从节点:192.168.56.103
已经分别安装好单机MySQL,现在配置两台MySQL的主从复制。
1.主节点授权同步用户
官方文档是分两步进行的:
mysql> CREATE USER ‘repl’@’%.mydomain.com’ IDENTIFIED BY ‘slavepass’;
mysql> GRANT REPLICATION SLAVE ON . TO ‘repl’@’%.mydomain.com’;
实际上一条命令即可:
grant replication slave,reload,super on *.* to 'repl'@'192.168.56.103' identified by 'asdfjkl';
2.主节点修改配置文件
vi /data/mysqldata/3306/my.cnf
添加内容,其实主要是server-id=102,另外对于InnoDB,官档建议加两个参数:
[mysqld]
server-id=102
#binlog-ignore-db = mysql
#replicate-do-db = xxdb
#InnoDB
innodb_flush_log_at_trx_commit=1
sync_binlog=1
主节点重启MySQL:
shell> mysqladmin shutdown
shell> mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
3.从节点修改配置文件
vi /data/mysqldata/3306/my.cnf
添加:
[mysqld]
server-id=103
#replicate-do-db = xxdb
从节点重启MySQL:
shell> mysqladmin shutdown
shell> mysqld_safe --defaults-file=/data/mysqldata/3306/my.cnf &
4.主节点锁定后查看状态
主节点上锁:
mysql> FLUSH TABLES WITH READ LOCK;
主节点查看状态:
(root@localhost)[(none)]> show master status
-> ;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000011 | 432 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
5.从节点配置并启动同步、查看状态
从节点配置:
mysql> CHANGE MASTER TO
MASTER_HOST='192.168.56.102',
MASTER_USER='repl',
MASTER_PASSWORD='asdfjkl',
MASTER_LOG_FILE='mysql-bin.000011',
MASTER_LOG_POS=432;
这里的日志文件和当前位置是根据上一步查到的主节点状态填写的。
启动SLAVE:
mysql> START SLAVE;
查看从节点状态:
mysql> show slave status \G
6.主节点解锁并验证从节点是否正常同步
主节点解锁:
mysql> UNLOCK TABLES;
主节点建库:
mysql> create database xxdb;
从节点查看是否同步:
mysql> show databases;