MySQL主从复制原理图:
一、基本配置:虚拟机开两个Ubuntu server
主数据库:
开启二进制日志
配置唯一server-id
获得master二进制日志文件名及位置
创建一个用于slave和master通信的账号
从数据库:
配置唯一server-id
使用master创建的账号读取master的二进制日志
启动slave服务
二、前提:
Ubuntu server16.04.5
mysql 5.7.25
主数据库:10.31.151.44
从数据库:10.31.151.45
三、主数据库修改:
找到主数据库的的配置文件,我的是/etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]下添加
log-bin=mysql-bin
server-id=1
如下:
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
log-bin=mysql-bin
server-id=1
开启MySQL,创建用于通信的账号,并赋予权限:
CREATE USER 'ceshi'@'10.31.151.45' IDENTIFIED BY 'slavepass'; # 创建账号
GRANT REPLICATION SLAVE ON *.* TO 'ceshi'@'10.31.151.45';# 赋予权限
flush privileges; # 刷新权限
重启MySQL sudo service mysql restart
查看master 状态,并记录二进制文件名及位置
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
四、从数据库修改:
找到mysql配置,添加server-id
[mysqld]
server-id=2 #设置server-id,必须唯一
开启mysql,执行SQL语句
CHANGE MASTER TO MASTER_HOST='10.31.151.44',MASTER_USER='ceshi',MASTER_PASSWORD='slavepass',MASTER_LOG_FILE='mysql-bin.000004',MASTER_LOG_POS=154;
启动slave同步:
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
查看slave状态:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.31.151.44
Master_User: rep2
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 154
Relay_Log_File: localhost-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes # 必须为yes,如果是contenting,注意账号或IP有没有写错
Slave_SQL_Running: Yes # 必须为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: 154
Relay_Log_Space: 531
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: 5a786ac2-3766-11e9-b981-000c294cbe35
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)
五、验证
验证主数据库:
主数据库创建一个叫ceshi的库:
mysql> create database ceshi charset=utf8;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ceshi |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
验证从数据库:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| ceshi |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
MySQL主从复制配置成功。