主从就是主从复制,简单来说,是使用两个或两个以上相同的数据库,将一个数据库当做主数据库,而另一个数据库当做从数据库。在主数据库中进行相应操作时,从数据库记录下所有主数据库的操作,使其二者一模一样。
主从复制步骤:
主从复制配置步骤:
需求:
搭建两台MySQL
服务器,一台作为主服务器,一台作为从服务器,主服务器进行写操作,从服务器进行读操作
分别在主从两台服务器上安装mysql-5.7
版本,此处略过安装步骤,若有疑问请参考《mysql基础》与《mysql进阶》两篇文章。
环境说明:
数据库角色 | IP | 应用与系统版本 | 有无数据 |
---|---|---|---|
主数据库master | 192.168.153.132 | mariadb/redhat8 | 无数据 |
从数据库slave | 192.168.153.133 | mariadb/redhat8 | 无数据 |
//master主机
[root@master ~]# yum -y install mariadb*
[root@master ~]# systemctl start mariadb
[root@master ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@master ~]# systemctl stop firewalld
[root@master ~]# systemctl disable firewalld
[root@master ~]# setenforce 0
//slave主机
[root@slave ~]# yum -y install mariadb*
[root@slave ~]# systemctl start mariadb
[root@slave ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 80 0.0.0.0:3306 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 128 [::]:22 [::]:*
[root@slave ~]# systemctl stop firewalld
[root@slave ~]# systemctl disable firewalld
[root@slave ~]# setenforce 0
//master主机
[root@master ~]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.17-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 ‘lc‘@‘192.168.153.133‘ identified by ‘123456‘;
Query OK, 0 rows affected (0.000 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.001 sec)
MariaDB [(none)]> quit
Bye
//slave主机
[root@slave ~]# mysql -ulc -p123456 -h192.168.153.132
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.17-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)]> quit
Bye
//master主机
[root@master ~]# vim /etc/my.cnf
#
# This group is read both both by the client and the server
# use it for options that affect everything
#
//添加下面三行配置,=号左边格式要一样,右边格式也要一样
[mysqld]
log-bin=mysql_bin
server-id=10
//slave主机
原文:https://www.cnblogs.com/leixixi/p/14226916.html