MySQL5.6 Replication主从复制(读写分离) 配置完整版_MySQL
MySQL5.6主从复制(读写分离)教程
1、MySQL5.6开始主从复制有两种方式:
基于日志(binlog);
基于GTID(全局事务标示符)。
需要注意的是:GTID方式不支持临时表!所以如果你的业务系统要用到临时表的话就不要考虑这种方式了,至少目前最新版本MySQL5.6.12的GTID复制还是不支持临时表的。
所以本教程主要是告诉大家如何通过日志(binlog)方式做主从复制!
2、MySQL官方提供的MySQL Replication教程:
http://dev.mysql.com/doc/refman/5.6/en/replication.html
第一步:准备工作
主服务器: 192.168.1.100
从服务器: 192.168.1.101
MySQL软件版本:
MySQL-server-advanced-5.6.18-1.el6.x86_64.rpm
MySQL-cient-advanced-5.6.18-1.el6.x86_64.rpm
第二步:在主服务器和从服务器上安装MySQL数据库软件
安装方法,请参见
RedHat6.5下MySQL5.6集群配置完整版 http://www.linuxidc.com/Linux/2014-06/103748.htm
MySQL数据库软件安装完成后,不要急着做mysql启动操作。建议把mysql初始化生成的/usr/my.cnf
(如果是从源文件编译安装时,路径应该是在/usr/local/mysql/mysql.cnf)删除,然后把优化好的mysql
配置文件my.cnf放到/etc下。
第三步:修改主数据库的配置文件/usr/my.cnf
[mysqld]
server-id=1
log-bin=mysqlmaster-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
第四步:修改从数据库配置文件/usr/my.cnf
log-bin=mysqlslave-bin.log
sync_binlog=1
innodb_buffer_pool_size=512M
innodb_flush_log_at_trx_commit=1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
lower_case_table_names=1
log_bin_trust_function_creators=1
第五步:在主数据库和从数据库服务器上分别执行以下命令重新启动主数据库和从数据库
[root@master ~]# service mysql restart
[root@slave ~]# service mysql restart
第六步:在主数据库上创建用于主从复制的账户
[root@master ~]# mysql -uroot -p
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.1.101' IDENTIFIED BY '111111';
Query OK, 0 rows affected (0.00 sec)
注意:以上命令中的IP地址,是从数据库服务器的IP地址。
--------------------------------------分割线 --------------------------------------
Ubuntu14.04下安装MySQLhttp://www.linuxidc.com/Linux/2014-05/102366.htm
《MySQL权威指南(原书第2版)》清晰中文扫描版 PDFhttp://www.linuxidc.com/Linux/2014-03/98821.htm
Ubuntu 14.04 LTS 安装 LNMP Nginx/PHP5 (PHP-FPM)/MySQLhttp://www.linuxidc.com/Linux/2014-05/102351.htm
Ubuntu 14.04下搭建MySQL主从服务器http://www.linuxidc.com/Linux/2014-05/101599.htm
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群http://www.linuxidc.com/Linux/2013-11/93019.htm
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdbhttp://www.linuxidc.com/Linux/2013-08/89270.htm
--------------------------------------分割线 --------------------------------------
第七步:主数据库锁表(禁止再插入数据以获取主数据库的的二进制日志坐标)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
第八步:查看主数据库的状态(并记录下File字段和Position字段的值,在配置从服务器时有用到)
mysql> show master status;
+------------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------------+----------+--------------+------------------+-------------------+
| mysqlmaster-bin.000004 | 327 | | | |
+------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
第九步:创建主数据库的快照文件
[root@master ~]# cd /usr/bin/
# ./mysqldump -uroot -p -h127.0.0.1 -P3306 --all-databases --triggers --routines --events >>/mnt/windows/all.sql
上面命令中的红色部分,是一个共享目录,这个目录可以同时被主数据库服务器和从数据库服务器访问到。
如果没有这样的共享目录,可以将all.sql放在其它任何目录下,然后使用scp命令复制到远程从数据库服务器的某个目录中
这条命令的执行时间根据数据量的不同,会有所不同,如果主数据库的数据量很大,可能需要很长时间,那么在这种情况下,就最好在晚上没有业务的时候进行这个操作,否则第七步中的锁表操作会对业务系统造成很大的影响
第十步:解锁主数据库的锁表操作
[root@master ~]# mysql -uroot -p (本命令在主数据库服务器上执行)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
第十一步:在从数据库服务器上导入第七步创建的快照文件到从数据库中
[root@slave ~]# mysql -uroot -p -h127.0.0.1 -P3306
第十二步:在从数据库服务器上设置主数据库服务器向从数据库服务器同步
[root@slave ~]# mysql -uroot -p
mysql> change master to master_host = '192.168.1.100',master_user='repl',master_password='111111',master_log_file='mysqlmaster-bin.000004',master_log_pos=327;
注意:红色部分的值,是在第八步中查出来的,这里不能弄错了
第十三步:启动从数据库复制线程
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
第十四步:查询从数据库的复制线程状态
mysql> show slave status /G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.100
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysqlmaster-bin.000004
Read_Master_Log_Pos: 327
Relay_Log_File: slave-relay-bin.000002
Relay_Log_Pos: 289
Relay_Master_Log_File: mysqlmaster-bin.000004
Slave_IO_Running: Yes
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: 327
Relay_Log_Space: 462
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: 0
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: 2e5e1b22-f0a9-11e3-bbac-000c297799e0
Master_Info_File: /var/lib/mysql/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)
如果Slave_IO_Running和Slave_SQL_Running两项都为yes,就表示主从复制配置成功了.
下面可以开始测试配置是否成功了,首先在主数据库的test数据库中新建一张表,然后插入几条数据,然后到从数据库看看是否同步过来了。
注意:当从数据库有大量的查询时,可以暂时将从数据库的复制线程关闭掉,等查询量降下来了,再打开,这样也不会丢失数据。
更多详情见请继续阅读下一页的精彩内容:http://www.linuxidc.com/Linux/2014-06/103752p2.htm

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

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



Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

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.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.
