MariaDB 10.0 基于GTID实现主从复制
2016-05-16 本文已影响1268人
53921f46e0b9
目前公司线上数据库为单点数据库,因业务需求需要部署主从服务
环境
系统版本:CentOS Linux release 7.2.1511 (Core)
内核版本:3.10.0-327.el7.x86_64
MariaDB版本:10.0.25-MariaDB
主服务器:192.168.60.103
从服务器:192.168.60.104
MariaDB安装
这里使用的安装方式如下 CentOS 7 下 MariaDB 10 安装
模拟数据
单台数据库,实时有数据写入。
MariaDB [(none)]> create database test_will;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use test_will
Database changed
MariaDB [test_will]> create table mytable (name varchar(20), sex char(1), birth date, birthaddr varchar(20));
Query OK, 0 rows affected (0.04 sec)
MariaDB [test_will]> quit
Bye
[root@master ~]# vim 1.sh
for i in {1..10000}
do
mysql test_will -e "insert into mytable values( 'abc','f','2016-11-11','chian$i');"
sleep 1
done
[root@master ~]# sh 1.sh &
[1] 8688
修改MariaDB配置
在[mysqld]
下添加内容
通配
[mysqld]
binlog-format=ROW # 二进制日志文件格式
log-slave-updates=True # slave更新是否记入日志
master-info-repository=TABLE
relay-log-info-repository=TABLE # 此两项为打开从服务器崩溃二进制日志功能,信息记录在事物表而不是保存在文件
sync-master-info=1 # 值为1确保信息不会丢失
slave-parallel-threads=2 #同时启动多少个复制线程,最多与要复制的数据库数量相等即可
binlog-checksum=CRC32 # 效验码
master-verify-checksum=1 # 启动主服务器效验
slave-sql-verify-checksum=1 # 启动从服务器效验
binlog-rows-query-log-events=1 # 用于在二进制日志详细记录事件相关的信息,可降低故障排除的复杂度;
主服务器
除通配外,还需要要以下配置
bind-address = 192.168.60.103 # 监听本机网卡ip
server_id = 103 # 一组主从组里的每个id必须是唯一值,取值为1到2的32次方-1的整数。推荐用ip位数
log-bin = marirdb-bin # 二进制日志,后面指定存放位置。如果只是指定名字,默认存放在/var/lib/mysql下
MariaDB10.0中,server_id系统变量成为动态变量
备服务器
除通配外,还需要要以下配置
server_id = 104
relay_log = relay-bin # 中继日志, 后面指定存放位置。如果只是指定名字,默认存放在/var/lib/mysql下
重启
修改完配置后,主从服务器都需要重启MariaDB
systemctl restart mysql
停机时数据写入会失败,丢失
[root@master ~]# systemctl restart mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2 "No such file or directory")
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2 "No such file or directory")
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 104 "Connection reset by peer"
验证主服务器
[root@master ~]# mysql -uroot -p
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| marirdb-bin.000002 | 1638 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
此时看到在主服务器上能看到mysql会开始记录日志事件
主服务器进行数据备份
数据库锁表
MariaDB [(none)]> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
开多一个会话窗口
[root@master ~]# mysqldump -uroot -p --all-databases > /tmp/db.sql
[root@master ~]# scp /tmp/db.sql 192.168.60.104:/tmp/db.sql
The authenticity of host '192.168.60.104 (192.168.60.104)' can't be established.
ECDSA key fingerprint is 5d:47:e7:ad:f3:5e:2b:81:ff:02:22:4d:f5:4a:f8:16.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.60.104' (ECDSA) to the list of known hosts.
root@192.168.60.104's password:
db.sql 100% 470KB 470.2KB/s 00:00
记录当前日志位置并解锁
MariaDB [(none)]> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| marirdb-bin.000002 | 4458 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> SELECT BINLOG_GTID_POS('marirdb-bin.000002', 4458);
+---------------------------------------------+
| BINLOG_GTID_POS('marirdb-bin.000002', 4458) |
+---------------------------------------------+
| 0-103-22 |
+---------------------------------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
这里通过二进制日志跟偏移位置查看此时的GTID值
主服务器数据库赋予同步账户
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave_user'@'192.168.60.104' IDENTIFIED BY 'slave';
MariaDB [(none)]> FLUSH PRIVILEGES;
在从MariaDB服务器上还原数据
[root@slave ~]# mysql -uroot -p < /tmp/db.sql
启动同步
MariaDB [(none)]> SET GLOBAL gtid_slave_pos='0-103-22';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> CHANGE MASTER TO
-> MASTER_HOST='192.168.60.103',
-> MASTER_USER='slave_user',
-> MASTER_PASSWORD='slave',
-> MASTER_PORT=3306,
-> MASTER_USE_GTID=slave_pos;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> START SLAVE;
Query OK, 0 rows affected (0.01 sec)
这里跟以前基于二进制日志进行复制的区别就是:需要设置全局gtid_slave_pos值,并且使用MASTER_USE_GTID语句
查看同步状态,无错误即正常
MariaDB [(none)]> SHOW SLAVE STATUS\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.60.103
Master_User: slave_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: marirdb-bin.000002
Read_Master_Log_Pos: 38193
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 34393
Relay_Master_Log_File: marirdb-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
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: 38193
Relay_Log_Space: 34688
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: 103
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: Slave_Pos
Gtid_IO_Pos: 0-103-201
1 row in set (0.00 sec)
关闭数据模拟
[root@master ~]# kill -9 8688
验证
[root@master ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> use test_will
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test_will]> SELECT COUNT(*) FROM mytable;
+----------+
| COUNT(*) |
+----------+
| 256 |
+----------+
1 row in set (0.00 sec)
[root@slave ~]# mysql -uroot -p
Enter password:
MariaDB [(none)]> use test_will
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [test_will]> SELECT COUNT(*) FROM mytable;
+----------+
| COUNT(*) |
+----------+
| 256 |
+----------+
1 row in set (0.00 sec)
数据量一致,主从同步成功。