search
HomeDatabaseMysql TutorialDG之主库、备库切换(物理备库)

DG之主库、备库切换 一.开库与关库顺序 开库顺序 先启备库,再启主库(启动监听、打开告警日志) 关库顺序 先关主库,再关备库 二.主备库切换 1、操作过程一览 步骤1:启动备库、监听、告警; 步骤2:启动主库、监听、告警; 步骤3:主库(bj)查询信息 SQ

DG之主库、备库切换

一.开库与关库顺序

开库顺序

先启备库,再启主库(启动监听、打开告警日志)

关库顺序

先关主库,再关备库

二.主备库切换

1、操作过程一览

步骤1:启动备库、监听、告警;

步骤2:启动主库、监听、告警;

步骤3:主库(bj)查询信息

SQL>selectname,dbid,database_role,protection_mode from v$database;

\

步骤4:备库(sh)上做recover

SQL>recovermanaged standby database disconnect from session;

步骤5:备库上插入数据

SQL>insert into scott.emp1 select * from scott.emp;

报错

\

虽然备库处于open状态,但它是一个read-only状态。

步骤6:查看主库(bj)、备库(sh)日志是否一致

SQL>selectmax(sequence#) from v$archived_log where name is not null;

或SQL>selectmax(sequence#) from v$archived_log;

举例:

主库:

\

 

备库:

\

上图表明已经归档的最大序列号均为34,说明日志是同步的,可以进行主备库切换。

步骤7:主库(bj)下检查是否可以切换

SQL>selectname,database_role,protection_mode,switchover_status from v$database;

switchover_status显示“TO STANDBY”表示允许做切换。

\

步骤8:备库(sh)下检查是否可以切换

SQL>selectname,database_role,protection_mode,switchover_status from v$database;

\

说明:备库是不允许主动去切成主库的,NOT ALLOWED状态说明正常。

步骤9:检查主库(bj)上的会话

SQL>select username,sid from v$session where username is notnull;

\

查看是否仍有用户使用数据库,若仍有人使用,是不允许切换的,切换前要通知确保无用户使用。

步骤10:主库(bj)变为物理备库(sh)

若没有会话,将主库(bj)变为物理备库(sh)

SQL> ALTER DATABASECOMMIT TO SWITCHOVER TO PHYSICAL STANDBY;

若有会话的话,关闭会话,将主库(bj)变为物理备库(sh)

SQL> alter database commit to switchover to physical standby withsession shutdown;

步骤11:关闭、重启之前的主库(bj)到mount状态

SQL> SHUTDOWN IMMEDIATE;

SQL> STARTUP MOUNT;

SQL> selectswitchover_status from v$database;

SWITCHOVER_STATUS

--------------------

TO PRIMARY

注:在这个时候,两个数据库都应该处于备库状态。

步骤12:查证备库的转换状态

SQL> SELECTSWITCHOVER_STATUS FROM V$DATABASE;

SWITCHOVER_STATUS

-----------------

TO_PRIMARY ——可以转换为主库,说明是备库状态

步骤13:将目标物理备库(sh)转换为主库

SQL> ALTER DATABASECOMMIT TO SWITCHOVER TO PRIMARY(WITHSESSION SHUTDOWN);

步骤14:完成备库(sh)到主库转换,物理备库启动到open

若物理备库(bj)在最近一次启动时不是处于只读模式 opened in read-only mode,直接将数据库打开,然后下一步。

SQL> select status fromv$instance;

STATUS

------------

MOUNTED

SQL> ALTER DATABASE OPEN;

SQL> select switchover_statusfrom v$database;

SWITCHOVER_STATUS

--------------------

TO STANDBY

若物理备库(bj)在最近一次启动时是只读模式,则先将数据库正常关闭再起库。

SQL> SHUTDOWN IMMEDIATE;

SQL> STARTUP;

步骤15:在新备库(bj)上重新开启日志应用

SQL> ALTER DATABASERECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;

步骤16:在新的主库(sh)上切换日志,开始传输数据给备库

SQL> ALTER SYSTEM SWITCHLOGFILE;

SQL> /

SQL> select max(sequence#)from v$archived_log;

MAX(SEQUENCE#)

--------------

56

SQL> col name for a50;

SQL> select NAME ,SEQUENCE# from v$archived_log; 查看归档日志

步骤17:在备库(bj)上查看归档,两值相等说明备库能收到日志

SQL> select max(sequence#)from v$archived_log;

MAX(SEQUENCE#)

--------------

56

SQL> col name for a50;

SQL> select NAME ,SEQUENCE# from v$archived_log; 查看归档日志

2、总结主备库切换重点

 

主库

备库

查询能否切换(应该显示允许)

 

 

备库查询能否切换(应该显示不允许)

切换为物理备库

 

切换到mount状态

 

 

查看备库转换状态

 

转换为主库

 

切换到mount状态

启动到open

 

 

 

 

启动到open状态

开启日志应用

 

 

切换日志

 

查看归档序列号

查看归档序列号

 

查询状态:备库

查询状态:主库

***********************************************声明************************************************

原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/huangyanlong)。

表述有错误之处,请您留言,不胜感激。

提醒:点击目录,更有助于您的查看。

*****************************************************************************************************

Statement
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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.