虚拟机环境如下:
| Node1 | 192.168.1.110 | Centos8 |
| Node2 | 192.168.1.111 | Centos8 |
yum install -y mysql*
- systemctl stop firewalld
- systemctl disable firewalld
- systemctl start mysqld
- systemctl enable mysqld
- mysql_secure_installation \\初始化mysqld服务
-
-
-
- New password: \\输入设置密码
-
- Re-enter new password:
-
- Estimated strength of the password: 100
- Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
- 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) :
-
- ... skipping.
-
-
- 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) :
-
- ... skipping.
- 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) :
-
- ... skipping.
- 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) :
-
- ... skipping.
- All done!
- vim /etc/my.cnf
-
- 添加以下内容:
- [mysqld]
- server-id=1 \\指定ID,主从的两台虚拟机ID必须不同
- log-bin=mysql-bin \\mysql根据配置自动设置指定的二进制文件名
systemctl restart mysqld
- mysql -uroot -p
-
- \\输入密码
create user 'synch'@'192.168.100.131' identified by '123456';
grant replication slave on *.* to 'synch'@'192.168.100.131';
select * from mysql.user where User='synch'\G;
show grants for 'synch'@'192.168.100.131';
flush privileges;

- vim /etc/my.cnf
- 添加以下内容
- [mysqld]
- server-id=2
- replicate-do-db=test \\指定要复制的数据库
systemctl restart mysqld
- mysql -uroot -p
- \\输入密码
stop slave;
- change
- master to
- master_host='192.168.100.130', \\IP地址为Node1的IP地址
- master_port=3306,
- master_user='synch',
- master_password='123456',
- master_log_file=‘mysql-bin.000002', \\文件和位置为show master status图片的内容
- master_log_pos=1902;
start slave;
show slave status\G;
在同步状态查看到Slave_IO_Running和Slave_SQL_Running两个状态皆为YES即证明主从数据库配置成功。
