Home Database Mysql Tutorial MySQL双Master配置_MySQL

MySQL双Master配置_MySQL

May 30, 2016 pm 05:10 PM

主机环境说明。

master1: 10.8.1.11

master2: 10.8.1.12

版本信息:

    [root@m1 ~]# mysql -V

    mysql  Ver 14.14 Distrib 5.6.27, for Linux (x86_64) using  EditLine wrapper

 

1、主库开启bin-log功能,配置server-id

 

修改my.cf配置文件,开启bin-log功能,配置server-id。

    [root@m1 ~]# more /etc/my.cnf
    
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    
    [mysqld]
    server-id = 1
    datadir = /var/lib/mysql
    log_bin = /var/lib/mysql/bin-log
    socket = /var/lib/mysql/mysql.sock
    slave_net_timeout = 60
    log-slave-updates
    slave-skip-errors=all
    skip-name-resolve
    sync_binlog=1
    auto_increment_increment=2
    auto_increment_offset=1
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
#salve-net-timeout默认是3600秒,缩短时间是为了防止双YES的假象
#(事实上我已遇到,参考地址:http://www.cnblogs.com/billyxp/p/3470376.html)
如果要指定同步或不同步哪些库,可使用如下参数
#binlog-do-db=osyunweidb   #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行
#binlog-ignore-db=mysql    #不同步mysql系统数据库
Copy after login

至于这些参数的说明具体看手册。

红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。

紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步

2、确认bin-log与server-id是否开启:

查看命令 show variables like 'log_bin'; show variables like 'server_id';

mysql> show variables like 'log_bin';
+---------------+-------+
|Variable_name | Value |
+---------------+-------+
|log_bin       | ON   |
+---------------+-------+
1 rowin set (0.00 sec)
mysql>show variables like 'server_id';
+---------------+-------+
|Variable_name | Value |
+---------------+-------+
|server_id     | 1     |
+---------------+-------+
1 rowin set (0.00 sec)
Copy after login

3、创建复制授权用户

mysql> grant replication slave on *.* to replication@'%'identified by '123456'; #授权该用户对所有表都能进行复制

mysql>flush privileges; #刷新权限

4、锁表,记录log-bin文件名和位置

mysql>flush tables with read lock;     #锁定所有表,此时数据库不能写入数据
QueryOK, 0 rows affected (0.05 sec)

mysql>show master status;      #查看最新bin-log文件及位置
+------------------------+------------+-------------------+-------------------------+
|File                   | Position   | Binlog_Do_DB      | Binlog_Ignore_DB        |
+------------------------+------------+-------------------+-------------------------+
|mysql-bin.000001       |  26314    |                   |                        |
+------------------------+------------+-------------------+-------------------------+
1row in set (0.00 sec)
Copy after login

5、锁表状态全备mysql数据

由于退出当前mysql登陆窗口,锁表功能就失效,需克隆一个会话进行全备。

#mysqldump-uroot -p -A -B > /tmp/mysql_bak_2015_11_17.sql

看下备份数据大小,确认备份成功。

[root@m1 ~]# ls -l mysql_bak_2015_11_17.sql

-rw-r--r--. 1 root root 645327 Nov 18 06:27 mysql_bak_2015_11_17.sql

[root@m1 ~]#

6、解除锁表

mysql>unlock tables;

或直接quit退出即可。

7、从库开启bin-log功能,配置server-id

从库开启bin-log功能后,待会在主上在配置同步,互为主从就完成了。

[root@m2 ~]# vi /etc/my.cnf
    
    # For advice on how to change settings please see
    # http://dev.mysql.com/doc/refman/5.6/en/server-configuration-defaults.html
    
    [mysqld]
    server-id = 2
    datadir = /var/lib/mysql
    log_bin = /var/lib/mysql/mysql-bin
    socket = /var/lib/mysql/mysql.sock
    slave_net_timeout = 60
    log-slave-updates
    slave-skip-errors=all
    skip-name-resolve
    sync_binlog=1
    auto_increment_increment=2
    auto_increment_offset=2
    
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

#salve-net-timeout默认是3600秒,缩短时间是为了防止双YES的假象
Copy after login

#

至于这些参数的说明具体看手册。

红色的部分非常重要,如果一个MASTER 挂掉的话,另外一个马上接管。

紫红色的部分指的是服务器频繁的刷新日志。这个保证了在其中一台挂掉的话,日志刷新到另外一台。从而保证了数据的同步

#/etc/init.d/mysql restart

8、确认从库bin-log与server-id是否开启

查看命令 show variables like 'log_bin'; show variables like 'server_id';

mysql>show variables like 'log_bin';
+---------------+-------+
|Variable_name | Value |
+---------------+-------+
|log_bin       | ON    |
+---------------+-------+
1row in set (0.00 sec)
mysql>show variables like 'server_id';
+---------------+-------+
|Variable_name | Value |
+---------------+-------+
|server_id     | 2     |
+---------------+-------+
1row in set (0.00 sec)
Copy after login

9、从库导入主库的全备数据

登陆mysql导入数据

mysql>source /root/mysql_bak_2015_11_17.sql

10、记录从库bin-log信息

因为在从库导入全备数据时,此时主库与从库的内容是一致的,但是bin-log位置不一定一致。

mysql>show master status;      #查看最新bin-log文件及位置
+------------------------+------------+-------------------+-------------------------+
|File                   |Position   | Binlog_Do_DB      | Binlog_Ignore_DB        |
+------------------------+------------+-------------------+-------------------------+
|mysql-bin.000003       |  2328055   |                   |                        |
+------------------------+------------+-------------------+-------------------------+
1row in set (0.00 sec)
Copy after login

11、从库设置同步主库

此处binlog文件与位置状态,是主库在步骤4锁表时show master status查看的位置状态。

CHANGE MASTER TO 
MASTER_HOST='10.8.1.11',
MASTER_PORT=3306,
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000001',
MASTER_LOG_POS=26314;
Copy after login

12、开启从库同步并确认同步是否成功

使用start slave开启同步功能,使用show slave status\G查看同步是否成功

mysql>start slave;
QueryOK, 0 rows affected (0.00 sec)
mysql>show slave status\G   #\G不按表格输出
***************************1. row ***************************
               Slave_IO_State: Waiting formaster to send event
                  Master_Host: 10.0.0.2
                  Master_User: replication
                  Master_Port: 8306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 136270
               Relay_Log_File:mysqld-relay-bin.000002
                Relay_Log_Pos: 72697
        Relay_Master_Log_File: mysql-bin.000001
             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: 98758
              Relay_Log_Space: 110366
              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: 622   #查看主从同步延迟,延迟大则可能需要优化
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
1row in set (0.00 sec)
Copy after login

#sql线程与IO线程都是YES,slave配置成功。

13、主库设置同步从库

由于从库是全备导入,原先在主库上配置的复制帐户也同样导入,所以这里不用在从库上新授权复制用户。

从库上的binlog文件与位置状态,是从库在刚导入时show master status查看到的位置状态。

CHANGEMASTER TO 
MASTER_HOST='10.8.1.12',
MASTER_PORT=3306,
MASTER_USER='replication',
MASTER_PASSWORD='123456',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=2328055;
#修改相应信息,直接把这些配置在mysql中粘贴即可。
Copy after login

14、开启同步并确认同步是否成功

使用start slave开启同步功能,使用show slave status\G查看同步是否成功

mysql>start slave;
QueryOK, 0 rows affected (0.00 sec)

mysql>show slave status\G
***************************1. row ***************************
               Slave_IO_State: Waiting formaster to send event
                  Master_Host: 172.16.0.2
                  Master_User: replication
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 107
               Relay_Log_File:mysqld-relay-bin.000006
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000007
             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: 107
              Relay_Log_Space: 556
              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: 2
1row in set (0.00 sec)
Copy after login

 

 

#IO线程与sql线程都是正常。

 

15、互为主从测试

 

在两台mysql各创建一个库,看两边是否都能进行同步。

分别在主库上执行 create database test01;

从库上执行create database test02;

 

看两台数据库上执行show databases;,看是否都有test01表和test02表。

我的经过测试,双主测试成功。

Statement of this Website
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

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 Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

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.

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

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.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

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.

Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

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: Simple Concepts for Easy Learning 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.

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

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.

Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

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.

Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

See all articles