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)。
表述有错误之处,请您留言,不胜感激。
提醒:点击目录,更有助于您的查看。
*****************************************************************************************************

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.