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数据库主从服务器文档的部署及主从文档切换的知识就介绍到这里了,希望本次的介绍能够对您有所收获。
【编辑推荐】