一、实验环境
两台CentOS-7.5虚拟机
虚机配置:1G内存、1核CPU、40G磁盘
MySQL版本:社区版MySQL-5.7
master_a:192.168.218.128
master_b:192.168.218.136
防火墙+selinux:均已关闭
双主:两台服务器互为主从
双主分两步实现:
1.配置以master_a为主,master_b为从的主从环境;
2.配置以master_b为主,master_a为从的主从环境
配置思路:
先实施Master->Slave的主主同步。主主是数据双向同步,主从是数据单向同步。
二、安装MySQL服务
说明:两台服务器同时执行以下命令,因为两台MySQL服务器要安装一模一样的环境
1)卸载系统自带的mariadb服务
[[email protected] ~]#
[[email protected] ~]# rpm -qa mariadb #先查看是否已安装mariadb服务
[[email protected] ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64 #卸载该程序
[[email protected] ~]# rpm -qa mariadb #查看卸载mariadb是否成功
[[email protected] ~]#
2)下载并安装mysql-5.7的安装源
yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm #安装mysql-5.7的安装源
[[email protected] ~]#
[[email protected] ~]# ll /etc/yum.repos.d/ | grep ‘mysql-community‘
[[email protected] ~]#
[[email protected] ~]# yum list mysql-community-server #查看该MySQL源中可以安装的MySQL版本
3)删除Linux系统原来自带的 my.cnf 文件
删除Linux系统原来自带的 my.cnf 文件,如果my.cnf 文件存在的话
[[email protected] ~]# rm -rf /etc/my.cnf #删除该文件
[[email protected] ~]#
4)正式安装MySQL-5.7
[[email protected] ~]#
[[email protected] ~]# yum -y install mysql-community-server #安装MySQL服务
[[email protected] ~]# rpm -qa mysql-community-server
5)启动MySQL-5.7
[[email protected] ~]#
[[email protected] ~]# systemctl start mysqld.service
[[email protected] ~]# systemctl enable mysqld.service
[[email protected] ~]#
三、修改两台MySQL服务器的配置文件
说明:因为两个服务器都互为对方的主从,所以配置差不多,但是server-id是不一样的
1)master_a 端配置
[[email protected] ~]#
[[email protected] ~]# vim /etc/my.cnf #编辑修改master_a 的配置文件
2)master_b 端配置
[[email protected] ~]#
[[email protected] ~]# vim /etc/my.cnf #编辑修改master_b 的配置文件
3)创建用于存放binlog文件的目录
master_a 端创建:
[[email protected] ~]#
[[email protected] ~]# mkdir /var/lib/mysql/binary_log
[[email protected] ~]#
[[email protected] ~]# chown mysql:mysql /var/lib/mysql/binary_log #授权
[[email protected] ~]#
[[email protected] ~]# ll /var/lib/mysql | grep ‘binary_log‘
master_b 端创建:
[[email protected] ~]#
[[email protected] ~]# mkdir /var/lib/mysql/binary_log
[[email protected] ~]#
[[email protected] ~]# chown mysql:mysql /var/lib/mysql/binary_log #授权
[[email protected] ~]#
[[email protected] ~]# ll /var/lib/mysql | grep ‘binary_log‘
四、重启MySQL服务
说明:修改完配置文件后,重启MySQL服务使配置生效
1)master_a 端执行
[[email protected] ~]#
[r[email protected] ~]# systemctl restart mysqld
[[email protected] ~]#
2)master_b 端执行
[[email protected] ~]#
[[email protected] ~]# systemctl restart mysqld
[[email protected] ~]#
五、查看二进制文件是否生成
1)master_a 端执行
[[email protected] ~]#
[[email protected] ~]# ls /var/lib/mysql/binary_log/
2)master_b 端执行
[[email protected] ~]#
[[email protected] ~]# ls /var/lib/mysql/binary_log/
六、获取MySQL临时登录密码
1)master_a 端获取:
[[email protected] ~]#
[[email protected] ~]# grep ‘temporary‘ /var/log/mysqld.log
2)master_b 端获取
七、使用安全配置向导设置MySQL
说明:MySQL使用前的基本设置
注:两台MySQL服务器操作几乎一模一样,但是root密码可以设置的不一样
1)master_a 端设置
[[email protected] ~]#
[[email protected] ~]# mysql_secure_installation #安全配置向导
Securing the MySQL server deployment.
Enter password for user root: #输入上面获取的临时密码 ;76/.favFurg
The existing password for the user account root has expired. Please set a new password.
New password: #设置新root密码 Ctsi.2020
Re-enter new password: #再次输入新root密码 Ctsi.2020
The ‘validate_password‘ plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100 #密码强度为100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : no #不再修改root密码了,上面已经改过了
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success. #移除匿名用户
Normally, root should only be allowed to connect from
‘localhost‘. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success. #禁止root用户远程登录mysql服务器
By default, MySQL comes with a database named ‘test‘ that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #删除测试数据库
- Dropping test database...
Success. - Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success. #刷新授权表
All done!
[[email protected] ~]#
2)master_b 端设置
master_b 端安全配置向导设置和master_a 端设置一模一样,连root用户的密码都一样,都是 Ctsi.2020
具体设置过程省略……
八、先配置以master_a 为主,master_b 为从的主从复制模式
1)设置MySQL复制账号并做授权
master_a 端操作:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
mysql>
mysql> show databases;
mysql>
mysql> grant replication slave on . to [email protected] identified by ‘Mysql.2020‘; #授权repluser账户可以通过从服务器192.168.218.136来登录主服务器并复制主服务器的所有库和所有表
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql>
mysql> flush privileges; #刷新授权表
Query OK, 0 rows affected (0.00 sec)
mysql>
2)master_a 主服务器端导出一个全备
[[email protected] ~]#
[[email protected] ~]# mysqldump -uroot -p‘Ctsi.2020‘ --all-databases --single-transaction > /tmp/mysql_full-1.sql #导出一个全备
[[email protected] ~]#
[[email protected] ~]# ll /tmp/mysql_full-1.sql
3)将mysql主服务器的第一次全备拷贝到从服务器
[[email protected] ~]#
[[email protected] ~]# scp /tmp/mysql_full-1.sql [email protected]:/tmp/
[[email protected] ~]#
[[email protected] ~]# ssh [email protected] #远程登录到从服务器端查看全备是否复制成功
[[email protected] ~]# ll /tmp/mysql_full-1.sql
[[email protected] ~]#
[[email protected] ~]# logout #退出从服务器远程登录
4)从服务器端导入数据库全备
master_b 端操作:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘ < /tmp/mysql_full-1.sql #导入全备,使得主从两端状态处于一致
此时,master端和slave端数据处于一致状态
5)查看主服务器端数据库的状态并记住
说明:这个状态非常重要,后面要用到,所以要记住
master_a 端执行:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘ #登录主服务器端数据库
mysql>
mysql> show master status; #查看主库状态
6)设置主从复制
master_b 端执行:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘ #登录从服务器端数据库
mysql>
mysql> change master to
-> master_host=‘192.168.218.128‘,
-> master_user=‘repluser‘,
-> master_password=‘Mysql.2020‘,
-> master_log_file=‘master_a-binlog.000001‘,
-> master_log_pos=1163;
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql>
mysql>
mysql>
mysql> start slave; #启动slave同步进程
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql> show slave status\G #查看slave状态
九、验证以master_a 为主,master_b为从的主从复制
方法:在master_a 端插入数据,在master_b 端验证是否同步过去
1)在master端创建数据库并插入数据
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
mysql>
mysql> create database t1;
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> use t1;
Database changed
mysql>
mysql> create table Student(
-> Sid varchar(10),
-> Sname nvarchar(10),
-> Sage datetime,
-> Ssex nvarchar(10)
-> );
Query OK, 0 rows affected (0.15 sec)
mysql>
mysql>
mysql> insert into Student values(‘01‘,‘张磊‘,‘1995-02-08‘,‘男‘);
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> insert into Student values(‘02‘,‘李华‘,‘1995-06-11‘,‘男‘);
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> select * from Student;
2)去slave端查看是否存在master中插入的数据
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘ #登录master_b 端数据库
mysql>
mysql> show databases;
mysql>
mysql> select * from t1.Student;
结论:对比 master_a 和 master_b 发现,两端数据一致,主从同步成功
十、再配置以master_b 为主,master_a 为从的主从复制模式
理解:在原来的CentOS主节点上创建新的用户(※这点很重要,因为之前已经是主从配置,所以在master_a节点上创建会自动同步至master_b上,如果在master_b上创建用户,则可能会导致两节点数据不一致情况发生)
在master_a端进行操作:
1)设置MySQL复制账号并做授权
在master_a端执行:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
mysql>
mysql> grant replication slave on . to [email protected] identified by ‘Mysql.2020‘;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
2)查看主服务器端数据库的状态并记住
说明:这个状态非常重要,后面要用到,所以要记住
在master_b 端执行:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
3)设置主从复制
在master_a端执行:
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
mysql>
mysql> change master to
-> master_host=‘192.168.218.136‘,
-> master_user=‘repluser‘,
-> master_password=‘Mysql.2020‘,
-> master_log_file=‘master_b-binlog.000001‘,
-> master_log_pos=831197;
Query OK, 0 rows affected, 2 warnings (0.13 sec)
mysql>
mysql>
mysql> start slave; #启动slave同步进程
Query OK, 0 rows affected (0.00 sec)
mysql>
mysql>
mysql> show slave status\G #查看slave状态
十一、验证以master_b 为主,master_a 为从的主从复制
方法:在master_b 端插入数据,在master_a 端验证是否同步过去
1)在master端创建数据库并插入数据
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
mysql>
mysql> create database t2; #创建数据库t2
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> use t2;
Database changed
mysql>
mysql> create table Student(
-> Sid varchar(10),
-> Sname nvarchar(10),
-> Sage datetime,
-> Ssex nvarchar(10)
-> );
Query OK, 0 rows affected (0.11 sec)
mysql>
mysql>
mysql> insert into Student values(‘03‘,‘王青‘,‘1996-01-06‘,‘女‘); #插入数据
Query OK, 1 row affected (0.01 sec)
mysql>
mysql> insert into Student values(‘04‘,‘三毛‘,‘1980-09-25‘,‘女‘);
Query OK, 1 row affected (0.10 sec)
mysql>
mysql> select * from Student;
2)去slave端查看是否存在master中插入的数据
[[email protected] ~]#
[[email protected] ~]# mysql -uroot -p‘Ctsi.2020‘
mysql>
mysql> show databases;
mysql>
mysql> select * from t2.Student;
结论:对比 master_a 和 master_b 发现,两端数据一致,主从同步成功
十二、本实验结论
经过两轮验证得出:MySQL双主复制配置成功
原文地址:https://blog.51cto.com/14783377/2485673