Table of Contents
mysql数据库主从库配置之主从切换
Home php教程 php手册 mysql数据库主从库配置之主从切换

mysql数据库主从库配置之主从切换

Jun 13, 2016 am 08:39 AM
android

mysql数据库主从库配置之主从切换

MySQL:主从复制(Replication)搭建 介绍了 MySQL 主从配置过程,这篇 介绍手工主从切换过程。

一 环境信息
主库 192.168.1.60/3306 主机名 db1
备库 192.168.1.61/3306 主机名 db2
备注: 主从节点 mysql 安装略,Replication 安装略。


二 主从切换
--主,备节点都要创建 Replication 用户。如果无,参考下面创建。
<p style="margin-top:0px;margin-bottom:10px;">create user 'rep1'@'%' identified by 'rep1abcd1243d'; GRANT REPLICATION SLAVE ON *.* TO 'rep1'@'%'; </p>
Copy after login

--查询从库状态
<p style="margin-top:0px;margin-bottom:10px;">root@localhost:francs>show slave status\G </p>
Copy after login

--查询主库状态
<p style="margin-top:0px;margin-bottom:10px;">root@localhost:francs>show slave hosts; +-----------+-------------------+------+-----------+--------------------------------------+ | Server_id | Host | Port | Master_id | Slave_UUID | +-----------+-------------------+------+-----------+--------------------------------------+ | 2 | 192.168.2.38(db2) | 3306 | 1 | ad397a06-7c56-11e4-b2fb-000c29dcb3b5 | +-----------+-------------------+------+-----------+--------------------------------------+ 1 row in set (0.00 sec) </p>
Copy after login

--从库: 停止 IO_THREAD 线程
<p style="margin-top:0px;margin-bottom:10px;">root@localhost:francs>stop slave IO_THREAD; root@localhost:francs>show slave status\G *************************** 1. row ***************************  Slave_IO_State:   Master_Host: 192.168.2.37  Master_User: rep1  Master_Port: 3306  Connect_Retry: 60  Master_Log_File: bin-log.000001  Read_Master_Log_Pos: 362  Relay_Log_File: db2-relay-bin.000002  Relay_Log_Pos: 523  Relay_Master_Log_File: bin-log.000001  Slave_IO_Running: No  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: 362  Relay_Log_Space: 694  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: 1  Master_UUID: 0c130d47-22bb-11e4-aaaa-000c2986ac80  Master_Info_File: mysql.slave_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) </p>
Copy after login

--激活从库(从库上操作)
<p style="margin-top:0px;margin-bottom:10px;">root@localhost:francs>stop slave; root@localhost:francs>reset master; root@localhost:francs>reset slave all; root@localhost:francs>show binary logs; +----------------+-----------+ | Log_name | File_size | +----------------+-----------+ | bin-log.000001 | 120 | +----------------+-----------+ 1 row in set (0.00 sec) </p>
Copy after login
备注:reset slave all 命令会删除从库的 replication 参数,之后 show slave status\G 的信息返回为空。

--将原来主库变为从库
<p style="margin-top:0px;margin-bottom:10px;">mysql> CHANGE MASTER TO  MASTER_HOST='192.168.1.61',  MASTER_PORT=3306,  MASTER_USER='rep1',  MASTER_PASSWORD='rep1abcd1243d',  MASTER_LOG_FILE='bin-log.000001',  MASTER_LOG_POS=120;   root@localhost:francs>start slave; root@localhost:francs>show slave status\G </p>
Copy after login
备注:这步执行之后,发现 db1 日志文件报了以下错误,提示 db1 连接不上 db2。

--db1 日志报错
<p style="margin-top:0px;margin-bottom:10px;">2015-03-02 14:24:14 26198 [ERROR] Slave I/O: error connecting to master 'rep1@192.168.1.61:3306' - retry-time: 60 retries: 8, Error_code: 1045 </p>
Copy after login

--解决过程
<p style="margin-top:0px;margin-bottom:10px;">--连接测试 [mysql@db1 ~]$ mysql -h 192.168.1.60 -P 3306 -urep1  备注:居然不需要密码能直接能连上。 --测试匿名用户 [mysql@db1 ~]$ mysql -h 192.168.1.60 -P 3306 -urep2  备注:依然不需要密码。 --原因分析 root@localhost:francs>select Host,User,Password from mysql.user where User=''; +-----------+------+----------+ | Host | User | Password | +-----------+------+----------+ | localhost | | | | db1 | | | +-----------+------+----------+ 备注: 原来 db2 节点上存在 User 为空的的两行,表示匿名用户可以连接数据库, 删除这两行,之后 flush privileges; --再连接测试 [mysql@db1 ~]$ mysql -h 192.168.1.60 -P 3306 -urep1  备注:这次连接需要密码了。之后再次观看 db1 同步日志,不再报错。 </p>
Copy after login


三 数据验证
主从切换后db2 为主节点, db1 为备节点,在 db2 节点上插入一条数据测试同步是否正常。

--db2 上执行
<p style="margin-top:0px;margin-bottom:10px;">root@localhost:francs>insert into test_sr(id) values(30); Query OK, 1 row affected (0.03 sec) root@localhost:francs>select * from test_sr order by id desc limit 1; +------+---------------------+ | id | create_time | +------+---------------------+ | 30 | 2015-03-02 15:19:53 | +------+---------------------+ 1 row in set (0.00 sec) </p>
Copy after login

--db1 上验证
<p style="margin-top:0px;margin-bottom:10px;">root@localhost:francs>select * from test_sr order by id desc limit 1; +------+---------------------+ | id | create_time | +------+---------------------+ | 30 | 2015-03-02 15:19:53 | +------+---------------------+ 1 row in set (0.00 sec) </p>
Copy after login
备注:数据同步正常,以上是对 MySQL 主备切换的初次了解,后续再补充。
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:23 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Samsung Galaxy S25 Ultra leaks in first render images with rumoured design changes revealed Sep 11, 2024 am 06:37 AM

OnLeaks has now partnered with Android Headlines to provide a first look at the Galaxy S25 Ultra, a few days after a failed attempt to generate upwards of $4,000 from his X (formerly Twitter) followers. For context, the render images embedded below h

IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size IFA 2024 | TCL\'s NXTPAPER 14 won\'t match the Galaxy Tab S10 Ultra in performance, but it nearly matches it in size Sep 07, 2024 am 06:35 AM

Alongside announcing two new smartphones, TCL has also announced a new Android tablet called the NXTPAPER 14, and its massive screen size is one of its selling points. The NXTPAPER 14 features version 3.0 of TCL's signature brand of matte LCD panels

Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Vivo Y300 Pro packs 6,500 mAh battery in a slim 7.69 mm body Sep 07, 2024 am 06:39 AM

The Vivo Y300 Pro just got fully revealed, and it's one of the slimmest mid-range Android phones with a large battery. To be exact, the smartphone is only 7.69 mm thick but features a 6,500 mAh battery. This is the same capacity as the recently launc

New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades New report delivers damning assessment of rumoured Samsung Galaxy S25, Galaxy S25 Plus and Galaxy S25 Ultra camera upgrades Sep 12, 2024 pm 12:22 PM

In recent days, Ice Universe has been steadily revealing details about the Galaxy S25 Ultra, which is widely believed to be Samsung's next flagship smartphone. Among other things, the leaker claimed that Samsung only plans to bring one camera upgrade

Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Samsung Galaxy S24 FE billed to launch for less than expected in four colours and two memory options Sep 12, 2024 pm 09:21 PM

Samsung has not offered any hints yet about when it will update its Fan Edition (FE) smartphone series. As it stands, the Galaxy S23 FE remains the company's most recent edition, having been presented at the start of October 2023. However, plenty of

Motorola Razr 50s shows itself as possible new budget foldable in early leak Motorola Razr 50s shows itself as possible new budget foldable in early leak Sep 07, 2024 am 09:35 AM

Motorola has released countless devices this year, although only two of them are foldables. For context, while most of the world has received the pair as the Razr 50 and Razr 50 Ultra, Motorola offers them in North America as the Razr 2024 and Razr 2

Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Xiaomi Redmi Note 14 Pro Plus arrives as first Qualcomm Snapdragon 7s Gen 3 smartphone with Light Hunter 800 camera Sep 27, 2024 am 06:23 AM

The Redmi Note 14 Pro Plus is now official as a direct successor to last year'sRedmi Note 13 Pro Plus(curr. $375 on Amazon). As expected, the Redmi Note 14 Pro Plus heads up the Redmi Note 14 series alongside theRedmi Note 14and Redmi Note 14 Pro. Li

See all articles