本文介绍基于安装在两台linux服务器上的MariaDB介绍,master(192.168.159.100)中已经有历史数据,启动主从复制前需要做完整备份,slave(192.168.159.101)
MariaDB和MySQL有两种主从复制方式:
这里主要介绍基于bin-log的方式,原理:
]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=maria
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=maria-bin
server-id=100
binlog_format=mixed
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=maria
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-bin=maria-bin
server-id=101
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@cdh001 ~]# systemctl restart mariadb.service
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'mysync'@'192.168.159.%' IDENTIFIED BY 'mysync';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| maria-bin.000001 | 2090 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]> change master to
-> master_host='192.168.159.100',
-> master_user='mysync',
-> master_password='mysync',
-> master_log_file='maria-bin.000001',
-> master_log_pos=2090;
Query OK, 0 rows affected (0.01 sec)
主数据库中的数据如果可有可无,那么就可以不用处理,如果是重要数据,就需要在启动主从备份前先将数据完整备份到从数据库,使主从保持数据一致。
MariaDB [(none)]> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
[root@cdh001 /]# mkdir -p /data/db_backup
[root@cdh001 /]# cd /data/db_backup
[root@cdh001 db_backup]# mysqldump -uroot -p --master-data=1 --single-transaction --routines --triggers --events --all-databases >all.sql
Enter password:
[root@cdh001 db_backup]# ls
all.sql
MariaDB [(none)]> unlock tables;
Query OK, 0 rows affected (0.00 sec)
source /data/db_backup/all.sql
报错,然后我直接把all.sql拖到本地navicat中连接的slave中的MariaDB中执行成功了。如图可以看到主从数据一致了在Slave从MariaDB上执行命令,启动同步并查看状态,以下两个参数为yes则表示主从复制构建成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
这时在master上执行的操作就会同步到slave上了
MariaDB [mysql]> start slave;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.159.100
Master_User: mysync
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: maria-bin.000001
Read_Master_Log_Pos: 92985
Relay_Log_File: mariadb-relay-bin.000002
Relay_Log_Pos: 68546
Relay_Master_Log_File: maria-bin.000001
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: 92985
Relay_Log_Space: 68842
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: 100
1 row in set (0.00 sec)
在slave的MariaDB上执行以下操作后重启MariaDB
stop slave;
reset slave;