MySQL主从数据库配置

2020-12-18   517 次阅读


参考文章

1、数据库配置

类型地址名称配置
主库192.168.0.104master双核1.5G
从库192.168.0.105slave单核1G

2、配置主库

2.1、修改my.cnf配置文件,添加一下内容
# 服务的唯一编号
server-id = 1

# 开启mysql binlog功能
log-bin = mysql-bin

# binlog记录内容的方式,记录被操作的每一行
binlog_format = ROW

# 减少记录日志的内容,只记录受影响的列
binlog_row_image = minimal

# 指定需要复制的数据库名为jgyw
binlog-do-db = jgyw

修改完成后,重启mysql服务器

service mysqld restart

2.1、添加从库的访问账号

grant replication slave on *.* to 'jgyw'@'192.168.0.231' identified by 'jgyw@HAHA5273yang.';

flush privileges;

执行完成后,在执行 show master status\G;
返回的信息为:

mysql> show master status\G;
*************************** 1. row ***************************
             File: mysql-bin.000001
         Position: 601
     Binlog_Do_DB: jgyw
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 
1 row in set (0.00 sec)

ERROR: 
No query specified

3、配置从库

3.1、配置my.cnf文件

# 服务的唯一编号
server-id = 2

# 开启mysql binlog功能
log-bin = mysql-bin

# binlog记录内容的方式,记录被操作的每一行
binlog_format = ROW

# 减少记录日志的内容,只记录受影响的列
binlog_row_image = minimal

# 指定需要复制的数据库名为jgyw
replicate-do-db = jgyw

修改完成后重启mysql服务 service mysqld restart

3.2、配置同步信息

# 设置主服务器ip,同步账号密码,同步位置
change master to master_host='192.168.0.104',master_user='jgyw',master_password='jgyw@HAHA5273yang.',master_log_file='mysql-bin.000004',master_log_pos=68058;

# 开启同步功能
start slave;

在mysql终端执行 show slave status\G;

返回的配置信息:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.104
                  Master_User: jgyw
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 2213
               Relay_Log_File: Knight02-relay-bin.000006
                Relay_Log_Pos: 875
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: jgyw
          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: 2213
              Relay_Log_Space: 1085
              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: 4a3b6464-d61d-11ea-bbd1-1e4cd34193d4
             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.01 sec)

ERROR: 
No query specified

4、错误处理

mysql> stop slave;
Query OK, 0 rows affected (0.04 sec)
mysql> set global sql_slave_skip_counter=1;
Query OK, 0 rows affected (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

Q.E.D.

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议