对mysql进行主从复制的简单操作,只限于简单操作,下面的操作均在 centos8 中进行的,centos7也可以看,无妨
主从复制的操作主要分为无数据复制和有数据复制,但是主从关系分为好多种
主从复制(也称 AB 复制)允许将来自一个MySQL数据库服务器(主服务器)的数据复制到一个或多个MySQL数据库服务器(从服务器)。
主要准备两个纯净版虚拟机,用来安转mysql,不过不是纯净虚拟机没关系
下面这几个操作是两个虚拟机都需要做的
# 清除虚拟机中的mysql和mariaba相关文件
yum -y erase `rpm -qa | grep -E "mysql|mariadb"`
# 清除相关的文件
rm -rf /etc/my* /var/lib/mysql* /var/log/mysql*
# 看是否清楚干净
[[ ! -f /etc/my.cnf ]] && [[ ! -d /var/lib/mysql ]] && [[ ! -f /usr/bin/mysql ]] && echo "数据库已清除" || echo "数据库未清理"
systemctl stop firewalld;setenforce 0
yum install -y mysql-community-server
systemctl start mysqld
mysqladmin -p"`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password '1'
下面分主库和从库
# 配置log-bin
mkdir /data/log-bin -p
chown mysql.mysql /data/log-bin -R
vim /etc/my.cnf
# 里面添加下面两句
server-id=1
log-bin=/data/log-bin/mysql-bin
# 重启mysql
systemctl restart mysqld
mysql -p1
mysql> create user 'lx'@'%' identified by '1';
mysql> grant replication slave on *.* to 'lx'@'1';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1264 | | | |
+------------------+----------+--------------+------------------+-------------------+
vim /etc/my.cnf
server-id=2
# 重启数据库
systemctl restart mysqld
mysql -p1
mysql> change master to
-> master_host='主库ip',
-> master_port=3306,
-> master_user='lx',
-> master_password='1',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1264;
mysql> start slave;
mysql> show slave status;
Slave_IO_State: Waiting for source to send event
Master_Host: master
Master_User: l
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 781
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
# 上面两个是yes,则表示配置成功
# 如果有问题的话,注意两个服务器的防火墙SELinux要关掉,配置要写对之类的问题
# 之后的话在主库做的修改在从库中就能看到了
思路:锁库,备份数据文件 然后配置主从复制
主要准备两个纯净版虚拟机,用来安转mysql,不过不是纯净虚拟机没关系
下面这几个操作是两个虚拟机都需要做的
# 清除虚拟机中的mysql和mariaba相关文件
yum -y erase `rpm -qa | grep -E "mysql|mariadb"`
# 清除相关的文件
rm -rf /etc/my* /var/lib/mysql* /var/log/mysql*
# 看是否清楚干净
[[ ! -f /etc/my.cnf ]] && [[ ! -d /var/lib/mysql ]] && [[ ! -f /usr/bin/mysql ]] && echo "数据库已清除" || echo "数据库未清理"
systemctl stop firewalld;setenforce 0
yum install -y mysql-community-server
systemctl start mysqld
mysqladmin -p"`awk '/temporary password/{p=$NF}END{print p}' /var/log/mysqld.log`" password '1'
下面分主库和从库
# 配置log-bin
mkdir /data/log-bin -p
chown mysql.mysql /data/log-bin -R
vim /etc/my.cnf
# 里面添加下面两句
server-id=1
log-bin=/data/log-bin/mysql-bin
# 重启mysql
systemctl restart mysqld
mysql -p1
mysql> create user 'lx'@'%' identified by '1';
mysql> grant replication slave on *.* to 'lx'@'1';
mysql> flush privileges;
mysql> flush tables with read lock;
mysql> show master status;
mysqldump -uroot -p1 -A > /all.sql
# 复制到从库的服务器上
scp /all.sql 从库服务器ip:/
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 1264 | | | |
+------------------+----------+--------------+------------------+-------------------+
mysql> unlock tables;
vim /etc/my.cnf
server-id=2
# 重启数据库
systemctl restart mysqld
mysql -p1 < /all.sql
mysql -p1
mysql> change master to
-> master_host='主库ip',
-> master_port=3306,
-> master_user='lx',
-> master_password='1',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1264;
mysql> start slave;
mysql> show slave status;
Slave_IO_State: Waiting for source to send event
Master_Host: master
Master_User: l
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 781
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 326
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
# 上面两个是yes,则表示配置成功
# 如果有问题的话,注意两个服务器的防火墙SELinux要关掉,配置要写对之类的问题
# 之后的话在主库做的修改在从库中就能看到了