MYSQL主从复制环境构建至少需2台服务器,可以配置1主多从,多主多从,本实验为1主1从,实验步骤如下:
系统环境准备
Master:192.168.133.134
Slave: 192.168.133.135
关闭防火墙
$ systemctl stop firewalld
$ setenforce 0
$ vi /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled enforcing-->disabled
# SELINUXTYPE= can take one of three values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
Master端安装及配置
yum下载mysql
$ yum install mariadb mariadb-server epel-release net-tools -y
启动mysql
$ systemctl start mariadb
添加开机自启
$ systemctl enable mariadb
查看mysql是否正常启动
$ netstat -nltp;ps -ef|grep mysql
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 9618/mysqld
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 7527/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 7258/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6862/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 6865/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7268/master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 7527/X
tcp6 0 0 :::22 :::* LISTEN 6862/sshd
tcp6 0 0 ::1:631 :::* LISTEN 6865/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 7268/master
mysql 9453 1 0 20:32 ? 00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql 9618 9453 0 20:32 ? 00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root 9732 9198 0 20:37 pts/1 00:00:00 grep --color=auto mysql
编辑 /etc/my.cnf 文件
在[mysqld]下面添加如下代码:
server-id=1 #数据库唯一ID,主从的标识号绝对不能重复,master端server-id与slave端不一样即可,相当于身份id
log-bin=mysql-bin #开启bin-log,并指定文件目录和文件名前缀,记录执行的sql语句,相当于日志功能
binlog-ignore-db=mysql #不同步的系统数据库。如果是多个不同步库,就以此格式另写几行;也可以在一行,中间逗号隔开
sync_binlog = 1 #确保binlog日志写入后与硬盘同步
binlog_checksum = none #跳过现有的采用checksum的事件,mysql5.6.5以后的版本中binlog_checksum=crc32,而低版本都是binlog_checksum=none
binlog_format = mixed #bin-log日志文件格式,设置为MIXED可以防止主键重复
配置文件修改完记得重启mysql $ systemctl restart mariadb 如果无法正常启动报错如下: Job for mariadb.service failed because the control process exited with error code. See “systemctl status mariadb.service” and “journalctl -xe” for details. 执行如下命令解决: $ cd /var/lib/mysql $ mv ib_logfile0 ib_logfile0.bak $ mv ib_logfile1 ib_logfile1.bak 应该就行了
在Master上设置同步权限
# 进入Master数据库
$ mysql
# 只允许192.168.133.135使用root用户,且密码为"fcz123"连接主库做数据同步
$ grant replication slave,replication client on *.* to root@'192.168.33.135' identified by "fcz123";
# 允许所有使用root用户,密码为"fcz123"连接主库做数据同步
$ grant replication slave,replication client on *.* to root@'%' identified by "fcz123";
# 设置数据库密码
$ set password for 用户名@localhost = password('新密码');
# 刷新权限
$ flush privileges;
# 权限查看
$ show grants;
$ show grants for root@'192.168.133.135';
锁库(防止导出过程中有数据写入)
# 锁定数据库
$ flush tables with read lock;
# 查看数据库状态
$ show master status;
# 创建目录
$ mkdir /fcz
# 导出需要同步的库
$ mysqldump -uroot -p'' -S /var/lib/mysql/mysql.sock --events -A -B |gzip >/fcz/mysql_back.$(date +%F).sql.gz
Enter password: 输入数据库密码
# 解除锁定
$ unlock tables ;
Slave端安装及配置
安装步骤如上
编辑 /etc/my.cnf 文件
在[mysqld]下面添加如下代码:
server-id=2 #设置从服务器id,必须于主服务器不同
replicate-ignore-db=mysql #不同步的数据库
slave-skip-errors = all #跳过所有的错误,继续执行复制操作
配置文件修改完记得重启mysql $ systemctl restart mariadb
把从主库导出的数据恢复到从库
$ cd /fcz
$ scp mysql_back.2022-05-18.sql.gz root@192.168.133.135:/fcz
配置主从同步指令
#进入Slave数据库
$ mysql
#执行同步前,要先关闭slave
$ stop slave;
#需要和主数据库的show master status命令结果保持一致
$ change master to master_host='Master_ip',master_user='用户名',master_password='密码',master_log_file='File',master_log_pos=Position;
# 启动Slave端
$ start slave;
# 查询数据库状态
$ show slave status \G;
找到下面这两行,如果都为Yes,那么恭喜你主从同步配置成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
在Master写入数据查看是否同步
主库创建数据库 从库查看数据库
|