mysql主从切换 日志_Mysql 主从切换

一主一从切换示例

1.修改配置文件

master 配置文件

innodb_flush_log_at_trx_commit=1

sync_binlog=1

read_only=1

slave 配置文件

innodb_flush_log_at_trx_commit=1

sync_binlog=1

#read_only=1

2.查询主,从库的状态

--master

mysql>  show processlist \G

*************************** 1. row ***************************

Id: 9

User: root

Host: localhost

db: test

Command: Sleep

Time: 576

State:

Info: NULL

*************************** 2. row ***************************

Id: 13

User: repl

Host: 192.168.1.106:41457

db: NULL

Command: Binlog Dump

Time: 404

State: Master has sent all binlog to slave; waiting for binlog to be updated

Info: NULL

*************************** 3. row ***************************

Id: 15

User: root

Host: localhost

db: test

Command: Query

Time: 0

State: NULL

Info: show processlist

3 rows in set (0.01 sec)

mysql> show master status \G

*************************** 1. row ***************************

File: mysql-bin.000007

Position: 2940

Binlog_Do_DB:

Binlog_Ignore_DB:

1 row in set (0.00 sec)

--slave

mysql>  show processlist \G

*************************** 1. row ***************************

Id: 1

User: system user

Host:

db: NULL

Command: Connect

Time: 353

State: Waiting for master to send event

Info: NULL

*************************** 2. row ***************************

Id: 2

User: system user

Host:

db: NULL

Command: Connect

Time: 35

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: NULL

Info: show processlist

3 rows 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.107

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000007

Read_Master_Log_Pos: 2940

Relay_Log_File: master-relay-bin.000012

Relay_Log_Pos: 1026

Relay_Master_Log_File: mysql-bin.000007

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: 2940

Relay_Log_Space: 1183

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: 2

1 row in set (0.00 sec)

3.主库置只读状态

--master

mysql> set global read_only=1;

4.再次查看主,从状态,从接收完所有日志后,停止io线程

--slave

mysql> stop slave io_thread;

Query OK, 0 rows affected (0.00 sec)

mysql>  show processlist \G

mysql> show slave status \G

Make sure that the slave have processed any statements in their relay log.

show processlist \G中显示 State: Slave has read all relay log;

5.从库置为主库

--slave

mysql> stop slave;

Query OK, 0 rows affected (0.00 sec)

mysql> reset master;

Query OK, 0 rows affected (0.01 sec)

mysql> reset slave;

Query OK, 0 rows affected (0.01 sec)

mysql> show master status \G

*************************** 1. row ***************************

File: mysql-bin.000001

Position: 107

Binlog_Do_DB:

Binlog_Ignore_DB:

1 row in set (0.00 sec)

mysql> set global read_only=0;

6.主库置为从库

mysql> reset master;

Query OK, 0 rows affected (0.01 sec)

mysql>  CHANGE MASTER TO

->         MASTER_HOST='master',

->         MASTER_USER='repl',

->         MASTER_PASSWORD='repl',

->         MASTER_LOG_FILE='mysql-bin.000001',

->         MASTER_LOG_POS=107;

Query OK, 0 rows affected (0.03 sec)

mysql> start slave;

Query OK, 0 rows affected (0.00 sec)

mysql> show slave status \G

*************************** 1. row ***************************

Slave_IO_State: Waiting for master to send event

Master_Host: master

Master_User: repl

Master_Port: 3306

Connect_Retry: 60

Master_Log_File: mysql-bin.000001

Read_Master_Log_Pos: 107

Relay_Log_File: slave1-relay-bin.000002

Relay_Log_Pos: 253

Relay_Master_Log_File: mysql-bin.000001

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: 107

Relay_Log_Space: 410

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

1 row in set (0.00 sec)

MySQL 主从复制是一种非常重要且常用的数据同步方式,对于数据库管理员来说,保证主从同步的正常运行对于数据库的可靠性和稳定性都是非常重要的。当我们在使用 MySQL 主从同步时,如果发现 slave_io_running:no 的情况,那么意味着 MySQL 从服务器的 IO 线程停止了工作。 IO 线程是 MySQL 主从复制中非常重要的一个组件,它的作用是从主服务器上读取二进制数据,并将其传输到从服务器上。如果 IO 线程出现问题,那么意味着从服务器无法从主服务器获取新的数据,从而导致从服务器无法同步主服务器的数据。 在出现 slave_io_running:no 的情况时,我们需要立即查找 IO 线程出现问题的原因,并进行解决。以下是常见的 IO 线程故障原因及对应解决方案: 1. 网络问题。如果主服务器和从服务器之间的网络出现了问题,那么 IO 线程就无法正常工作。我们可以通过检查网络连接以及网络质量来解决这个问题。 2. DNS 解析错误。如果主服务器和从服务器之间使用的是主机名进行通信,那么可能出现 DNS 解析错误的情况。我们需要检查主机名是否正确,以及 DNS 解析是否正常。 3. 权限问题。如果从服务器的用户权限不正确,那么也可能会导致 IO 线程无法正常工作。我们需要检查从服务器的用户权限是否足够,以及是否正确配置了主从服务器之间的连接信息。 4. 数据库配置问题。如果从服务器的数据库配置不正确,那么也可能会导致 IO 线程无法正常工作。我们需要检查从服务器的数据库配置是否正确,并根据需要进行更改。 总的来说,当出现 slave_io_running:no 的情况时,我们需要立即对问题进行排查和解决,以便尽快恢复 MySQL 主从同步的正常运行。在排查问题过程中,我们需要充分了解 MySQL 主从复制的工作原理,熟悉常见的故障原因,并加强对 MySQL 数据库的日常维护和管理工作,以提高数据库的可靠性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值