MySql数据库主从手动同步

记录一次主从数据不同步,手动同步;

主库:

[jumpserver@SX-Test-Oracle ~]$ /usr/local/mysql/bin/mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 35007
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

从库:

[jumpserver@SX-Test-Nginx ~]$ /usr/local/mysql/bin/mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14562
Server version: 5.5.37-log Source distribution

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

开始手动同步:

1、在主库上执行,锁定数据库(当前状态可查询,不可插入),查看同步状态

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

mysql>  show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000491 | 84843181 | sc,dev       | mysql            |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

2、在主库服务器上执行,导出当前数据库备份,并拷贝到备库服务器上。

[jumpserver@SX-Test-Oracle ~]$ sudo /usr/local/mysql/bin/mysqldump -uroot -p sc >/tmp/sc.20180801
Enter password: 

3、在从库上执行,停止从库的同步状态。

mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

4、在从库上执行,导入主库的备份文件

mysql> source /tmp/sc.20180801
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

5、在从库上执行,查看从库的状态,并设置从库的同步节点。

从第1步中的主库show master status信息里的| File| Position两项取值

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 |
+----------------+---------------+-------------+-------------+---------------+------------------+---------------------+--------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
|                | 172.17.23.203 | root        |        3306 |            60 | mysql-bin.000491 |            84843181 | SX-Test-Nginx-relay-bin.000002 |       7813930 | mysql-bin.000491      | No               | No                | sc,dev          |                     |                    |                        |                         |                             |          0 |            |            0 |            84843181 |         7814094 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |             0 |               |              0 |                |                             |                1 |
+----------------+---------------+-------------+-------------+---------------+------------------+---------------------+--------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+
1 row in set (0.00 sec)

mysql> change master to master_host ='172.17.23.203',master_user ='root',master_port=3306,master_password='XXXXX',master_log_file ='mysql-bin.000491',master_log_pos=84843181;
Query OK, 0 rows affected (0.05 sec)

6,在从库上执行,开启从库同步

mysql>  start slave;
Query OK, 0 rows affected (0.00 sec)

7、在主库上执行,主库解除锁定

mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)

至此完成了。

猜你喜欢

转载自www.cnblogs.com/liudan182/p/9401244.html