mysql主从复制
准备2台虚拟机。以centOS7.3为例
node1:172.16.250.24
node2:172.16.250.25
node1(master)
systemctl start mariadb
启动二进制日志
vim /etc/my.cnf
查看中继日志是否开启
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
查看中继日志
MariaDB [(none)]> SHOW MASTER LOGS;
为当前节点设置一个全局唯一的ID号
查看server ID
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%server%';
创建有复制权限的用户账号
权限:REPLICATION SLAVE
和 REPLICATION CLIENT
MariaDB [(none)]> GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'172.16.%.%' IDENTIFIED BY '222222';
node2(salver)
启动中继日志
vim /etc/my.cnf
查看中继日志是否开启
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%log%';
- 为当前节点设置一个全局唯一的ID号
查看server ID
MariaDB [(none)]> SHOW GLOBAL VARIABLES LIKE '%server%';
-
使用有复制权限的账号链接至主服务器,并启动复制线程。
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='172.16.250.24', MASTER_USER='repluser', MASTER_PASSWORD='222222', MASTER_LOG_FILE='master-bin.000001', MASTER_LOG_POS=593;
启动复制线程
MariaDB [(none)]> START SLAVE;
MariaDB [(none)]> SHOW SLAVE STATUS\G;
查看自己的 从 状态
SHOW SLAVE STATUS\G; (\G表示以竖排结构显示)
测试验证
node1 (manster)
MariaDB [mysql]> CREATE DATABASE mydb;
MariaDB [(none)]> SHOW MASTER STATUS;
node2 (slave)
MariaDB [(none)]> SHOW SLAVE STATUS\G;
MariaDB [(none)]> SHOW DATABASES;