共计 4110 个字符,预计需要花费 11 分钟才能阅读完成。
操作系统:CentOS Linux release 7.3.1611 (Core)
数据库:MariaDB-10.2.6-linux-glibc_214-x86_64
主服务器:10.10.10.56
从服务器:10.10.10.163
1. 今天要做的事,单向主从同步
关于 MariaDB(MySQL) 数据库安装请参阅《Centos7.3 x64 位二进制安装(MySQL)MariaDB 10.1.20 数据库之生产篇》下面就开始 MariaDB(MySQL) 服务器集群的配置过程。
2. 在 MariaDB(MySQL) 配置文件中修改或添加以下信息
vim /etc/my.cnf | |
主从通用配置 | |
binlog-format = mixed | |
master-verify-checksum = 1 | |
slave-sql-verify-checksum = 1 |
3. 主服务器 Master 除了通用配置外, 还需要加入以下代码
server-id = 56 #MySQL 服务器 ID, 不重复 | |
log-bin = mysql-bin #二进制日志(默认开启)sync-binlog = 1 #主服务器进行设置, 用于事务安全 | |
log-bin-index = mysql-bin |
4. 从服务器 Slave 除了通用配置外, 还需要加入以下代码
server-id = 163 | |
relay-log = relay-bin #中继日志 | |
slave-parallel-threads = 2 #设定从服务器的 SQL 线程数 | |
#replicate-do-db = renwoleblogdb# 复制指定的数据库, 多个写多行 | |
replicate-ignore-db = mysql #不备份的数据库, 多个写多行 | |
relay_log_recovery = 1 #从站崩溃后可以使用, 防止损坏的中继日志处理。log-slave-updates = 1 #slave 将复制事件写进自己的二进制日志 | |
relay-log-index = relay-bin |
此外 Mysql 从服务器没有必要开启二进制日志,但是在一些情况下, 必须设置, 例如; 如果 slave 为其它 slave 的 master,必须设置 bin_log。我这里就默认开启。
5. 以上只是简单的介绍了每个参数的作用, 这些参数的具体设置还需根据用户的实际情况进行相关调整, 具体可到官方了解
《复制和二进制日志服务器系统变量》https://mariadb.com/kb/en/mariadb/replication-and-binary-log-server-system-variables/ | |
关于系统变量的兼容性,可参阅官方《MariaDB 与 MySQL 兼容性》https://mariadb.com/kb/en/mariadb/mariadb-vs-mysql-compatibility/ |
6. 主服务器 Master 授权配置
主 MariaDB 服务器上创建专用账号并授权数据库权限, 以及从服务器 IP 的远程访问
# mysql -uroot -p | |
Enter password:【输入你的 MySQL 密码回车】MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'renwoleuseracc'@'%' IDENTIFIED BY 'renwoleuserpass'; // 创建 Slave 专用备份账号 | |
MariaDB [(none)]> flush privileges; // 刷新 MySQL 权限 | |
MariaDB [(none)]> SELECT DISTINCT CONCAT('User:''',user,'''@''',host,''';') AS query FROM mysql.user; // 查看授权情况 | |
MariaDB [(none)]> flush tables with read lock; // 锁定数据库防止 master 值变化 | |
MariaDB [(none)]> show master status; // 获取 master 状态值 | |
+-----------------+----------+------------+-----------------+ | |
| File |Position |Binlog_Do_DB|Binlog_Ignore_DB | | |
+-----------------+----------+------------+-----------------+ | |
| mysql-bin.000006| 627 | | | | |
+-----------------+----------+------------+-----------------+ | |
1 row in set (0.00 sec) |
7. 一旦获取了备份时正确的 Binlog 位点(文件名和偏移量),那么就可以用 BINLOG_GTID_POS() 函数来计算 GTID
MariaDB [(none)]> SELECT BINLOG_GTID_POS("mysql-bin.000006", 627); | |
+------------------------------------------+ | |
| BINLOG_GTID_POS('mysql-bin.000006', 627) | | |
+------------------------------------------+ | |
| 0-56-4 | | |
+------------------------------------------+ | |
1 row in set (0.01 sec) |
8. 从服务器 Slave 配置
正如官方所说从 MariaDB 10.0.13 版本开始,mysqldump 会自动完成这个工作,并且把 GTID 的写在导出文件中,只要设置 –master-data 或 –dump-slave 的同时设置 –gtid 即可。
这样的话新的 SLAVE 就可以通过设置 @@gtid_slave_pos 的值来设定复制的起始位置,用 CHANGE MASTER 把这个值传给主库,然后开始复制:
# mysql -uroot -p | |
Enter password:【输入你的 MySQL 密码】MariaDB [(none)]> SET GLOBAL gtid_slave_pos = "0-56-4"; | |
MariaDB [(none)]> change master to master_host='10.10.10.56',MASTER_PORT = 3306,master_user='renwoleuseracc',master_password='renwoleuserpass',master_use_gtid=slave_pos; // 进行主从授权 | |
MariaDB [(none)]> START SLAVE; // 启动 Slave | |
MariaDB [(none)]> show slave status\G | |
*************************** 1. row *************************** | |
Slave_IO_State: Waiting for master to send event | |
Master_Host: 10.10.10.56 | |
Master_User: renwoleuseracc | |
Master_Port: 3306 | |
Connect_Retry: 60 | |
Master_Log_File: mysql-bin.000006 | |
Read_Master_Log_Pos: 627 | |
Relay_Log_File: relay.000035 | |
Relay_Log_Pos: 537 | |
Relay_Master_Log_File: mysql-bin.000006 | |
Slave_IO_Running: Yes | |
Slave_SQL_Running: Yes | |
... | |
... | |
... | |
Using_Gtid: Slave_pos | |
Gtid_IO_Pos: 0-56-4 |
9. 如果 Slave_IO_Running 与 Slave_SQL_Running 都为 YES, 表明从服务已经运行,Using_Gtid 列判断 GTID 值是否一致。
说明:
master_host 表示 master 授权地址 | |
MASTER_PORT MySQL 端口 | |
master_user 表示 master 授权账号 | |
master_password 表示密码 | |
master_use_gtid GTID 变量值 |
10. 接下来解锁主服务器数据库表
MariaDB [(none)]> unlock tables; // 解锁数据表 | |
MariaDB [(none)]> show slave hosts; // 查看从服务器连接状态 | |
MariaDB [(none)]> show global status like "rpl%"; // 查看客户端 |
11. 从服务器 Slave 查看 relay 的所有相关参数
MariaDB [(none)]> show variables like '%relay%';
12. 主从已经配置完成。现在无论在主服务器上增、改、删、查,都会同步到从服务器,根据自己的需求进行相关测试即可。
关于 master slave 重置语法
重置 master 的核心语法
RESET MASTER; 意思是执行 RESET MASTER 将删除所有二进制日志文件,并创建一个空白的二进制日志文件,数字后缀为.000001,RESET MASTER 不会影响 SLAVE 服务器上的工作状态,所以执行这个命令会导致 Slave 找不到 Master 的 binlog,从而造成同步失败。
重置 slave 的核心语法
RESET SLAVE; 含义是;RESET SLAVE 将清除 slave 上的同步位置并删除所有旧的同步中继日志文件,但重置前必须先停止 slave 服务(STOP SLAVE)
