实验背景
主机:server1:172.25.55.1
从机:server2:172.25.55.2
都关闭防火墙。
原理:主从复制,是通过mysql的IO线程和SQL线程来完成的。
从机上的io_thread 负责从主库拿binlog并写到relaylog, sql_thread 负责读relaylog并执行。从机与主机达到数据同步。因为有个复制过程因此会延时。
一、安装mysql
[root@server1 ~]# ls
mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@server1 ~]# tar xf mysql-5.7.24-1.el7.x86_64.rpm-bundle.tar
[root@server1 ~]# ls
[root@server1 ~]# yum install mysql-community-client-5.7.24-1.el7.x86_64.rpm mysql-community-common-5.7.24-1.el7.x86_64.rpm mysql-community-libs-5.7.24-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.24-1.el7.x86_64.rpm mysql-community-server-5.7.24-1.el7.x86_64.rpm
[root@server1 ~]# vim /etc/my.cnf
server-id=1
log-bin=mysql-bin
[root@server1 ~]# systemctl start mysqld

[root@server1 ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: (输入临时密码)
The existing password for the user account root has expired. Please set a new password.
New password: (输入新密码)
Re-enter new password:
Change the password for root ? ((Press y|Y for Yes, any other key for No)
(是否改变root用户的密码)
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.(是否去除匿名用户?)
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.(不允许root用户远程登录?)
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y(删除测试数据库并访问它)
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.(现在重新加载授权表)
server2的mysql安装步骤与server1相同。
但配置文件中的id号与主机不同。
[root@server2 ~]# vim /etc/my.cnf
server-id=2
二、设置主从复制
- 主机
登录主机mysql ,设置权限。
设置权限后查看master状态,查看到pos号1566.
(mysql每次操作都会有一个Pos号,通过pos号会查到刚才所作的操作,将命令复制过来,执行。)

- 从机:
测试主机授权是否成功:
从机可远程访问主机的数据库,则主机上的授权是成功的。

在从机上设置master时添加的pos号就是在主机上查看到的pos号。
mysql> change master to master_host='172.25.55.1',master_user='rep',master_password='Redhat_123',master_log_file='mysql-bin.000002',master_log_pos=1566;
Query OK, 0 rows affected, 2 warnings (0.19 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)
查看slave状态:
IO线程和SQL线程都是YES,则主从复制成功。

测试:
在主机上创建一个数据库,

在从机上也能查看到


本文详细介绍了在两台服务器上进行MySQL主从复制的全过程,包括安装MySQL、配置服务器ID、设置权限、启动主从复制及验证复制效果。通过具体步骤和命令展示,帮助读者理解并实现MySQL的数据同步。
3129

被折叠的 条评论
为什么被折叠?



