Database
Mysql Tutorial
Detailed explanation of MySQL's replication two-way backup configuration
Detailed explanation of MySQL's replication two-way backup configuration
MySQL的replication双向备份配置,分享如下。
一、 设置A服务器
编辑/usr/local/mysql/etc/my.cnf文件,在mysqld块里添加如下内容:
log-bin=/jongo/mysql/logs/mysql-bin server-id = 2 binlog-do-db=bbscs8 这里的binlog-do-db是用来设置可以被slave服务器同步的数据 库名称 binlog-do-db=bdt binlog-do-db=adsense binlog-do-db=bdt_bbs binlog-do-db=bdt_help
master-host = 192.168.2. 备份192.168.2.7上的数据库,即这台做为备机的需要备份的数据库的主机器地址
master-user = backup 上面机器上设置的,用来同步的用户名胜古迹
master-password = 17buybackup@#$ 要用主服务器上同步过来的数据库的名称
log-slave-updates
grant replication slave on *.* to 'backup'@'192.168.2.%' identified by 'bdtbackup@#$';
给同步添加一个用户
二、 设置B服务器
server-id = 1
binlog-do-db= jongo17buy 这里的binlog-do-db是用来设置可以被slave服务器同步的数据
库名称
master-host = 192.168.2.5 备份192.168.2.7上的数据库,即这台做为备机的需要备份的数据库的主机器地址
master-user = backup 上面机器上设置的,用来同步的用户名胜古迹
master-password = bdtbackup@#$ 要用主服务器上同步过来的数据库的名称
grant replication slave on *.* to 'backup'@'192.168.2.%' identified by '17buybackup@#$';
三、 两个服务器上
在两个服务器上,将要同步的数据库分别在各的备机手工同步或重新导入一下。
然后,分别启动 mysql> start slave
四、 同步监控脚本
mysql_monitor.sh文件
#!/bin/bash
. /root/.bash_profile
. /jongo/shell/contact_config.sh
MYSQL_PASSWD="admin8*"
IO_RUNING=`echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD |grep "Slave_IO_Running" |awk -F ":" '{ print $2 }'`
SQL_RUNING=`echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD |grep "Slave_SQL_Running" |awk -F ":" '{ print $2 }'`
SQL_ERROR=`echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD |grep "Last_SQL_Error" |awk -F ":" '{ print $2 }'`
echo "io_status=\"$IO_RUNING\"" >/jongo/shell/tmp.txt
echo "sql_status=\"$SQL_RUNING\"">>/jongo/shell/tmp.txt
echo "sql_error=\"$SQL_ERROR\"">>/jongo/shell/tmp.txt
. /jongo/shell/tmp.txt
if [ "$io_status" != " Yes" -o "$sql_status" != " Yes" ]
then
echo "17buy的mysql数据库的slave(203.156.192.29) 于"`date "+%Y-%m-%d %H:%m:%S"`"发生错误:">/jongo/shell/mysql_monitor.log
echo "请立即检查,谢谢!" >>/jongo/shell/mysql_monitor.log
echo "slave 服务器的状态如下:">>/jongo/shell/mysql_monitor.log
echo "show slave status \G;" |mysql -uroot -p$MYSQL_PASSWD >>/jongo/shell/mysql_monitor.log
for email in $contact_email
do
mail -s "mysql slave on 17buy was false" $email /shell/mysql_monitor.log
done
fi
contact_config.sh文件
contact_email=wayne.wang@jongogroup.com五、 常见问题
a. 出现Error initializing relay log position: Could not find target log during relay log initialization,Error_code: 1593错误
~ 这个一般是由于在mysql数据库的数据目录下找不到localhost-relay-bin.***文件导致的,可以用类似下列语句来调整:
CHANGE MASTER TO
-> MASTER_HOST='192.168.2.5’,
-> MASTER_USER='backup',
-> MASTER_PASSWORD='./bdtbackup@#$',
-> MASTER_PORT=3306,
-> MASTER_LOG_FILE='master2-bin.001',
-> MASTER_LOG_POS=4,
-> MASTER_CONNECT_RETRY=10,CHANGE MASTER TO
-> RELAY_LOG_FILE='./slave-relay-bin.006',
-> RELAY_LOG_POS=4025;注意:
这里指定的文件路径是绝对路径,如果要用相对路径一定要用./filename形式,否报找不到对应的文件错误;
b. 出现Slave I/O: error connecting to master 'backup@192.168.2.5:3306' - retry-time: 60 retries: 86400, Error_code: 1045 错误:
出现这个错误可能有:
1) 用户的权限不对;
2) 主服务器不上连接,如防火墙没有打开相应的端口;
3) 从服务器上的RELAY_LOG_POS的值不对,可以用上面的CHANGE MASTER TO语句来调整
c. 同步出错处理
当服务器同步出错了,可以有以下两种方法来处理它:
(a) 如果只是数据更新或者插入错误,而且对将来的影响不是很大,这个数据可以不要时,可以:
stop slave; SET GLOBAL SQL_SLAVE_SKIP_COUNTER=1:
将这条出错的查询跳掉就可以了,然后:
start slave; 重启同步过程就可以了
(b) 另外的方法是 :当服务器出现严重的问题时可以:
stop slave
然后用其他软件手动同步一下,然后再主机上:
show master status;
查到master_log_file和master_log_pos,然后再从机器上:
change master to master_log_file='mysql-bin.000007',master_log_pos=261836644 ; start slave
就有可以同步了
The above is the detailed content of Detailed explanation of MySQL's replication two-way backup configuration. For more information, please follow other related articles on the PHP Chinese website!
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
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
MySQL: Simple Concepts for Easy Learning
Apr 10, 2025 am 09:29 AM
MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.
How to open phpmyadmin
Apr 10, 2025 pm 10:51 PM
You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".
How to create navicat premium
Apr 09, 2025 am 07:09 AM
Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.
How to create a new connection to mysql in navicat
Apr 09, 2025 am 07:21 AM
You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.
MySQL and SQL: Essential Skills for Developers
Apr 10, 2025 am 09:30 AM
MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.
How to use single threaded redis
Apr 10, 2025 pm 07:12 PM
Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.
How to recover data after SQL deletes rows
Apr 09, 2025 pm 12:21 PM
Recovering deleted rows directly from the database is usually impossible unless there is a backup or transaction rollback mechanism. Key point: Transaction rollback: Execute ROLLBACK before the transaction is committed to recover data. Backup: Regular backup of the database can be used to quickly restore data. Database snapshot: You can create a read-only copy of the database and restore the data after the data is deleted accidentally. Use DELETE statement with caution: Check the conditions carefully to avoid accidentally deleting data. Use the WHERE clause: explicitly specify the data to be deleted. Use the test environment: Test before performing a DELETE operation.
MySQL: An Introduction to the World's Most Popular Database
Apr 12, 2025 am 12:18 AM
MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.


