mariadb主从复制实现读写分离
主从复制的前提
- 两台以上的mysql实例,server_id(>0),server_uuid
- 主库必须要开启二进制日志(binlog)
- 主库要开启一个专用的复制用户(replication slave)
- “补课”:备份主库数据,恢复到从库
- 通知从库可以进行自动复制(change master to 主库ip,port user password 起点)
- 在从库启动专用的复制线程(start slave)
例题
使用OpenStack私有云平台,创建两台云主机vm1和vm2,在这两台云主机上分别安装数据库服务,并配置成主从数据库,vm1节点为主库,vm2节点为从库(数据库密码设置为000000)
master ip 192.168.200.5
slave ip 192.168.200.19
Master&&slave:
安装
yum -y install mariadb mariadb-server
启动mariadb
systemctl start mariadb
Master:
vim /etc/my.conf
在[mysqld]下添加
[mysqld]
server-id=1
log-bin=master-bin
systemctl restart mariadb
验证配置是否生效
MariaDB [(none)]> show master status;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 314 | | |
+-------------------+----------+--------------+------------------+
创建用户并授权
MariaDB [(none)]> grant replication slave on *.* to 'user'@'%' identified by 'password';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.01 sec)
Slave:
vim /etc/my.conf
在[mysqld]下添加
[mysqld]
server-id=2
read-only=on //只读模式
relay-log=relay-bin //开启日志
重启
[root@slave ~]# systemctl restart mariadb
连接主库
MariaDB [(none)]> change master to master_host='192.168.200.5',master_user='user',master_password='password',master_log_file='master-bin.000001',master_log_pos=314;
Query OK, 0 rows affected (0.05 sec)
注:master_log_file与master_log_pos与上面master中show出来的对应
重启
[root@slave ~]# systemctl restart mariadb
查看
[root@slave ~]# mysql -uroot -p000000
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.1.20-MariaDB MariaDB Server
Copyright (c) 2000, 2016, 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.200.5
Master_User: user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-bin.000001
Read_Master_Log_Pos: 609
Relay_Log_File: relay-bin.000003
Relay_Log_Pos: 833
Relay_Master_Log_File: master-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: 609
Relay_Log_Space: 1125
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
1 row in set (0.00 sec)
注:两个yes即为成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
验证
Master
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.01 sec)
MariaDB [(none)]> create database test;
Query OK, 1 row affected (0.01 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.01 sec)
Slave:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.02 sec)
成功