目录
系统环境描述:本教程基于CentOS 8.0版本虚拟机
mysql 规划(二主二从)
节点 | MySQL(主) | MySQL(从) | IP |
node1 | * | 192.168.31.8 | |
node2 | * | 192.168.31.9 | |
node3 | * | 192.168.31.167 | |
node4 | * | 192.168.31.154 |
软件版本:
MySQL ? 5.7
rpm -qa | grep -i mysql
-- 依次卸载上一步骤罗列出来的依赖
yum remove mysql-common-8.0.26-1.module_el8.4.0+915+de215114.x86_64
yum remove mysql57-community-release-el7-10.noarch
find / -name mysql
rm -rf /var/lib/selinux/targeted/active/modules/100/mysql
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql57-community-release-el7-10.noarch.rpm
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
yum -y install mysql-community-server
在CentOS 8 下会报错:错误:没有任何匹配: mysql-community-server
原因是:【注】出现问题的核心原因为:基于RHEL 8和Oracle Linux 8的基于EL8的系统,在默认情况下附带了一个名为mysql的模块。 除非禁用此模块,否则它将屏蔽,
存储库提供的软件包。只要是 EL8系统安装任何版本的 MySQL 都会有这样的问题。
需要执行一下命令:yum module disable mysql
接着再执行:yum -y install mysql-community-server
然后安装是,执行事务检查报错:
错误:事物测试失败:
file /etc/my.cnf from install of mysql-community-server-5.7.43-1.el7.x86_64 conflicts with file from package mariadb-connector-c-config-3.1.11-2.el8_3.noarch
原因分析:
网上搜了下,说是我的yum 里面安装了mariadb-connector-c-config.noarch ,于是就要执行命令查看一下
yum list installed
果然如此,于是,就要删除掉mariadb-connector-c-config.noarch,
yum remove mariadb-connector-c-config.noarch
接着再执行:
yum -y install mysql-community-server
成功了
systemctl start mysqld.service
systemctl status mysqld.service
grep "password" /var/log/mysqld.log
mysql -uroot -p
#设置密码策略为LOW,此策略只检查密码的长度
set global validate_password_policy=LOW;
set global validate_password_length=6;
ALTER USER 'root'@'localhost' IDENTIFIED BY 'xiaoning';
grant all privileges ?on ?*.* to 'root'@'%' identified by 'xiaoning' with grant option;
提示:以上操作,二主二从服务器都需要执行!
vim ?/etc/my.cnf
#开启?进制?志
log-bin=mysql-bin
#设置服务id
server-id=1
#设置不要复制的数据库
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#设置需要复制的数据库
binlog-do-db=mydb
#设置logbin格式;可选值:STATEMENT、ROW、MIXED
binlog_format=STATEMENT
#作为从数据库的时候,有写?操作也要更新?进制?志?件
log-slave-updates=1
#?增?字段从哪个数开始
auto-increment-offset=1
#?增?字段每次递增的量
auto-increment-increment=2
#中继日志
relay-log=relay-log-bin
#中继日志
relay-log-index=relay-log-bin.index
systemctl restart mysqld.service
# 创建slave?户
CREATE USER 'ning_slave1'@'%' IDENTIFIED BY 'xiaoning';
# 设置密码
ALTER USER 'ning_slave1'@'%' IDENTIFIED WITH
mysql_native_password BY 'xiaoning';
# 授予复制权限
GRANT REPLICATION SLAVE ON *.* TO 'ning_slave1'@'%';
# 刷新权限
FLUSH PRIVILEGES;
#开启?进制?志
log-bin=mysql-bin
#设置服务id
server-id=2
#设置不要复制的数据库
binlog-ignore-db=mysql
binlog-ignore-db=performance_schema
binlog-ignore-db=sys
#设置需要复制的数据库
binlog-do-db=mydb
#设置logbin格式;可选值:STATEMENT、ROW、MIXED
binlog_format=STATEMENT
#作为从数据库的时候,有写?操作也要更新?进制?志?件
log-slave-updates=1
#?增?字段从哪个数开始
auto-increment-offset=2
#?增?字段每次递增的量
auto-increment-increment=2
#中继日志
relay-log=relay-log-bin
#中继日志
relay-log-index=relay-log-bin.index
systemctl restart mysqld.service
# 创建slave?户
CREATE USER 'ning_slave2'@'%' IDENTIFIED BY 'xiaoning';
# 设置密码
ALTER USER 'ning_slave2'@'%' IDENTIFIED WITH
mysql_native_password BY 'xiaoning';
# 授予复制权限
GRANT REPLICATION SLAVE ON *.* TO 'ning_slave2'@'%';
# 刷新权限
FLUSH PRIVILEGES;
show master status\G
CHANGE MASTER TO MASTER_HOST='192.168.31.9',
MASTER_USER='ning_slave2',
MASTER_PASSWORD='xiaoning',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=123;
注意,这里 MASTER_LOG_POS=123;参照的是执行show master status\G命令后返回的Position: ?123 值;
start slave;
show slave status\G;
说明:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上两个值都为Yes才是正确的。
show master status\G
CHANGE MASTER TO MASTER_HOST='192.168.31.8',
MASTER_USER='ning_slave1',
MASTER_PASSWORD='xiaoning',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=154;
注意,这里 MASTER_LOG_POS=123;参照的是执行show master status\G命令后返回的Position: 154 值;
start slave;
show slave status\G;
说明:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上两个值都为Yes才是正确的。
在主库一新建test库,查看主库二是否同步数据
测试结果发现,主库二同步了主库一新建的数据库,主库部署成功。
#开启?进制?志
log-bin=mysql-bin
#设置服务id
server-id=3
#启?中继?志
relay-log=relay-log-bin
#中继日志
relay-log-index=relay-log-bin.relay
#开启多通道table
master-info-repository=TABLE
relay-log-info-repository=TABLE
systemctl restart mysqld.service
mysql -uroot -p
show master status\G;
CHANGE MASTER TO MASTER_HOST='192.168.31.8',
MASTER_USER='ning_slave1',
MASTER_PASSWORD='xiaoning',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154 for channel 'master1';
CHANGE MASTER TO MASTER_HOST='192.168.31.9',
MASTER_USER='ning_slave2',
MASTER_PASSWORD='xiaoning',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154 ?for channel 'master2';
start slave;
show slave status\G;
说明:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上两个值都为Yes才是正确的。
#开启?进制?志
log-bin=mysql-bin
#设置服务id
server-id=4
#中继日志
relay-log-index=relay-log-bin.relay
#开启多通道table
master-info-repository=TABLE
relay-log-info-repository=TABLE
systemctl restart mysqld.service
mysql -uroot -p
show master status\G;
CHANGE MASTER TO MASTER_HOST='192.168.31.8',
MASTER_USER='ning_slave1',
MASTER_PASSWORD='xiaoning',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154 for channel 'master1';
CHANGE MASTER TO MASTER_HOST='192.168.31.9',
MASTER_USER='ning_slave2',
MASTER_PASSWORD='xiaoning',
MASTER_PORT=3306,
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=154 ?for channel 'master2';
start slave;
show slave status\G;
说明:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
以上两个值都为Yes才是正确的。
从主库一新建一个数据库test3,看下两个从库是否会同步数据
测试结果发现,两个从库同步了主库一新建的数据库test3,整个二主二从服务部署成功。
好了,本次分享就到这里,如果帮助到大家,欢迎大家点赞+关注+收藏,有疑问也欢迎大家评论留言!