Mysql主从复制的应用场景
一,从服务器作为主服务器的事实数据备份
二,主从服务器实现读写分离,主服务器写,从服务器读。
在一般应用中,读取的压力十分巨大,写的压力反而会小很多。所以可以实现一主多从来实现。
搭建环境:Ubuntu 18.04
建议root权限安装
配置root账号密码
sudo passwd root
安装Mysql数据库
apt -y install mysql-server mysql-client
配置数据库
mysql_secure_installtion
首先:配置主服务器master
配置数据库的配置文件
vim /etc/mysql/mysql.conf.d/mysql.cnf
内容
#bind-address = 127.0.0.1 //注释掉
server-id = 1 //取消注释
log_bin = /var/log/mysql/master-bin.log //修改值
重启mysql
systemctl restart mysql
开机启动mysql
systemctl enable mysql
进入数据库进行配置
mysql -uroot -p
..
创建用于slave获取master机器上账号
set global validate_password_policy=0;
grant replication slave on . to 'rep'@'%' identified by '87488587';
flush privileges;
set global validate_password.policy=0;//配置Mysql8
create user 'rep'@'%' identified with mysql_native_password by '87488587';//配置Mysql8
grant replication slave on *.* to 'rep'@'%';//配置Mysql8
flush privileges;
查看主服务Mysql的状态
mysql> show master status;
#这两个值很重要,故单独把值提出来
mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| master-mysql-bin.000001 | 154 | | | |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
从服务的配置
配置Ubuntu 18.04 安装Mysql 配置Mysql
/etc/mysql/mysql.conf.d/mysql.cnf
配置从数据库文件
#bind-address = 127.0.0.1 //注释掉
server-id = 2 //修改为2,从服务器
进入数据库操作
mysql -uroot -p
配置主数据库的信息
change master to
master_host='',
master_port=3306,
master_user='',
master_password='',
master_log_file='master-bin.000001',//主服务器的状态
master_log_pos=154;//主服务器的位置
从数据库开始运行
start slave;
查看从数据库的状态
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.2.4
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master-mysql-bin.000012
Read_Master_Log_Pos: 323
Relay_Log_File: master_2-relay-bin.000019
Relay_Log_Pos: 550
Relay_Master_Log_File: master-mysql-bin.000012
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 323
Relay_Log_Space: 933
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 1
Master_UUID: 04225056-0b88-11ea-a726-00155d708e5e
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
当从库的 IO线程和SQL线程的状态都是yes时
说明主从模式配置成功。
至此,可以去主数据库建立数据库,看看是否到同步到从数据库
配置数据库的主主配置
主主配置其实是互为主从。
其中要注意的自增的解决。当出现某些情况下,会导致自增字段的重复引起混乱。
vim /etc/mysql/mysql.conf.d/mysql.cnf
log-slave-updates = true #将复制事件写入binlog,一台服务器既做主库又做从库此选项必须要开启
#masterA自增长IDauto_increment_offset = 1 //初始值
#字段的增量:1,3,5,7,auto_increment_increment = 2
#masterB自增加IDauto_increment_offset = 2 //初始值
#字段的增量:2,4,6,8auto_increment_increment = 2
主主服务器都需要建立同步账号
#两个服务器都需要配置grant replication slave on *.* to 'repl'@'%' identified by '12345678';
flush privileges;
其余和主从服务器配置一样的。