mysql 主从复制
MySQL数据库自身提供的主从复制功能可以方便的实现数据的多处自动备份,实现数据库的拓展。多个数据备份不仅可以加强数据的安全性,通过实现读写分离还能进一步提升数据库的负载性能。
主要通过日志方式来复制对应的操作到每一个从数据。
场景:
- 现在只有一台服务器,前期还不需要多台服务器的需求,以后可能随着业务量的提升来增加服务器。
- 现在就打算开启mysql的主从功能,好为后面增加从库来提升改善单数据库的读。
方案:
- 先在主数据数据库启动为master 模式,记录日志的起始点,后期再接入从库,但不能马上验证主从关系的是否成功搭建。
- 在同一台服务器上跑两个不同端口的mysqld 服务器,一个主一个从。不过搭建流程稍显复杂。
- 通过简单配置 用docker 跑两个mysql 对应版本的镜像 映射不同端口来做主从。
下面是通过mysql镜像跑两个对应的容器来映射不同的端口
# docker获取mysql 的镜像后 通过 -v 来指定服务器目录到docker中使用,这样就不会随着容器的删除数据重置。
# msyql 主数据库
# -v 说明 宿主目录:容器目录
# 目录/vdb/data/docker/mysql/master/logs 记录主数据数据操作的bin日志
# 目录 /vdb/data/docker/mysql/master/data 存放数据生成的database 对应数据
# 目录 /vdb/data/docker/mysql/master/conf.d 指定对应的配置目的对容器中
# -p 映射端口 宿主端口:容器端口
# -e 设置对应的容器中mysql 的root 登录密码
docker run --name mysql -v /vdb/data/docker/mysql/master/logs:/var/lib/mysql-logs -v /vdb/data/docker/mysql/master/data:/var/lib/mysql -v /vdb/data/docker/mysql/master/conf.d:/etc/mysql/mysql.conf.d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=Game@Root123 -d ee7cbd482336
#mysql 从数据库
docker run --name mysql-slave -v /vdb/data/docker/mysql/slave/mysql:/var/lib/mysql -v /vdb/data/docker/mysql/slave/conf.d:/etc/mysql/mysql.conf.d -p 3308:3306 -e MYSQL_ROOT_PASSWORD=Game@Root123 -d ee7cbd482336
主从数据库运行的配置:
# 主数据库配置 放在/vdb/data/docker/mysql/master/conf.d 目录下
# mysqld-master.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
######## 主数据库要添加的配置 #############
#repeatset master
log-bin=/var/lib/mysql-logs/mysql-bin #开启二进制日志
#log fromat
binlog_format=mixed # 指定日志文件格式
#master server-id
server-id=101 # 指定唯一serverid 每个数据库不能重复
#从数据库配置 放在/vdb/data/docker/mysql/slave/conf.d 目录下
# mysqld-slave.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
#log-error = /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address = 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
######## 从数据库要添加的配置 ########
#slave server-id
server-id=102
在主数据中添加从数据库的访问者
'slave'@'%' slave 账号, % 指定所有电脑,可以指定ip 如 192.168.1.1
IDENTIFIED BY 为密码:
分配权限
mysql> GRANT replication slave ON *.* TO 'slave'@'%' IDENTIFIED BY '111111';
查看操作日志和postion
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000003 | 73 | | | |
+------------------+----------+--------------+------------------+-------------------+
在从数据中指定主数据的bin log 文件和position
mysql> CHANGE MASTER TO
-> MASTER_HOST='182.92.172.80',
-> MASTER_PROT =3307,
-> MASTER_USER='rep1',
-> MASTER_PASSWORD='slavepass',
-> MASTER_LOG_FILE='mysql-bin.000003',
-> MASTER_LOG_POS=73;
查看状态是否连接成功
mysql> show slave status;
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State | Master_Host | Master_User | Master_Port | Connect_Retry | Master_Log_File | Read_Master_Log_Pos | Relay_Log_File | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 172.16.0.11 | slave | 3307 | 60 | mysql-bin.000003 | 345763 | 19885d75bedd-relay-bin.000002 | 345255 | mysql-bin.000003 | Yes | Yes | | | | | | | 0 | | 0 | 345763 | 345469 | None | | 0 | No | | | | | | 0 | No | 0 | | 0 | | | 101 | 1edd4d11-48b7-11e9-a91a-0242ac110002 | /var/lib/mysql/master.info | 0 | NULL | Slave has read all relay log; waiting for more updates | 86400 | | | | | | | | 0 | | | |
+----------------------------------+-------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+----------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
搭建结束:可以去住数据库创建数据和插入数据验证主从复制是否成功。