


Detailed explanation of MySQL's master-slave replication, read-write separation, backup and recovery
1. MySQL master-slave replication
1. Introduction
Why do we use master-slave replication?
Purpose of master-slave replication:
can do real-time backup of the database to ensure data integrity;
can separate reading and writing, the master server only writes, and the slave server only writes Read, which can improve overall performance.
Principle diagram:
As can be seen from the above figure, synchronization is completed by synchronous reading and writing of log files.
#2. Change the configuration file
Operate the machine for two days, make sure the server-id is different, usually the main server The ID should be smaller than the secondary ID. Be sure to pay attention.
# 3306和3307分别代表2台机器 # 打开log-bin,并使server-id不一样 #vim /data/3306/my.cnf log-bin = /data/3306/mysql-bin server-id = 1 #vim /data/3307/my.cnf log-bin = /data/3307/mysql-bin server-id = 3 #检查 1、 [root@bogon ~]# egrep "log-bin|server-id" /data/3306/my.cnf log-bin = /data/3306/mysql-bin server-id = 1 [root@bogon ~]# egrep "log-bin|server-id" /data/3307/my.cnf log-bin = /data/3307/mysql-bin server-id = 3 2、 [root@localhost ~]# mysql -uroot -p -S /data/3306/mysql.sock -e "show variables like 'log_bin';" Enter password: +--------+--------+ | Variable_name | Value | +--------+--------+ | log_bin | ON | # ON 为开始开启成功 +--------+--------+
3. Establish an account for slave replication. rep
Usually create a dedicated account for master-slave replication. Do not Forget authorization.
# 主库授权,允许从库来连接我取日志 [root@localhost ~]# mysql -uroot -p -S /data/3306/mysql.sock Enter password: # 允许从库192.168.200网段连接,账号rep,密码nick。 mysql> grant replication slave on *.* to 'rep'@'192.168.200.%' identified by 'nick'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) # 检查创建的rep账号: mysql> select user,host from mysql.user; +-----+-------------+ | user | host | +-----+--------------+ | root | 127.0.0.1 | | rep | 192.168.200.% | | root | localhost | | root | localhost.localdomain | +-----+------------------+ 7 rows in set (0.00 sec)
4. Back up the main database and restore it to the slave database
Back up the existing data in the master database and restore it to the slave database , at this time the data of the two hosts are consistent.
If you have data in advance, you must not forget this.
#1) Lock the main library so that it only has read-only permissions.
mysql> flush table with read lock; Query OK, 0 rows affected (0.00 sec) #5.1、5.5锁表命令略有不同。 # 5.1锁表:flush tables with read lock; # 5.5锁表:flush table with read lock;
2) Remember to back up at this point.
mysql> show master status; +-------+------+--------+---------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------+------+--------+---------+ | mysql-bin.000013 | 410 | | | +-------+------+--------+---------+ 1 row in set (0.00 sec)
3) Clone the window and back up the data.
[root@bogon ~]# mysqldump -uroot -p -S /data/3306/mysql.sock -A -B --events --master-data=2|gzip >/opt/rep.sql.gz Enter password: 参数: -A:备份所有的 #看rep.sql.gz参数 vim /opt/rep.sql.gz -- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=410;
4) Check the master status; whether the value is normal.
mysql> show master status; +------+------+---------+-------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------+-----+---------+--------+ | mysql-bin.000013 | 410 | | | +--------+----+---------+--------+ 1 row in set (0.00 sec)
5)Unlock the library
mysql> unlock tables; Query OK, 0 rows affected (0.00 sec)
6)Restore to the slave library
[root@bogon ~]# gunzip < /opt/rep.sql.gz | mysql -uroot -p -S /data/3307/mysql.sock Enter password:
5. Configure the slave library and take effect
Change the connection parameters of the slave library and the main library, and the configuration will take effect. The inspection was successful!
#1) Enter the slave library.
[root@bogon ~]# mysql -uroot -p -S /data/3307/mysql.sock Enter password:
2) Change the parameters used by the slave server to connect and communicate with the master server.
mysql> CHANGE MASTER TO MASTER_HOST='192.168.200.98', MASTER_PORT=3306, MASTER_USER='rep', MASTER_PASSWORD='nick', MASTER_LOG_FILE='mysql-bin.000013', MASTER_LOG_POS=410; Query OK, 0 rows affected (0.01 sec)
3) View the changed parameters.
[root@localhost ~]# cd /data/3307/data/ [root@localhost data]# cat master.info 18 mysql-bin.000013 410 192.168.200.98 REP nick 3306 60 0 0 1800.000 0
4) Effective!
mysql> start slave; Query OK, 0 rows affected (0.01 sec)
5) Check the following parameters, if they match, it is normal!
mysql> show slave status\G Relay_Master_Log_File: mysql-bin.000013 Slave_IO_Running: Yes #取logo。 Slave_SQL_Running: Yes #读relay-bin、logo,写数据。 Seconds_Behind_Master: 0 #落后主库的秒数。
6) View relay-bin.logo.
[root@localhost 3307]# cd /data/3307 [root@localhost 3307]# ll 总用量 48 drwxr-xr-x. 9 mysql mysql 4096 10月 29 18:52 data -rw-r--r--. 1 mysql mysql 1900 10月 29 11:45 my.cnf -rwx------. 1 root root 1307 10月 20 17:06 mysql -rw-rw----. 1 mysql mysql 6 10月 29 11:00 mysqld.pid -rw-r-----. 1 mysql mysql 15090 10月 29 18:49 mysql_nick3307.err srwxrwxrwx. 1 mysql mysql 0 10月 29 11:00 mysql.sock -rw-rw----. 1 mysql mysql 150 10月 29 18:49 relay-bin.000001 -rw-rw----. 1 mysql mysql 340 10月 29 18:52 relay-bin.000002 -rw-rw----. 1 mysql mysql 56 10月 29 18:49 relay-bin.index -rw-rw----. 1 mysql mysql 53 10月 29 18:52 relay-log.info
7) View relay-log.info.
[root@localhost 3307]# cat relay-log.info /data/3307/relay-bin.000002 340 mysql-bin.000013 497
8) View master.info.
[root@localhost 3307]# cat data/master.info 18 mysql-bin.000013 497 192.168.200.98 rep nick 3306 60 0 0 1800.000 0
6. Read-write separation
Read-write separation is common in production environments and is also a necessary skill.
Ignore MySQL master-slave replication authorization table synchronization and read-write separation.
[root@bogon 3306]# vim my.cnf #添加以下四行 replicate-ignore-db = mysql binlog-ignore-db = mysql binlog-ignore-db = performance_schema binlog-ignore-db = information_schema server-id = 1
1) Method to prevent data from being written to the slave library through the read-only parameter.
#修改配置文件。 vim /data/3307/my.cnf [mysqld] read-only #对用户授权事不能指定有super或all privileges权限。不然没效果。 #创建账户suoning,并刷新权限。 mysql> grant select,insert,update,delete on *.* to 'suoning'@'localhost' identified by '123'; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) #用创建账户登录,并创建库 [root@bogon 3307]# mysql -usuoning -p123 -S /data/3307/mysql.sock mysql> create user kangkangkang@'192.%' identified by 'old123'; ERROR 1290 (HY000): The MySQL server is running with the --read-only option so it cannot execute this statement
2) Master-slave synchronization failure
A. Last_SQL_Errno: 1007 stop slave; set global sql_slave_skip_counter = 1; start slave; B.忽略 skip-name-resolve #忽略名字解析 slave-skip-errors = 1032,1062,1007 #忽略故障编号 server-id = 3
3) Open bin-log from the library
vim my.cnf log-bin = /data/3307/mysql-bin log-slave-updates #表示从库记录bin-log expire_logs_days = 7 #保留7天bin-log。
7. Master downtime
Let’s simulate it. If the host goes down, how can we quickly replace the slave? Minimize losses? Of course, dual-machine hot backup is also a good choice, which will be explained to you in the next section.
In an environment with one master and multiple slaves, if the master goes down, choose one slave to be the master and continue to synchronize with other slaves.
A.查看每个从库的master.info,看谁的更靠前,最新,更大,丢的数据最少。 [root@localhost 3307]# cat /data/3307/data/master.info mysql-bin.000015 326 B.确保所有relay log全部更新完毕。 在每个从库上执行stop slave io_thread;show processlist; 知道看到Has read all relay log;表示从库更新都执行完毕。 C.登陆mysql -uroot -p -S /data/3307/mysql.sock stop slave; reset master; quit D.进入到数据库目录,删除master.info relay-log.info cd /data/3307/data/ rm -f master.info relay-log.info E. 3307提升为主库 vim /data/3307/my.cnf 开启log-bin = /data/3307/mysql-bin 如存在log-slave-updates,read-only等一定要注释。 /data/3307/mysql restart F.其它从库操作 stop slave; change master to master_host ='192.168.200.98'; start slave; show slave status\G
8. Dual-primary
Use primary-primary premise: the primary key of the table increases automatically.
In the case of dual masters, the ID will be like this. The ID is written to the library M1: 1, 3, 5 and M2: 2, 4, 6 through the program.
[root@localhost 3307]# vim my.cnf [mysqld] auto_increment_increment = 2 auto_increment_offset = 2 [root@localhost 3307]# ./mysql restart [root@localhost 3306]# vim my.cnf [mysqld] auto_increment_increment = 2 auto_increment_offset = 1 log-bin = /data/3306/mysql-bin log-slave-updates [root@localhost 3306]# ./mysql restart [root@localhost 3306]# mysqldump -uroot -pnick -S /data/3307/mysql.sock -A -B --master-data=1 -x --events > /opt/3307bak.sql [root@localhost 3306]# mysql -uroot -pnick -S /data/3306/mysql.sock < /opt/3307bak.sql mysql> CHANGE MASTER TO MASTER_HOST='192.168.200.98', MASTER_PORT=3307, MASTER_USER='rep', MASTER_PASSWORD='nick'; mysql> start slave; Query OK, 0 rows affected (0.00 sec) mysql> show slave status \G
2. MySQL backup and recovery
1. Back up a single database
The most basic backup of a single database.
1>语法:mysqldump –u 用户名 –p 数据库名> 备份的数据库名 2>备份nick_defailt数据库,查看内容。 [root@localhost ~]# mysqldump -uroot -p -B nick_defailt >/opt/mysql_nick_defailt.bak Enter password: [root@localhost ~]# egrep -v "#|\*|--|^$" /opt/mysql_nick_defailt.bak DROP TABLE IF EXISTS `oldsuo`; CREATE TABLE `oldsuo` ( `id` int(4) NOT NULL, `name` char(20) NOT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; LOCK TABLES `oldsuo` WRITE; INSERT INTO `oldsuo` VALUES (2,'ç´¢å®',0,NULL),(3,'索尼',0,NULL),(4,'底底',0,NULL); UNLOCK TABLES; DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `qq` varchar(15) DEFAULT NULL, `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `suo` int(4) DEFAULT NULL, `age` tinyint(2) NOT NULL DEFAULT '0', `dept` varchar(16) DEFAULT NULL, `sex` char(4) DEFAULT NULL, PRIMARY KEY (`id`), KEY `index_name` (`name`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=latin1; LOCK TABLES `student` WRITE; INSERT INTO `student` VALUES (NULL,2,'oldsuo',NULL,0,NULL,NULL),(NULL,3,'kangknag',NULL,0,NULL,NULL),(NULL,4,'kangkang',NULL,0,NULL,NULL),(NULL,5,'oldsuo',NULL,0,NULL,NULL),(NULL,6,'kangknag',NULL,0,NULL,NULL),(NULL,7,'kangkang',NULL,0,NULL,NULL); UNLOCK TABLES; 3>启用压缩备份数据库 [root@localhost~]#mysqldump -uroot -p -B nick_defailt|gzip>/opt/mysql_nick_defailt.bak.gz Enter password: [root@localhost ~]# ll /opt/ 总用量 28 -rw-r--r--. 1 root root 2109 10月 24 16:36 data_bak.sq2 -rw-r--r--. 1 root root 2109 10月 24 16:36 data_bak.sql -rw-r--r--. 1 root root 1002 10月 27 11:55 mysql_nick_defailt.bak -rw-r--r--. 1 root root 1002 10月 27 11:56 mysql_nick_defailt.bak.gz -rw-r--r--. 1 root root 3201 10月 27 11:46 mysql_nick_defailt_B.bak drwxr-xr-x. 2 root root 4096 11月 22 2013 rh -rw-r--r--. 1 root root 1396 10月 24 16:11 student_bak.sql 4>恢复nick_defailt数据库 [root@localhost ~]# mysql -uroot -p nick_defailt </opt/mysql_nick_defailt.bak Enter password: #加-B恢复方法 [root@localhost ~]# mysql -uroot -p </opt/mysql_nick_defailt_B.bak Enter password: 5>总结 1、备份用-B参数。增加use db,和create database的信息。 2、用gzip对备份的数据压缩。
2. Back up multiple databases
What about backing up multiple databases?
#多个数据库名中间加空格 [root@localhost ~]# mysqldump -uroot -p -B nick_defailt oldsuo oldsuo_1|gzip>/opt/mul.sql.gz Enter password:
3. Back up single and multiple tables
What should I do if I back up single and multiple tables?
1>语法:mysqldump -u 用户名 -p 数据库名 表名 > 备份的文件名 [root@localhost ~]# mysqldump -uroot -p nick_defailt student >/opt/mysql_nick_defailt_student.bak Enter password: 2>语法:mysqldump -u 用户名 -p 数据库名 表名1 表名2 > 备份的文件名 [root@localhost ~]# mysqldump -uroot -p nick_defailt student oldsuo >/opt/mysql_nick_defailt.bak Enter password:
4. Parameters of mysqldump
Key parameters of mysqldump
-B指定多个库,增加建库语句和use语句。 --compact去掉注释,适合调试输出,生产不用。 -A 备份所有库。 -F刷新binlog日志。 --master-data 增加binlog日志文件名及对应的位置点。 -x,--lock-all-tables -l,--locktables -d 只备份表结构 -t 只备份数据 --single-transaction 适合innodb事务数据库备份。
5. Add Volume recovery
The important thing is coming. The production environment generally uses incremental backup and recovery; the so-called incremental means to continue to add data on the basis of the original data, without having to add it again every time. save time and energy.
A:增量恢复必备条件: 1.开启MySQL数据库log-bin参数记录binlog日志。 [root@localhost 3306]# grep log-bin /data/3306/my.cnf log-bin = /data/3306/mysql-bin 2.存在数据库全备。 B:生产环境 mysqldump备份命令: # 进行数据库全备,(生产环境还通过定时任务每日凌晨执行) mysqldump -uroot -pnick -S /data/3306/mysql.sock --default-character-set=gbk --single-transaction -F -B nick |gzip >/server/backup/mysql_$(date +%F).sql.gz # innodb引擎备份 mysqldump -u$MYUSER -p$MYPASS -S $MYSOCK -F --single-transaction -A -B |gzip >$DATA_FILE # myisam引擎备份 mysqldump -u$MYUSER -p$MYPASS -S $MYSOCK -F -A -B --lock-all-tables |gzip >$DATA_FILE C:恢复: # 通过防火墙禁止web等应用向主库写数据或者锁表。让主库暂时停止更新,然后再进行恢复。 # 误操作删除nick库! 1.检查全备及binlog日志 [root@localhost 3306]# cd /server/backup/ [root@localhost backup]# gzip -d mysql_2015-10-31.sql.gz [root@localhost backup]# vim mysql_2015-10-31.sql [root@localhost backup]# grep -i "change" mysql_2015-10-31.sql
2. Immediately refresh and back up the binlog
[root@localhost 3306]# mysqladmin -uroot -pnick -S /data/3306/mysql.sock flush-logs [root@localhost 3306]# cp /data/3306/mysql-bin.000030 /server/backup/ #误操作log-bin,倒数第二 [root@localhost backup]# mysqlbinlog -d nick mysql-bin.000030 >bin.sql #导出为.sql格式。 [root@localhost backup]# vim bin.sql 找到语句drop database nick删除!!!(误操作语句)
3.Restore
[root@localhost backup]# mysql -uroot -pnick -S /data/3306/mysql.sock <mysql_2015-10-31.sql #恢复之前的数据库全备 [root@localhost backup]# mysql -uroot -pnick -S /data/3306/mysql.sock nick < bin.sql #恢复删除误操作语言的bin-log。 # 搞定!!!
The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work.
The above is the detailed content of Detailed explanation of MySQL's master-slave replication, read-write separation, backup and recovery. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

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

1. The first choice for the Laravel MySQL Vue/React combination in the PHP development question and answer community is the first choice for Laravel MySQL Vue/React combination, due to its maturity in the ecosystem and high development efficiency; 2. High performance requires dependence on cache (Redis), database optimization, CDN and asynchronous queues; 3. Security must be done with input filtering, CSRF protection, HTTPS, password encryption and permission control; 4. Money optional advertising, member subscription, rewards, commissions, knowledge payment and other models, the core is to match community tone and user needs.

There are three ways to connect Excel to MySQL database: 1. Use PowerQuery: After installing the MySQLODBC driver, establish connections and import data through Excel's built-in PowerQuery function, and support timed refresh; 2. Use MySQLforExcel plug-in: The official plug-in provides a friendly interface, supports two-way synchronization and table import back to MySQL, and pay attention to version compatibility; 3. Use VBA ADO programming: suitable for advanced users, and achieve flexible connections and queries by writing macro code. Choose the appropriate method according to your needs and technical level. PowerQuery or MySQLforExcel is recommended for daily use, and VBA is better for automated processing.

To achieve MySQL deployment automation, the key is to use Terraform to define resources, Ansible management configuration, Git for version control, and strengthen security and permission management. 1. Use Terraform to define MySQL instances, such as the version, type, access control and other resource attributes of AWSRDS; 2. Use AnsiblePlaybook to realize detailed configurations such as database user creation, permission settings, etc.; 3. All configuration files are included in Git management, support change tracking and collaborative development; 4. Avoid hard-coded sensitive information, use Vault or AnsibleVault to manage passwords, and set access control and minimum permission principles.

There are three main ways to set environment variables in PHP: 1. Global configuration through php.ini; 2. Passed through a web server (such as SetEnv of Apache or fastcgi_param of Nginx); 3. Use putenv() function in PHP scripts. Among them, php.ini is suitable for global and infrequently changing configurations, web server configuration is suitable for scenarios that need to be isolated, and putenv() is suitable for temporary variables. Persistence policies include configuration files (such as php.ini or web server configuration), .env files are loaded with dotenv library, and dynamic injection of variables in CI/CD processes. Security management sensitive information should be avoided hard-coded, and it is recommended to use.en

To collect user behavior data, you need to record browsing, search, purchase and other information into the database through PHP, and clean and analyze it to explore interest preferences; 2. The selection of recommendation algorithms should be determined based on data characteristics: based on content, collaborative filtering, rules or mixed recommendations; 3. Collaborative filtering can be implemented in PHP to calculate user cosine similarity, select K nearest neighbors, weighted prediction scores and recommend high-scoring products; 4. Performance evaluation uses accuracy, recall, F1 value and CTR, conversion rate and verify the effect through A/B tests; 5. Cold start problems can be alleviated through product attributes, user registration information, popular recommendations and expert evaluations; 6. Performance optimization methods include cached recommendation results, asynchronous processing, distributed computing and SQL query optimization, thereby improving recommendation efficiency and user experience.

PHP plays the role of connector and brain center in intelligent customer service, responsible for connecting front-end input, database storage and external AI services; 2. When implementing it, it is necessary to build a multi-layer architecture: the front-end receives user messages, the PHP back-end preprocesses and routes requests, first matches the local knowledge base, and misses, call external AI services such as OpenAI or Dialogflow to obtain intelligent reply; 3. Session management is written to MySQL and other databases by PHP to ensure context continuity; 4. Integrated AI services need to use Guzzle to send HTTP requests, safely store APIKeys, and do a good job of error handling and response analysis; 5. Database design must include sessions, messages, knowledge bases, and user tables, reasonably build indexes, ensure security and performance, and support robot memory

To recycle MySQL user permissions using REVOKE, you need to specify the permission type, database, and user by format. 1. Use REVOKEALLPRIVILEGES, GRANTOPTIONFROM'username'@'hostname'; 2. Use REVOKEALLPRIVILEGESONmydb.FROM'username'@'hostname'; 3. Use REVOKEALLPRIVILEGESONmydb.FROM'username'@'hostname'; 3. Use REVOKE permission type ON.*FROM'username'@'hostname'; Note that after execution, it is recommended to refresh the permissions. The scope of the permissions must be consistent with the authorization time, and non-existent permissions cannot be recycled.

To enable PHP containers to support automatic construction, the core lies in configuring the continuous integration (CI) process. 1. Use Dockerfile to define the PHP environment, including basic image, extension installation, dependency management and permission settings; 2. Configure CI/CD tools such as GitLabCI, and define the build, test and deployment stages through the .gitlab-ci.yml file to achieve automatic construction, testing and deployment; 3. Integrate test frameworks such as PHPUnit to ensure that tests are automatically run after code changes; 4. Use automated deployment strategies such as Kubernetes to define deployment configuration through the deployment.yaml file; 5. Optimize Dockerfile and adopt multi-stage construction
