主从同步构建思路:
确保数据相同--从库必须要有主库中的数据
主服务配置--启用binlog日志,授权用户,查看正在使用的日志
从服务器配置--设定server_id,指定主库信息
测试配置--客户端连接主数据库,写入数据,从库上也能查询
主服务器:host50 192.168.4.50
从服务器:host51 192.168.4.51
一,在主服务器host50上配置主服务器
*打开配置文件 vim /etc/my.cnf 修改如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
general-log
log-bin=master51 //启用binlog日志命名前缀master51
server_id=51 //指定服务器ID号
binlog_format="mixed" //指定日志格式
plugin-load="rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so"
rpl_semi_sync_master-enabled = 1
rpl_semi_sync_slave-enabled = 1
/*高可用架构下。需要同时启动master和slave
#validate_password_length=6
#validate_password_policy=0
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@host50]# systemctl restart mysqld
[root@host50]#ls /var/lib/mysql/master50.*
[root@host50]#mysql -uroot -p123456
用户授权:给从库host51添加连接时使用的用户名
mysql> grant replication slave on *.* to repluser@"192.168.4.51" identified by "123qqq...A";
查看正在使用的binlog日志信息
mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 | 154 | | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
*************************************************************************************************************************************
二,在host51中指定从库信息
*打开配置文件 vim /etc/my.cnf 修改如下:
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
server_id=51 //指定服务器ID号
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[root@host51]# systemctl restart mysqld
进入数据库
[root@host50]#mysql -uroot -p123456
指定主库信息
mysql>change master to
>master_user="repluser", //主库授权用户名
>master_host="192.168.4.50", //指定主库IP地址
>master_password="123qqq...A", //授权用户名密码
>master_log_file="master51.000001", //日志文件
>master_log_pos=154; //偏移位置
mysql>start slave; //启动slave进程
mysql>show slave status\G //查看slave状态
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.4.50
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master51.000001
Read_Master_Log_Pos: 599
Relay_Log_File: host52-relay-bin.000005
Relay_Log_Pos: 810
Relay_Master_Log_File: master51.000002
Slave_IO_Running: Yes //IO线程已运行
Slave_SQL_Running: Yes //SQL线程已运行
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: 599
Relay_Log_Space: 1183
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: 51
Master_UUID: 7663861b-8be3-11e8-b256-5254001db4c8
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)
三,测试配置
在主库上操作数据:
-新建newdb库t1表
-任意插入几条表记录
在从库上查看数据更改情况
-确认新建的newdb库t1表
-列出t1表的所有记录
*******************************************************************************************************************************************
配置主从同步可能出现的问题
问题一:
Slave_IO 线程没有运行
---报错:Slave_IO_Running:NO
mysql>show slave status\G
Slave_IO_Running:NO
Last_IO_Error: 报错信息......
原因分析
----连接不上master数据库服务器
解决办法:
---检查物理连接(ping) 检查授权用户
---禁用防火墙 关闭SElinux
---binlog日志文件指定错误(日志名或pos节点)
mysql> stop slave
mysql>change master to 选项=值;
mysql>start slave;
问题二:
Slave_SQL 线程没有运行
---报错:Slave_SQL_Running:NO
mysql>show slave status\G
Slave_SQL_Running:NO
Last_SQL_Error: 报错信息......
原因分析
----执行本机中继日志里的sql命令时,sql命令使用的库,表或记录在本机并不存在
解决办法:
mysql> stop slave;
mysal>...... //创建或恢复需要用到的库或者表
mysql> start slave;