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