环境:
centos7
MariaDB10.2
一、关闭防火墙
查看防火墙状态:systemctl status firewalld
停止防火墙:systemctl stop firewalld
设置开机不启用防火墙:systemctl disable firewalld
二、主数据库配置
MariaDB10.2安装后其配置文件为/etc/my.cnf,内容如下:
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
通过!includedir /etc/my.cnf.d这一句可以看出,其具体的配置文件在目录/etc/my.cnf.d下。该目录下的文件如下:
[root@localhost etc]# clear
[root@localhost etc]# cd /etc/my.cnf.d/
[root@localhost my.cnf.d]# ll
total 12
-rw-r--r--. 1 root root 763 Jul 27 05:26 enable_encryption.preset
-rw-r--r--. 1 root root 232 Jul 27 05:26 mysql-clients.cnf
-rw-r--r--. 1 root root 1080 Jul 27 05:26 server.cnf
[root@localhost my.cnf.d]#
我们需要修改server.cnf文件。主要开启bin-log和指定server-id,添加的内容如下:
# this is only for the mysqld standalone daemon
[mysqld]
# 忽略表名大小写
lower_case_table_names=1
# 开启bin-log
log-bin=mysql-bin
# 指定serverId
server-id=1
三、主服务器上创建mysql用户,以便从服务器使用
在mysql的命令行下执行以下命令:
语法格式:
GRANT REPLICATION SLAVE ON .{所有权限} TO 'slave'@'%'{用户名为slave,%为任意地址} identified by 'slave';
[root@localhost my.cnf.d]# clear
[root@localhost my.cnf.d]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.26-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' identified by 'slave';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]>
四、重启主服务器,然后查看主服务器的状态
重启mariadb后,在mysql命令行下输入:SHOW MASTER STATUS查看主服务器的状态
[root@localhost my.cnf.d]# systemctl restart mariadb
[root@localhost my.cnf.d]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.2.26-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
MariaDB [(none)]>
五、从节点mariadb配置
同主务器配置一样,只是server-id值要与主服务器的不同。
[mysqld]
# 忽略表名大小写
lower_case_table_names=1
# 开启bin-log
log-bin=mysql-bin
# 指定serverId
server-id=2
六、从节点slave配置
在从节点的mysql命令行执行命令:
CHANGE MASTER TO MASTER_HOST='192.168.100.70', MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=328;
如下:
[root@localhost my.cnf.d]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.2.26-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.100.70', MASTER_PORT=3306,MASTER_USER='slave',MASTER_PASSWORD='slave',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=328;
Query OK, 0 rows affected (0.04 sec)
MariaDB [(none)]>
语法:
CHANGE MASTER TO MASTER_HOST='主节点的IP地址', MASTER_PORT=主节端口号 MASTER_USER='主节点授权的用户', MASTER_PASSWORD='主节点授权的用户的密码',MASTER_LOG_FILE='mysql-bin.000007',MASTER_LOG_POS=2197;
PS:注意语法逗号前后不要用空格
MASTER_LOG_FILE及MASTER_LOG_POS为主节点的bin-log文件名及当前同步的位置,在主节点可通过SHOW MASTER STATUS查看。
七、查看从节点状态
重启从节点的mariadb,然后在mysql命令行下输入:show slave status\G;来查看从节点的状态。
[root@localhost my.cnf.d]# systemctl restart mariadb
[root@localhost my.cnf.d]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.2.26-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.100.70
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 328
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-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: 328
Relay_Log_Space: 868
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: 1
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
1 row in set (0.00 sec)
MariaDB [(none)]>
只要Slave_IO_Running: Yes与 Slave_SQL_Running: Yes这两项为yes就表示成功。
八、测试
在主数据库下建库、表、添加数据都会自动同步到从库
九、注意事项:
- 不可以直接从库写入数据,否则会导致主从数据不一致。
- 在配置的过程中应关闭主库的写,否则主库bin-log的id会随时变动。