- cat /etc/redhat-release
- CentOS Linux release 7.9.2009 (Core)
-
- 192.168.183.137 mysql-master
- 192.168.183.153 mysql-slave-1
- 192.168.183.154 mysql-slave-2
-
- # 关闭SELINUX
- sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
- setenforce 0
-
- # 关闭防火墙
- systemctl stop firewalld
- systemctl disable firewalld
三台机器均执行
- wget https://dev.mysql.com/get/mysql80-community-release-el7-7.noarch.rpm
- yum -y install mysql80-community-release-el7-7.noarch.rpm
- yum clean all && yum makecache
- yum -y install mysql-community-server
- systemctl start mysqld
- systemctl enable mysqld
-
- # 查看root密码
- grep 'temporary password' /var/log/mysqld.log
- mysql -uroot -ppassword
-
- # 修改root密码
- ALTER USER 'root'@'localhost' IDENTIFIED BY 'password';
- FLUSH PRIVILEGES;
- vim /etc/my.cnf
- # 加入以下内容
- server_id=137 # server_id需要保证唯一性,不可与其他从服务器相同 如果为0会拒绝所有从服务器连接
- log-bin=mysql-bin # 设置同步的binary log二进制日志文件名前缀,默认是binlog
- binlog_ignore_db = information_schema # 不需要同步的数据库
- binlog_ignore_db = performance_schema # 不需要同步的数据库
- innodb_flush_log_at_trx_commit=1 # 我们每次事务的结束都会触发Log Thread 将log buffer中的数据写入文件并通知文件系统同步文件。这个设置是最安全的设置,能够保证不论是MySQL Crash 还是OS Crash或者是主机断电都不会丢失任何已经提交的数据。
- vim /etc/my.cnf
- # 加入以下内容
- server_id=153
- log-bin=mysql-bin
- binlog_ignore_db = information_schema
- binlog_ignore_db = performance_schema
- innodb_flush_log_at_trx_commit=1
- vim /etc/my.cnf
- # 加入以下内容
- server_id=154
- log-bin=mysql-bin
- binlog_ignore_db = information_schema
- binlog_ignore_db = performance_schema
- innodb_flush_log_at_trx_commit=1
修改完成之后全部重启systemctl restart mysqld
- mysql -uroot -ppassworn
-
- # 创建同步账户master节点 执行
-
- mysql> CREATE USER slave@'%' IDENTIFIED BY 'QAZqaz1234@';
- mysql> GRANT ALL PRIVILEGES ON *.* TO 'slave'@'%'WITH GRANT OPTION;
- mysql> CHANGE MASTER TO GET_MASTER_PUBLIC_KEY=1;
- mysql> FLUSH PRIVILEGES;
- mysql> SHOW MASTER STATUS;
-
- mysql> show master status;
- +------------------+----------+--------------+---------------------------------------+-------------------+
- | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
- +------------------+----------+--------------+---------------------------------------+-------------------+
- | mysql-bin.000001 | 873 | | information_schema,performance_schema | |
- +------------------+----------+--------------+---------------------------------------+-------------------+
- mysql -uroot -ppassworn
-
- # slave-1和slave-2都是同样的操作
- mysql> change master to
- master_host='192.168.183.137',master_user='slave',master_password='QAZqaz1234@',
- master_log_file='master-bin.000001',master_log_pos=873;
-
- # 启动同步
- mysql> START SLAVE;
- mysql> FLUSH PRIVILEGES;
mysql> SHOW SLAVE STATUS\G
Slave_IO_Running: Connecting,
报错:Error connecting to source 'slave@192.168.183.137:3306'. This was attempt 5/86400, with a delay of 60 seconds between attempts. Message: Authentication plugin 'caching_sha2_password' reported error: Authentication requires secure connection.
发生这个问题的原因是在 mysql 8.0 以后,caching_sha2_password是默认的身份验证插件,而不是以往的mysql_native_password。在 MySQL Command Line 工具下修改 mysql 的默认身份验证插件即可。
登录主库
- ALTER USER slave@'%' IDENTIFIED WITH mysql_native_password BY 'QAZqaz1234@';
-
- FLUSH PRIVILEGES;
然后从库重新同步
- # slave-1和slave-2都是同样的操作
- mysql> STOP SLAVE;
-
- mysql> change master to
- master_host='192.168.183.137',master_user='slave',master_password='QAZqaz1234@',
- master_log_file='master-bin.000001',master_log_pos=1323;
-
- # 启动同步
- mysql> RESET SLAVE;
- mysql> START SLAVE;
- mysql> FLUSH PRIVILEGES;
再次查看同步状况可以看到已经成功:
mysql> SHOW SLAVE STATUS\G
在master节点上执行SQL语句
- create database db_test;
- show databases;
在从库上面查看
在master的db_test库里面创建表
- use db_test;
- # 创建一个my_user表:
- CREATE TABLE `my_user` (
- `id` tinyint(4) NOT NULL AUTO_INCREMENT,
- `account` varchar(255) DEFAULT NULL,
- `passwd` varchar(255) DEFAULT NULL,
- PRIMARY KEY (`id`)
- );
-
- # 插入数据:
- INSERT INTO `my_user` VALUES ('1', 'admin', 'admin');
- INSERT INTO `my_user` VALUES ('2', 'pu', '12345');
- INSERT INTO `my_user` VALUES ('3', 'system', 'system');
-
- # 查看插入数据
- select * from db_test.my_user;
到从库上都能查到则说明没问题。