IP | 服务器版本 | 名称 |
192.168.56.136 | CentOS 7.9 | 主服务器-1 |
192.168.56.140 | CentOS 7.9 | 主服务器-2 |
一、安装前的准备
1、卸载老版本
(1)查看是否安装mariadb(centos7默认安装)
命令: rpm -qa | grep mariadb
(2)卸载mariadb
命令:rpm -e mariadb-libs-5.5.68-1.el7.x86_64 --nodeps
2、安装依赖
命令:yum -y install perl perl-devel
二、安装MySQL(RPM包安装)
1、下载安装包
2、解压安装包
3、按如下顺序安装rpm包
rpm -ivh mysql-community-common-8.0.11-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-8.0.11-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-8.0.11-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-8.0.11-1.el7.x86_64.rpm
4、启动服务
命令:systemctl restart mysqld
5、修改密码并授权
(1)通过日志查找到初始密码
命令:grep 'password' /var/log/mysql.log
(2)登录MySQL
mysql -u root -p(回车后输入刚查找到的密码)
(3)重新设置密码
alter user root@'localhost' identified with mysql_native_password by 'new_password'
# 5.7版本之后使用该命令设置新密码
(4)授权root用户
# 修改允许访问的主机
use mysql;
update user set host='%' where user='root';
# 授予所有主机通过root用户和密码访问数据库所有表所有权限
grant all on *.* to root@'%';
# 刷新权限
flush privileges;
三、配置主主同步
1、修改配置文件/etc/my.cnf
[主服务器-1]
server-id=136
log-bin=master_136_bin
[主服务器-2]
server-id=140
log-bin=master_140_bin
2、创建同步账号
[主服务器-1]
# 登录MySQL
mysql -u root -p
# 进入mysql库
use mysql;
# 创建同步账号
create user masterone@'192.168.56.140' identified with mysql_native_password by 'Master_123';
# 授予权限
grant replication slave on *.* to mastertone@'192.168.56.140';
# 刷新
flush privileges;
[主服务器-2]
# 登录MySQL
mysql -u root -p
# 进入mysql库
use mysql;
# 创建同步账号
create user mastertwo@'192.168.56.136' identified with mysql_native_password by 'Master_123';
# 授予权限
grant replication slave on *.* to masterttwo@'192.168.56.136';
# 刷新
flush privileges;
3、开启同步
[主服务器-1]
# 停止同步
stop slave;
# 修改同步信息
# master_log_file和master_log_pos的信息通过在另一台数据库执行show master status获得
change master to master_host='192.168.56.140',master_user='master_two',master_password='Master_123',master_log_file='master-two-bin.000001',master_log_pos=334;
# 开启同步
start slave;
# 确认同步状态
show slave status;
[主服务器-2]
# 停止同步
stop slave;
# 修改同步信息
# master_log_file和master_log_pos的信息通过在另一台数据库执行show master status获得
change master to master_host='192.168.56.136',master_user='master_one',master_password='Master_123',master_log_file='master-one-bin.000001',master_log_pos=334;
# 开启同步
start slave;
# 确认同步状态
show slave status;