509 字
3 分钟
MariaDB主从复制
2022-03-11
统计加载中...
NOTE

使用OpenStack私有云平台,创建两台云主机vm1和vm2,在这两台云主机上分别安装数据库服务,并配置成主从数据库,vm1节点为主库,vm2节点为从库

安装Mariadb#

【master/slave】#

yum -y install mariadb mariadb-server
#启动Mariadb
systemctl start mariadb

【master】#

[root@master ~]# vim /etc/my.cnf.d/server.cnf

在[mysqld]下添加

[mysqld]
server-id=1
log-bin=master-bin
重启
[root@master ~]# systemctl restart mariadb
登录数据库
[root@slave ~]# mysql -uroot
验证配置是否生效
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 328 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.000 sec)
创建用户并授权
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'xiaye'@'%' IDENTIFIED BY '000000';
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.000 sec)

【slave】#

[root@slave ~]# vim /etc/my.cnf.d/server.cnf

在[mysqld]下添加

[mysqld]
server-id=2
log-bin=mysql-bin

重启#

[root@slave ~]# systemctl restart mariadb
登录数据库
[root@slave ~]# mysql -u root
连接主库
MariaDB [(none)]> change master to master_host='192.168.22.64',master_user='xiaye',master_password='000000';
Query OK, 0 rows affected (0.195 sec)
重启
[root@slave ~]# systemctl restart mariadb

查看#

TIP

注:两个yes即为成功

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

[root@slave ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.3.23-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.22.64
Master_User: xiaye
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 635
Relay_Log_File: slave-relay-bin.000003
Relay_Log_Pos: 934
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: 635
Relay_Log_Space: 1243
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
Slave_DDL_Groups: 2
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 0
1 row in set (0.000 sec)

验证#

【master】

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> create database test1;
Query OK, 1 row affected (0.000 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
5 rows in set (0.000 sec)

【slave】

MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test1 |
+--------------------+
5 rows in set (0.001 sec)
MariaDB主从复制
https://www.cuteleaf.cn/posts/system-ops/mariadb-master-slave-replication/
作者
夏叶
发布于
2022-03-11
许可协议
CC BY-NC-SA 4.0

部分信息可能已经过时