作者:E4b9a6, 创建:2023-02-13, 字数:4093, 已阅:93, 最后更新:2023-02-13
准备软件以及系统
为了方便称呼
接下来我们将需要备份(数据来源)的服务器称为 Master服务器,简称M服务器
对进行备份存储的服务器称为 Slave服务器,简称S服务器
首先安装MySQL5.6数据库,Cent7OS默认安装的MySQL版本已经是10.1了
所以我们需要从官网手动安装repo包,MySQL官网Repo包链接
wget https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
sudo rpm -ivh mysql80-community-release-el7-1.noarch.rpm
安装之后我们需要手动指定安装的版本,使用 sudo vim /etc/yum.repos.d/mysql-community.repo,其中enabled=0是禁用版本,enabled=1是启用指定版本
保存文件后使用 yum repolist enabled | grep mysql 查询已经启用的MySQL版本
安装初始化数据库并设置开机自启动
sudo yum install mysql-community-server
sudo systemctl enable mysqld
sudo mysql_secure_installation
sudo systemctl start mysqld
进入数据库,设置从库连接到Master数据库的备份用户,并授权相关数据库(根据数据库数量自行添加)
sudo mysql -p
grant replication slave on . to 'chancel'@'192.168.1.2' identified by 'chancel';
grant all privileges on cplusplusDB.* to chancel@'192.168.1.2' identified by ‘ChancelDB';
开启主数据库的BinaryLog配置,sudo vim /etc/my.cnf,修改如下
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
# binarylog部分
log-bin = mysql-bin
binlog_format = mixed
server-id = 1
read-only=0
#binlog-do-db=xxx
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
auto-increment-increment = 10
auto-increment-offset = 1
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
修改完成之后重启数据库,sudo systemctl restart mysqld,此时备份已经开启
但首次备份仍需手动参与,步骤啥先锁住Master数据库,然后导出数据库数据,记录master状态并记录下File和Position后解锁数据库
相关语句如下
# 锁库
flush tables with read lock;
# 导出数据
mysqldump -uroot -p [databasename] -R -E --default-character-set=utf8 > [database_backup].sql
# 查看Master状态,记录下File和Position
show master status\G
# 解锁数据库
unlock tables;
Master数据库配置到此结束,我们将备份的数据传到Slave服务器
安装MySQL5.6的步骤与 Master服务器安装数据库一致,此处不再复述,安装完成之后启动数据数据库
导入初始数据,语法参考
mysql -u root -p [databasename] <'[database_backup]'
接着我们修改从数据库的BinaryLog配置,sudo vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
# binarylog部分
log-bin=mysql-bin
binlog_format=mixed
server-id=2
replicate-ignore-db=mysql
replicate-ignore-db=performance_schema
replicate-ignore-db=information_schema
relay_log=mysqld-relay-bin
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
修改完成之后,重启数据库 sudo systemctl restart mysqld,此时备份没有开启,因为没有指定File以及Postion值
使用ROOT权限账户进入数据库,设定初始化的File以及Postion值
CHANGE MASTER TO
MASTER_HOST='192.168.1.1',
MASTER_USER='chancel',
MASTER_PASSWORD='chancel',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=245;
# 执行从库跟踪主库
start slave;
到这一步主从设置就完成了,可以在主库添加新的表/数据进行测试,如需要 主主同步,将Master服务器与Slave服务器的设置反过来设置一遍即可
mysql5.6的复制引入了uuid的概念,各个复制结构中的server_uuid得保证不一样,但是查看到直接copy data文件夹后server_uuid是相同的
可以在数据库中用ROOT账户使用show variables like '%server_uuid%';查看UUID
请保证主从的/etc/my.cnf文件中的serverid不一致,再确认/data文件下的auto.cnfid不一致(大部分情况下位于/var/lib/mysql下)