标签:mysql主从复制 mysql双机热备 mysql双机热备原理
1、mysq主从复制简介
1.1、复制介绍
MySQL支持单向、异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。如果你想要设置链式复制服务器,从服务器本身也可以充当主服务器。请注意当你进行复制时,所有对复制中的表的更新必须在主服务器上进行。否则,你必须要小心,以避免用户对主服务器上的表进行的更新与对从服务器上的表所进行的更新之间的冲突。
单向复制有利于健壮性、速度和系统管理:
· 主服务器/从服务器设置增加了健壮性。主服务器出现问题时,你可以切换到从服务器作为备份。
· 通过在主服务器和从服务器之间切分处理客户查询的负荷,可以得到更好的客户响应时间。SELECT查
询可以发送到从服务器以降低主服务器的查询处理负荷。但修改数据的语句仍然应发送到主服务器,以便主服务器和从服务器保持同步。如果非更新查询为主,该负载均衡策略很有效,但一般是更新查询。
· 使用复制的另一个好处是可以使用一个从服务器执行备份,而不会干扰主服务器。在备份过程中主服务器可以继续处理更新。
1.2、mysql主从复制概述
MySQL复制基于主服务器在二进制日志中跟踪所有对数据库的更改(更新、删除等等)。因此,要进行复制,必须在主服务器上启用二进制日志。每个从服务器从主服务器接收主服务器已经记录到其二进制日志的保存的更新,以便从服务器可以对其数据拷贝执行相同的更新。认识到二进制日志只是一个从启用二进制日志的固定时间点开始的记录非常重要。任何设置的从服务器需要主服务器上的在主服务器上启用二进制日志时的数据库拷贝。如果启动从服务器时,其数据库与主服务器上的启动二进制日志时的状态不相同,从服务器很可能失败。从服务器设置为复制主服务器的数据后,它连接主服务器并等待更新过程。如果主服务器失败,或者从服务器失去与主服务器之间的连接,从服务器保持定期尝试连接,直到它能够继续帧听更新。由--master-connect-retry选项控制重试间隔。默认为60秒。每个从服务器跟踪复制时间。主服务器不知道有多少个从服务器或在某一时刻有哪些被更新了。
1.3、mysql主从备份原理
1.3.1、MySQL从服务器发出START SLAVE时,从服务器创建一个I/O线程,以主服务器授权用户信息验证连接主服务器
1.3.2、MySQL主服务器验证从MySQL服务器发送过来的验证信息是否合法,如果合法就创建Binlog Dump线程并读取MySQL主服务器的bin-log二进制日志文件(有几个从服务器连接MySQL主服务器就在主服务器上创建几个IO线程)。
1.3.3、MySQL主服务器线程将二进制日志中的内容发送到从服务器。
1.3.4、从服务器I/O线程读取主服务器Binlog Dump线程发送的内容并将该数据拷贝到从服务器数据目录中的本地文件中,即中继日志,然后再去接受监听接受mysql主服务器发送的bin-log日志。
1.3.5、第3个线程是从服务器SQL线程,它用于读取中继日志中的信息并把中继日志中信息同步到从库中。
2、mysql主从复制步骤
2.1、确保在主服务器和从服务器上安装的MySQL版本兼容。理想情况,应在主服务器和从服务器上使用最近版本的MySQL。
2.2、在主服务器上为服务器设置一个连接账户。该账户必须授予REPLICATION SLAVE权限。如果账户仅用于
复制(推荐这样做),则不需要再授予任何其它权限。
假定你的IP地址为192.168.1.2,想要创建用户名为system的一个账户,从服务器可以使用该账户从你的IP为192.168.1.2的主机使用密码123456来访问主服务器。要创建该账户,可使用GRANT语句:
mysql> grant replication slave on *.* to ‘system‘@‘192.168.1.2‘ identified by ‘123456‘;
2.3、执行FLUSH TABLES WITH READ LOCK语句清空所有表和块写入语句:
mysql> FLUSH TABLES WITH READ LOCK;
当FLUSH TABLES WITH READ LOCK所置读锁定有效时,读取主服务器上当前的二进制日志名和偏移量值:
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
File列显示日志名,而Position显示偏移量。在该例子中,二进制日志值为mysql-bin.000002,偏移量为120。记录该值。以后设置从服务器时需要使用这些值。它们表示复制坐标,从服务器应从该点开始从主服务器上进行新
的更新。
取得快照并记录日志名和偏移量后,可以在主服务器上重新启用写活动:
mysql> UNLOCK TABLES;
2.4、停止mysql主服务器和从服务器:
[root@mysqldb1 3306]# mysqladmin -uroot -p123456 -S /mysqldata/3306/mysql.sock shutdown
[root@mysqldb1 3306]# mysqladmin -uroot -p123456 -S /mysqldata/3307/mysql.sock shutdown
2.5、确保主服务器主机上my.cnf文件的[mysqld]部分包括一个log-bin选项。该部分还应有一个server-
id=Master_id选项,其中master_id必须为1到2 32 –1之间的一个正整数值。例如:
[root@mysqldb1 3306]# vim /mysqldata/3306/my.cnf
[mysqld]
server-id = 1
log-bin=mysql-bin
如果没有提供那些选项,应添加它们并重启服务器。
2.6、停止用于从服务器的服务器并在其my.cnf文件中添加下面的行:
[mysqld]
server-id=slave_id
slave_id值同Master_id值一样,必须为1到2 32 –1之间的一个正整数值。并且,从服务器的ID必须与主服务器
的ID不相同。例如:
[mysqld]
server-id=2
如果设置多个从服务器,每个从服务器必须有一个唯一的server-id值,必须与主服务器的以及其它从服务器的不相同。可以认为server-id值类似于IP地址:这些ID值能唯一识别复制服务器群集中的每个服务器实例。如果不指定一个server-id值,如果没有定义master-host,则将它设置为1;否则设置为2。请注意如果server-id太长,主服务器拒绝所有来自从服务器的连接,并且从服务器拒绝连接到主服务器。这样,省略server-id只适合用二进制日志备份。
2.7、如果对主服务器的数据进行二进制备份,启动从服务器之前将它复制到从服务器的数据目录中。确保对这
些文件和目录的权限正确。服务器 MySQL运行的用户必须能够读写文件,如同在主服务器上一样。
如果使用mysqldum备份,先启动从服务器
[root@mysqldb1 3306]# /mysqldata/3306/mysqld start
Starting MySQL...
[root@mysqldb1 3307]# mysqldump -uroot -p123456 -S /mysqldata/3306/mysql.sock --all-databases > bak.sql
Warning: Using a password on the command line interface can be insecure.
2.8、启动mysql从服务器。如果前面已经复制了,用--skip-slave-start选项启动从服务器,以便它不立即尝试连接主服
务器。你也可能想要用--logs-warnings选项启动从服务器(默认设置启用),以便在错误日志中显示更多的问题
相关的信息(例如,网络或连接问题)。放弃的连接将记入错误日志,除非其值大于1。
如果使用mysqldump备份主服务器的数据,将转储文件装载到从服务器:
[root@mysqldb1 3306]# /mysqldata/3307/mysqld start
Starting MySQL...
[root@mysqldb1 3306]# netstat -tulnp | grep 330
tcp 0 0 :::3307 :::* LISTEN 29509/mysqld
tcp 0 0 :::3306 :::* LISTEN 28825/mysqld
[root@mysqldb1 3307]# mysql -uroot -p123456 -S /mysqldata/3307/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> source bak.sql
…… ……
mysql> STOP SLAVE;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> change master to
-> master_host=‘192.168.1.2‘,
-> master_port=3306,
-> master_user=‘system‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql-bin.000002‘,
-> master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.12 sec)
mysql> START SLAVE;
Query OK, 0 rows affected (0.13 sec)
mysql> quit
Bye
执行这些程序后,从服务器应连接主服务器,并补充自从快照以来发生的任何更新。
如果你忘记设置主服务器的server-id值,从服务器不能连接主服务器。
如果你忘记设置从服务器的server-id值,在从服务器的错误日志中会出现下面的错误:
Warning: You should set server-id to a non-0 value if master_host is set;
we will force server id to 2, but this MySQL server will not act as a slave.
如果由于其它原因不能复制,从服务器的错误日志中也会出现错误消息。从服务器复制时,会在其数据目录中发现文件dmaster.info和relay-log.info。从服务器使用这两个文件跟踪已经处理了多少主服务器的二进制日志。不要移除或编辑这些文件,除非你确切知你正在做什么并完全理解其意义。即使这样,最好是使用CHANGE MASTER TO语句。
注释:master.info的内容会覆盖命令行或在my.cnf中指定的部分选项。
3、数据库同步实例演示
3.1、在主服务器的数据库中添加mytest库,并在mytest库中插入user表,表的字段为:id,name,age其中id为自增主键,name为变长字符串,age为整数类型,并在user表中添加数据用以测试。
[root@mysqldb1 3306]# mysql -uroot -p123456 -S /mysqldata/3306/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.02 sec)
mysql> create database mytest;
Query OK, 1 row affected (0.00 sec)
mysql> use mytest;
Database changed
mysql> create table user(
-> id int primary key auto_increment,
-> name varchar(12) not null,
-> age int not null);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into user values(null,‘zhangsan‘,45);
Query OK, 1 row affected (0.01 sec)
mysql> select * from user;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | zhangsan | 45 |
+----+----------+-----+
1 row in set (0.00 sec)
mysql> SHOW SLAVE HOSTS; ###在主服务器查看从服务的主机信息###
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 2 | | 3307 | 1 | 22b20031-75f7-11e5-8ec2-000c299a977e |
+-----------+------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)
mysql> quit;
3.2、查看从服务器的状态信息
[root@mysqldb1 3306]# mysql -uroot -p123456 -S /mysqldata/3307/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mytest;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+------------------+
| Tables_in_mytest |
+------------------+
| user |
+------------------+
1 row in set (0.00 sec)
mysql> select * from user;
+----+----------+-----+
| id | name | age |
+----+----------+-----+
| 1 | zhangsan | 45 |
+----+----------+-----+
1 row in set (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: system
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 663
Relay_Log_File: mysqldb1-relay-bin.000002
Relay_Log_Pos: 826
Relay_Master_Log_File: mysql-bin.000002
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: 663
Relay_Log_Space: 1002
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: 19b72916-75f7-11e5-8ec2-000c299a977e
Master_Info_File: /mysqldata/3307/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)
ERROR:
No query specified
mysql>
4、安装以上方法配置端口为3308的从服务器。
4.1、编辑3308的配置文件my.cnf
[mysqld]
server-id=3
4.2、锁表查看主服务器上当前的二进制日志名和偏移量值
[root@mysqldb1 3307]# mysql -uroot -p123456 -S /mysqldata/3306/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 663 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.06 sec)
mysql> quit
Bye
4.3、备份主服务器数据信息
[root@mysqldb1 3307]# mysqldump -uroot -p123456 -S /mysqldata/3306/mysql.sock --all-databases > 3308bak.sql
Warning: Using a password on the command line interface can be insecure.
启动端口为3308的mysql实例
[root@mysqldb1 3307]# /mysqldata/3308/mysqld start
Starting MySQL...
[root@mysqldb1 3307]# netstat -tulnp | grep 330
tcp 0 0 :::3307 :::* LISTEN 29509/mysqld
tcp 0 0 :::3308 :::* LISTEN 31286/mysqld
tcp 0 0 :::3306 :::* LISTEN 28825/mysqld
4.4、将主服务器的数据导入到备份服务器
[root@mysqldb1 3307]# mysqldump -uroot -p123456 -S /mysqldata/3306/mysql.sock --all-databases > 3308bak.sql
Warning: Using a password on the command line interface can be insecure.
[root@mysqldb1 3307]# mysql -uroot -p123456 -S /mysqldata/3308/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> source 3308bak.sql
Query OK, 0 rows affected (0.07 sec)
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
…… …… ……
mysql> change master to
-> master_host=‘192.168.1.2‘,
-> master_port=3306,
-> master_user=‘system‘,
-> master_password=‘123456‘,
-> master_log_file=‘mysql-bin.000002‘,
-> master_log_pos=663;
Query OK, 0 rows affected, 2 warnings (0.03 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.1.2
Master_User: system
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 663
Relay_Log_File: mysqldb1-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000002
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: 663
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: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /mysqldata/3308/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
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)
ERROR:
No query specified
mysql> START SLAVE;
Query OK, 0 rows affected (0.06 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.2
Master_User: system
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 663
Relay_Log_File: mysqldb1-relay-bin.000002
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000002
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: 663
Relay_Log_Space: 459
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: 19b72916-75f7-11e5-8ec2-000c299a977e
Master_Info_File: /mysqldata/3308/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)
ERROR:
No query specified
mysql> quit
Bye
4.5、测试实端口为3308的mysql实例
[root@mysqldb1 ~]# mysql -uroot -p123456 -S /mysqldata/3306/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytest |
| performance_schema |
+--------------------+
4 rows in set (0.09 sec)
mysql> create database liangge;
Query OK, 1 row affected (0.00 sec)
mysql> use liangge;
Database changed
mysql> create table stuinfo(id int primary key auto_increment,
-> name varchar(12),
-> address varchar(50));
Query OK, 0 rows affected (0.09 sec)
mysql> insert into stuinfo values(null,‘liangge‘,‘myaddress‘);
Query OK, 1 row affected (0.14 sec)
mysql> quit
Bye
4.6、测试端口为3308的mysql实例备份服务器
[root@mysqldb1 3307]# mysql -uroot -p123456 -S /mysqldata/3308/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| liangge |
| mysql |
| mytest |
| performance_schema |
| test |
+--------------------+
6 rows in set (0.00 sec)
mysql> use liangge;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_liangge |
+-------------------+
| stuinfo |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from stuinfo;
+----+---------+-----------+
| id | name | address |
+----+---------+-----------+
| 1 | liangge | myaddress |
+----+---------+-----------+
1 row in set (0.00 sec)
mysql> quit
Bye
4.7、再测试一下端口为3307的mysql实例备份服务器
[root@mysqldb1 ~]# mysql -uroot -p123456 -S /mysqldata/3307/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| liangge |
| mysql |
| mytest |
| performance_schema |
+--------------------+
5 rows in set (0.10 sec)
mysql> use liangge;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_liangge |
+-------------------+
| stuinfo |
+-------------------+
1 row in set (0.00 sec)
mysql> select * from stuinfo ;
+----+---------+-----------+
| id | name | address |
+----+---------+-----------+
| 1 | liangge | myaddress |
+----+---------+-----------+
1 row in set (0.00 sec)
mysql> quit
Bye
4.7、在主服务器上查看备份主机的相关信息
[root@mysqldb1 3306]# mysql -uroot -p123456 -S /mysqldata/3306/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> show slave hosts;
+-----------+------+------+-----------+--------------------------------------+
| Server_id | Host | Port | Master_id | Slave_UUID |
+-----------+------+------+-----------+--------------------------------------+
| 3 | | 3308 | 1 | c16d2d40-7746-11e5-8dd2-000c299a977e |
| 2 | | 3307 | 1 | 22b20031-75f7-11e5-8ec2-000c299a977e |
+-----------+------+------+-----------+--------------------------------------+
2 rows in set (0.00 sec)
mysql> quit
Bye
4.8、多实例主服务器及从服务器线程查看
4.8.1、端口号为3306实例的MySQL数据库主服务器线程查看
[root@mysqldb1 3306]# mysql -uroot -p123456 -S /mysqldata/3306/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> SHOW PROCESSLIST\G;
*************************** 1. row ***************************
Id: 1
User: system
Host: 192.168.1.2:34590
db: NULL
Command: Binlog Dump
Time: 2423
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system
Host: 192.168.1.2:34591
db: NULL
Command: Binlog Dump
Time: 2420
State: Master has sent all binlog to slave; waiting for binlog to be updated
Info: NULL
*************************** 3. row ***************************
Id: 3
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: SHOW PROCESSLIST
3 rows in set (0.00 sec)
ERROR:
No query specified
mysql> quit
Bye
注:其中线程1和线程2是主服务器IO线程连接从服务器的复制线程,是主服务器IO线程通过Binlog Dump命令向从服务器IO线程发送binlog二进制日志文件使用的线程,此线程除了向从服务器发送二进制线程之外还会监听mysql主服务器binlog的改变。线程3是是由show processlist命令语句执行的所产生的线程,没多大意义。
State: Master has sent all binlog to slave; waiting for binlog to be updated
该信息表示所有主要更新已经被发送到从服务器,主服务器正等待更多的更新出现。
4.8.2、端口号为3307实例的MySQL数据库从服务器线程查看
[root@mysqldb1 3307]# mysql -uroot -p123456 -S /mysqldata/3307/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> SHOW PROCESSLIST\G;
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 3044
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 4090
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 4
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: SHOW PROCESSLIST
3 rows in set (0.00 sec)
ERROR:
No query specified
mysql> quit
Bye
注:该信息表示线程1是同主服务器通信的I/O线程,线程2是处理保存在中继日志中的更新的SQL线程。SHOW PROCESSLIST运行时,两个线程均空闲,等待其它更新。
State: Waiting for master to send event
说明从服务器IO线程处于等待监听空闲状态,正等待主服务器向其发送二进制日志文件呢。
State: Slave has read all relay log; waiting for the slave I/O thread to update it
说明从服务器SQL处理线程已经读完所有中继日志(relay-log)也属于空闲状态,正等待从服务器的IO线程更新中继日志。
Time列的值显示从服务器比主服务器滞后多长时间。
4.8.3、端口号为3308实例的MySQL数据库从服务器线程查看
[root@mysqldb1 3308]# mysql -uroot -p123456 -S /mysqldata/3308/mysql.sock
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.27-log Source distribution
Copyright (c) 2000, 2015, 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> SHOW PROCESSLIST\G;
*************************** 1. row ***************************
Id: 1
User: system user
Host:
db: NULL
Command: Connect
Time: 3003
State: Waiting for master to send event
Info: NULL
*************************** 2. row ***************************
Id: 2
User: system user
Host:
db: NULL
Command: Connect
Time: 3003
State: Slave has read all relay log; waiting for the slave I/O thread to update it
Info: NULL
*************************** 3. row ***************************
Id: 3
User: root
Host: localhost
db: NULL
Command: Query
Time: 0
State: init
Info: SHOW PROCESSLIST
3 rows in set (0.00 sec)
ERROR:
No query specified
mysql> quit
Bye
同上4.8.2
本文出自 “放牛娃” 博客,请务必保留此出处http://fangniuwa.blog.51cto.com/10209030/1759371
标签:mysql主从复制 mysql双机热备 mysql双机热备原理
原文地址:http://fangniuwa.blog.51cto.com/10209030/1759371