为了实现mariadb的高可用,常用的有主从复制,和双主复制,双主复制的好处是可以充分利用硬件资源。
约定:
node1IP:192.168.1.100 MariaDB:10.3.15
node2IP:192.168.1.101 MariaDB:10.3.15基于SSL实现Mysql加密的主从复制配置
Mariadb二进制安装和配置说明
CentOS 7安装 LNMP(Linux+Nginx+MariaDB+PHP)
一、放行Mariadb3306端口,修改Mariadb配置文件,注意是在server.cnf的【server】
,MySQL和MariaDB配置文件略有不同
###放行Mariadb3306端口
[root@localhost ~]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@localhost ~]# firewall-cmd --reload
###node1
[root@localhost ~]# vi /etc/my.cnf.d/server.cnf
[server]
server-id = 1
log-bin = mysql-bin
relay-log = relay-bin
###node2
[root@localhost ~]# vi /etc/my.cnf.d/server.cnf
[server]
server-id = 2
log-bin = mysql-bin
relay-log = relay-bin
二、在node1和node2上,分别执行以下命令,创建主从复制用户slaveuser并设定密码,如果只需要主从复制不需要双主复制,只需要在Mariadb 主节点 执行即可。
MariaDB [mysql]>grant replication slave,replication client on *.* to 'slaveuser'@'%' identified by '123456';
三、查看mariadb数据库的master状态信息
###node1 需要的两个东西File:mysql-bin.000004 和Position:245807,在创建同步并开启同步会用到。
MariaDB [mysql]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 245807 | | |
+------------------+----------+--------------+------------------+
###node2
MariaDB [mysql]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 | 1421 | | |
+------------------+----------+--------------+------------------+
四、创建同步并开启同步,如果只需要主从复制不需要双主复制,只需要在Mariadb 从节点 执行即可。
###node1节点
MariaDB [mysql]>change master to master_host='192.168.1.101',master_user='slaveuser',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=1421;
MariaDB [mysql]>start slave;
###node2节点
MariaDB [mysql]>change master to master_host='192.168.1.100',master_user='slaveuser',master_password='123456',master_log_file='mysql-bin.000004',master_log_pos=245807;
MariaDB [mysql]>start slave;
五、验证同步信息: Slave_IO_Running: Yes/ Slave_SQL_Running: Yes,必须均为Yes状态才行。
验证一:
###node1
MariaDB [mysql]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.101
Master_User: slaveuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 266651
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 21399
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes 此项为yes
Slave_SQL_Running: Yes 此项为yes
###node2
MariaDB [mysql]> start slave;
Query OK, 0 rows affected (0.001 sec)
MariaDB [mysql]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.100
Master_User: slaveuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 1421
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 555
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证二、在node1创建测试数据库abc,并在node2节点查看
###node1节点:
MariaDB [mysql]> create database abc;
Query OK, 1 row affected (0.000 sec)
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| abc |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
6 rows in set (0.000 sec)
###在node2节点查看
MariaDB [mysql]> show databases;
+--------------------+
| Database |
+--------------------+
| abc |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
6 rows in set (0.001 sec)
###在node2节点创建测试数据库,并在node1节点查看,省略。
###测试通过说明MariaDB双主成功
报错处理,如果同步不成功,尝试以下操作,先停止同步,然后重置同步,并重复步骤 一至五 双主复制的操作。
MariaDB [mysql]> stop slave;
MariaDB [mysql]> reset slave;
还没有任何评论,你来说两句吧