码迷,mamicode.com
首页 > 数据库 > 详细

mysql配置主从同步

时间:2018-07-27 22:23:56      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:inno   verify   running   war   replica   html   .so   mysql5.6   ide   

先安装mysql

注意指定编码为utf8mb4

https://www.cnblogs.com/yunns/p/4877333.html

主节点: 添加2行

[mysql]
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock

[mysqld]
skip-name-resolve
port = 3306
socket=/var/lib/mysql/mysql.sock
basedir=/main/mysql5.6.41
datadir=/main/mysql5.6.41/data
max_connections=200
character-set-server=utf8mb4
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
collation-server = utf8mb4_unicode_ci
init_connect=SET NAMES utf8mb4
character-set-client-handshake = FALSE
# master-slave 需要主从复制的时候才添加下面两行
log-bin=mysql-bin
server-id=1

 

从节点: 添加一行id

[mysql]
default-character-set=utf8mb4
socket=/var/lib/mysql/mysql.sock

[mysqld]
skip-name-resolve
port = 3306
socket=/var/lib/mysql/mysql.sock
basedir=/main/mysql5.6.41
datadir=/main/mysql5.6.41/data
max_connections=200
character-set-server=utf8mb4
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=16M
collation-server = utf8mb4_unicode_ci
init_connect=SET NAMES utf8mb4
character-set-client-handshake = FALSE
# master-slave 需要主从复制的时候才添加下面1行
server-id=2

然后安装:

./scripts/mysql_install_db --user=mysql  --defaults-file=/etc/my.cnf --basedir=/main/mysql5.6.41/  --datadir=/main/mysql5.6.41/data/

或者正常单节点安装完了再来设置最后那两行也行.

 

 

 

然后开始master上配置一个用户,允许slave从远端登录:

ip是slave的ip:

CREATE USER repl@172.17.222.161 IDENTIFIED BY 迷马密码米吗;#创建用户
GRANT REPLICATION SLAVE ON *.* TO repl@172.17.222.161;
flush privileges;

然后查看master的状态,尤其注意File和Position:

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

 

然后去从节点,登录mysql后配置slave:

MASTER_LOG_FILE和position信息和上面相同, 配置让他从主节点同步信息:

mysql> CHANGE MASTER TO
    ->      MASTER_HOST=172.17.222.160,
    ->      MASTER_USER=repl,
    ->      MASTER_PASSWORD=迷马密码米吗,
-> MASTER_LOG_FILE=mysql-bin.000003,
-> MASTER_LOG_POS=120;


Query OK,
0 rows affected, 2 warnings (0.01 sec)

启动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: 172.17.222.160
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 120
               Relay_Log_File: iz2zej5nlztmm1frnyl4wxz-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: Yes
            Slave_SQL_Running: 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: 120
              Relay_Log_Space: 474
              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: 29c0bb13-9199-11e8-92f2-00163e123ea4
             Master_Info_File: /main/mysql5.6.41/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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
1 row in set (0.00 sec)

 

 

 

 

 

 

如果遇到错误:

这是000003这个文件在master上不存在:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.17.222.160
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 73
               Relay_Log_File: iz2zej5nlztmm1frnyl4wxz-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 73
              Relay_Log_Space: 120
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: Could not find first log file name in binary log index file
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 29c0bb13-9199-11e8-92f2-00163e123ea4
             Master_Info_File: /main/mysql5.6.41/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 180727 21:10:17
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

 

 

 

这是position和master的状态不匹配:

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 172.17.222.160
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 73
               Relay_Log_File: iz2zej5nlztmm1frnyl4wxz-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000003
             Slave_IO_Running: No
            Slave_SQL_Running: 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: 73
              Relay_Log_Space: 474
              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: NULL
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 1236
                Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: binlog truncated in the middle of event; consider out of disk space on master; the first event mysql-bin.000003 at 73, the last event read from ./mysql-bin.000003 at 73, the last byte read from ./mysql-bin.000003 at 92.
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: 29c0bb13-9199-11e8-92f2-00163e123ea4
             Master_Info_File: /main/mysql5.6.41/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 180727 21:12:45
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
1 row in set (0.00 sec)

 

mysql配置主从同步

标签:inno   verify   running   war   replica   html   .so   mysql5.6   ide   

原文地址:https://www.cnblogs.com/radio/p/9379718.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!