MySQL数据库主从服务器文档的部署与切换详解

数据库 MySQL
本文我们首先介绍了MySQL Replication主从服务器文档部署设置的全过程,然后介绍了主从文档切换的方法,希望能够对您有所帮助。

MySQL数据库中如何实现主从服务器文档的部署呢?主从服务器之间怎样进行文档切换呢?本文我们主要就介绍了这一部分的内容,接下来我们就开始介绍。

一、部署文档

1.确保在主服务器和从服务器上安装的MySQL版本一致.

2.在主服务器上为从服务器设置一个连接账户

mysql GRANT REPLICATION SLAVE, SUPER, RELOAD ON *.* TO 'username'@10.1.1.4' IDENTIFIED BY 'use。

主服务器IP: 10.1.1.3

从服务器IP: 10.1.1.4

1.确保在主服务器和从服务器上安装的MySQL版本一致.

2.在主服务器上为从服务器设置一个连接账户

 

mysql> GRANT REPLICATION SLAVE, SUPER, RELOAD ON *.* TO IDENTIF  
 
IED BY 'userpassword'; 
  • 1.
  • 2.
  • 3.

 

3. 执行FLUSH TABLES WITH READ LOCK 进行锁表

 

mysql> FLUSH TABLES WITH READ LOCK; 
  • 1.

 

4. 让客户程序保持运行,发出FLUSH TABLES语句让读锁定保持有效。(如果退出客户程序,锁被释放)。进入主服务器的数据目录,然后执行命令:

 

shell> tar -cvf /tmp/mysql-snapshot.tar .   
 
shell> tar -xvf /tmp/mysql-snapshot.tar 
  • 1.
  • 2.
  • 3.

 

读取主服务器上当前的二进制日志名(File)和偏移量值(Position),并记录下来:

mysql > SHOW MASTER STATUS; | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | | mysql-bin.003 | 73 | test | manual,mysql | 取得快照并记录日志名和偏移量后,可以在主服务器上重新启用写活动:

mysql> UNLOCK TABLES;

5. 确保主服务器主机上my.cnf文件的[mysqld]部分包括一个log_bin选项

 

[mysqld]  
 
Log_bin=mysql-bin  
 
server-id=1 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

 

6. 停止用于从服务器的服务器并在其my.cnf文件中添加下面的行:

 

[mysqld]  
 
server-id=2 
  • 1.
  • 2.
  • 3.

 

7.如果对主服务器的数据进行二进制备份,启动从服务器之前将它复制到从服务器的数据目录中。

确保对这些文件和目录的权限正确。服务器 MySQL运行的用户必须能够读写文件,如同在主服务器上一样。

 

8. 用--skip-slave-start选项启动从服务器,以便它不立即尝试连接主服务器。

9. 在从服务器上执行下面的语句:

 

mysql> CHANGE MASTER TO MASTER_HOST='10.1.1.3',MASTER_USER='username',MASTER_PASSWORD='userpassword',  
 
MASTER_LOG_FILE='recorded_log_file_name',MASTER_LOG_POS=recorded_log_position
  • 1.
  • 2.
  • 3.

 

9. 启动从服务器线程:

mysql> START SLAVE;

10.验证部署是否成功

 

mysql> show slave status\G  
 
*************************** 1. row ***************************  
 
Slave_IO_State: Waiting for master to send event  
 
Master_Host: 10.1.1.3  
 
Master_User: rep_slave  
 
Master_Port: 3306  
 
Connect_Retry: 60  
 
Master_Log_File: mysql-bin.000058  
 
Read_Master_Log_Pos: 27324573  
 
Relay_Log_File: cacti-11-111-relay-bin.000008  
 
Relay_Log_Pos: 27324718  
 
Relay_Master_Log_File: mysql-bin.000058  
 
Slave_IO_Running: Yes  
 
Slave_SQL_Running: Yes  
 
Replicate_Do_DB:  
 
Replicate_Ignore_DB: mysql  
 
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: 27324573  
 
Relay_Log_Space: 27325025  
 
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  
 
1 row in set (0.00 sec) 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.

当Slave_IO_Running和Slave_SQL_Running都显示Yes的时候,表示同步成功。

二、切换文档

1. 确保从服务器已经处理了中继日志中的所有语句。 mysql> STOP SLAVE IO_THREAD。

2.然后检查SHOW PROCESSLIST语句的输出,直到你看到Has read all relay log。

3.当从服务器都执行完这些,它们可以被重新配置为一个新的设置。

4.在被提升为主服务器的从服务器上,发出 STOP SLAVE和RESET MASTER和RESET SLAVE操作。

5. 然后重启mysql服务。

6.在主服务器上RESET MASTER。然后CHANGE MASTER TO MASTER_HOST='10.1.1.4',MASTER_USER='rep_slave',MASTER_PASSWORD='userpassword';切换完成。

关于MySQL数据库主从服务器文档的部署及主从文档切换的知识就介绍到这里了,希望本次的介绍能够对您有所收获。

【编辑推荐】

  1. 关于Oracle数据库闪回个性的详细介绍
  2. 批量转换MySQL数据库表的存储引擎的方法
  3. MySQL数据库集群实现负载均衡的安装配置详解
  4. 一个mysql数据库配置问题导致登录失败的解决方案
  5. MySQL数据库打开文件太多导致mysqldump出错的解决
责任编辑:赵鹏 来源: 火魔网
相关推荐

2011-03-30 10:15:14

Mysql数据库服务器

2018-08-02 10:14:49

服务器数据库主从同步

2011-04-07 15:17:40

MySQL数据库服务器

2017-01-17 15:14:49

MySQL数据库自动化

2011-10-24 07:31:37

数据库服务器优化

2011-05-12 09:51:26

2011-07-28 14:49:40

2010-06-10 17:05:28

2011-08-24 10:15:55

Oracle数据库服务器进程

2010-06-12 09:46:05

MySQL数据库

2011-04-14 11:09:14

MySQL数据库

2012-02-16 11:00:12

Exadata数据库云服务器Oracle

2012-07-23 16:27:25

Oracle

2010-06-01 14:58:03

2010-07-06 13:22:13

SQL Server

2010-10-11 17:41:11

MySql服务器

2023-11-30 07:15:57

MySQL数据库

2011-03-31 17:02:19

MySQL数据库远程连接

2009-11-16 13:24:34

Oracle数据库服务

2010-10-11 17:23:47

mysql建主从服务器
点赞
收藏

51CTO技术栈公众号