Mysql主从复制(详细)

环境搭建

环境信息:

  • master
    • ip:192.168.188.99
    • Mysql版本:5.6.45
    • 服务器版本:CentOS7
  • slave
    • ip:192.168.188.102
    • Mysql版本:5.6.45
    • 服务器版本:CentOS7

配置Mysql

master

在[mysqld]下添加:
#启用二进制日志
log-bin=mysql-bin
#服务器唯一ID,一般取IP最后一段
server-id=99

Master完整配置

[root@master ~]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
# 启用二进制日志
log-bin=mysql-bin
# 服务器的唯一id,一般选取ip地址最后一段
server-id=99

#
# 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
#
# 永久开启慢查询
slow_query_log = ON
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
# 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
# 不区分大小写
# lower_case_table_names = 1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

Slave 配置

仅仅配置这一条就够了,不过一定要注意查看配置是否生效

# 服务器的唯一id,一般选取ip地址最后一段
server-id=102

slave完整配置信息

[root@slave etc]# cat /etc/my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html

[mysqld]
# 启用二进制日志
#log-bin=mysql-bin
# 服务器的唯一id,一般选取ip地址最后一段
server-id=102

#
# 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
#
# 永久开启慢查询
slow_query_log = ON
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
# 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
# 不区分大小写
# lower_case_table_names = 1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# Recommended in standard MySQL setup
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

两台主机(Master和Slave)修改完配置文件之后务必要重启下Mysql

service mysqld restart

Server-id核对

重启完之后登录查看master的server-id是否和mysql.cnf文件中一致

mysql> SHOW VARIABLES LIKE '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 99    |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.00 sec)

重启完之后登录查看slave的server-id是否和mysql.cnf文件中一致

mysql> SHOW VARIABLES LIKE '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 102   |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.00 sec)

Master创建同步账户

主机创建一个新的slave用户,设置复制权限

  • 创建用户

      create user 'slave'@'从机IP' identified by 'root';
    
  • 设置权限

     grant replication slave on *.* to 'slave'@'从机IP' identified by 'root';
    

最后刷新权限信息

FLUSH PRIVILEGES;

查看Master状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000006 |      346 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Slave开启配置同步

登录到slave服务器中执行

CHANGE MASTER TO 
-- Master的地址
MASTER_HOST='192.168.188.99',
-- Master的端口号
MASTER_PORT=3306,
-- Master中刚刚创建的slave用户
MASTER_USER='slave',
-- Master的刚刚创建的slave用户的密码
MASTER_PASSWORD='root',
-- 上面使用show master status查出来的字段File
MASTER_LOG_FILE='mysql-bin.000006',
-- 上面使用show master status查出来的字段Position
MASTER_LOG_POS=120; 

注意语句中间不要断开,master_port为mysql服务器端口号(无引号),master_user为执行同步操作的数据库账户,“120”无单引号(此处的120就是show master status 中看到的position的值,这里的mysql-bin.000006就是file对应的值)。

Slave启动服务器复制功能

登录到slave服务器中执行

start slave

检查从服务器复制功能的状态

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  
--------------------------------  --------------  -----------  -----------  -------------  ----------------  -------------------  -----------------------  -------------  ---------------------  ----------------  -----------------  ---------------  -------------------  ------------------  ----------------------  -----------------------  ---------------------------  ----------  ----------  ------------  -------------------  ---------------  ---------------  --------------  -------------  ------------------  ------------------  ------------------  ---------------  -----------------  --------------  ---------------------  -----------------------------  -------------  -------------  --------------  --------------  ---------------------------  ----------------  ------------------------------------  --------------------------  ---------  -------------------  ---------------------------------------------------------------------------  ------------------  -----------  -----------------------  ------------------------  --------------  ------------------  ------------------  -----------------  ---------------
Waiting for master to send event  192.168.188.99  slave               3306             60  mysql-bin.000006                  346  mysqld-relay-bin.000002            509  mysql-bin.000006       Yes               Yes                                                                                                                                                                 0                         0                  346              683  None                                         0  No                                                                                                                                  0  No                                         0                              0                                                             99  32c5596d-bf2f-11e9-818a-000c298b2a3c  /var/lib/mysql/master.info          0               (NULL)  Slave has read all relay log; waiting for the slave I/O thread to update it               86400                                                                                                                                                           0

Slave_IO_Running及Slave_SQL_Running进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。

测试主从同步

前提,有相同的数据库,有相同的数据.

Mysql主从复制(详细)_第1张图片

Mysql主从复制(详细)_第2张图片
Mysql主从复制(详细)_第3张图片

错误分析

错误代码: 1794
描述:SLAVE IS NOT configured OR failed TO initialize properly. You must AT LEAST SET --server-id TO ENABLE either a MASTER OR a slave. Additional error messages can be FOUND IN the MySQL error log.

遇到这种错误一定要在Master和Slave两个不同机器上使用

**SHOW VARIABLES LIKE '%server_id%';**

命令查看自己的server_id和mysql.cnf文件中配置的是否一致.

这是个巨大的坑啊
End.

你可能感兴趣的:(Mysql,Mysql,主从同步,Java后端,Mysql5.6)