您的位置:首页 > 运维架构 > Linux

linux6.4搭建mysql主从复制

2017-07-26 11:58 549 查看
1 复制概述

      Mysql内建的复制功能是构建大型,高性能应用程序的基础。将Mysql的数据分布到多个系统上去,这种分布的机制,是通过将Mysql的某一台主机的数据复制到其它主机(slaves)上,并重新执行一遍来实现的。复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器。主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循环。这些日志可以记录发送到从服务器的更新。当一个从服务器连接主服务器时,它通知主服务器从服务器在日志中读取的最后一次成功更新的位置。从服务器接收从那时起发生的任何更新,然后封锁并等待主服务器通知新的更新。
     请注意当你进行复制时,所有对复制中的表的更新必须在主服务器上进行。否则,你必须要小心,以避免用户对主服务器上的表进行的更新与对从服务器上的表所进行的更新之间的冲突。

   


1.1 mysql支持的复制类型:

  (1):基于语句的复制:  在主服务器上执行的SQL语句,在从服务器上执行同样的语句。MySQL默认采用基于语句的复制,效率比较高。  

            一旦发现没法精确复制时,   会自动选着基于行的复制。    

  (2):基于行的复制:把改变的内容复制过去,而不是把命令在从服务器上执行一遍. 从mysql5.0开始支持

  (3):混合类型的复制: 默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。

 1.2 . 复制解决的问题

         MySQL复制技术有以下一些特点:

         (1)    数据分布 (Data distribution )

         (2)    负载平衡(load balancing)

         (3)    备份(Backups) 

         (4)    高可用性和容错行 High availability and failover 

  1.3 复制如何工作 

        整体上来说,复制有3个步骤:   

       (1)    master将改变记录到二进制日志(binary log)中(这些记录叫做二进制日志事件,binary log events);

       (2)    slave将master的binary log events拷贝到它的中继日志(relay log);

       (3)    slave重做中继日志中的事件,将改变反映它自己的数据。

        

        

环境准备:

192.168.20.10 master

192.168.20.11 slave

第一、主机上安装mysql

1、查看是否默认安装了mysql

[root@master ~]# rpm -qa|grep mysql

libdbi-dbd-mysql-0.8.3-5.1.el6.x86_64

mysql-libs-5.1.66-2.el6_3.x86_64

mysql-bench-5.1.66-2.el6_3.x86_64

mysql-connector-java-5.1.17-6.el6.noarch

mysql-test-5.1.66-2.el6_3.x86_64

mod_auth_mysql-3.0.0-11.el6_0.1.x86_64

mysql-connector-odbc-5.1.5r1144-7.el6.x86_64

mysql-5.1.66-2.el6_3.x86_64

mysql-devel-5.1.66-2.el6_3.x86_64

php-mysql-5.3.3-22.el6.x86_64

rsyslog-mysql-5.8.10-6.el6.x86_64

mysql-server-5.1.66-2.el6_3.x86_64

qt-mysql-4.6.2-25.el6.x86_64

dovecot-mysql-2.0.9-5.el6.x86_64

2、使用yum卸载mysql

[root@master ~]# yum -y remove mysql-5.1.66-2.el6_3.x86_64

Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, security, subscription-manager, tmprepo, verify,

              : versionlock

..........................

Complete!

[root@master ~]# rpm -qa|grep mysql

mysql-libs-5.1.66-2.el6_3.x86_64

mysql-connector-java-5.1.17-6.el6.noarch

mod_auth_mysql-3.0.0-11.el6_0.1.x86_64

mysql-connector-odbc-5.1.5r1144-7.el6.x86_64

php-mysql-5.3.3-22.el6.x86_64

rsyslog-mysql-5.8.10-6.el6.x86_64

qt-mysql-4.6.2-25.el6.x86_64

dovecot-mysql-2.0.9-5.el6.x86_64

[root@master ~]# yum -y remove mysql-libs-5.1.66-2.el6_3.x86_64

.........                         

Complete!

[root@master ~]# rpm -qa|grep mysql

mysql-connector-java-5.1.17-6.el6.noarch

[root@master ~]# yum -y remove mysql-connector-java-5.1.17-6.el6.noarch

Loaded plugins: aliases, changelog, downloadonly, kabi, presto, product-id, security, subscription-manager
e202
, tmprepo, verify,

              : versionlock

...........

Complete!

3、主从节点安装mysql:

[root@master install]# ls

MySQL-5.6.37-1.el6.x86_64.rpm-bundle.tar

[root@master install]# tar xvf MySQL-5.6.37-1.el6.x86_64.rpm-bundle.tar 

MySQL-shared-5.6.37-1.el6.x86_64.rpm

MySQL-shared-compat-5.6.37-1.el6.x86_64.rpm

MySQL-server-5.6.37-1.el6.x86_64.rpm

MySQL-test-5.6.37-1.el6.x86_64.rpm

MySQL-client-5.6.37-1.el6.x86_64.rpm

MySQL-devel-5.6.37-1.el6.x86_64.rpm

MySQL-embedded-5.6.37-1.el6.x86_64.rpm

[root@master install]# rpm -ivh MySQL-server-5.6.37-1.el6.x86_64.rpm

warning: MySQL-server-5.6.37-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

Preparing...                ########################################### [100%]

   1:MySQL-server           ########################################### [100%]

2017-07-24 12:01:48 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-07-24 12:01:48 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.

2017-07-24 12:01:48 0 [Note] /usr/sbin/mysqld (mysqld 5.6.37) starting as process 36859 ...

2017-07-24 12:01:48 36859 [Note] InnoDB: Using atomics to ref count buffer pool pages

2017-07-24 12:01:48 36859 [Note] InnoDB: The InnoDB memory heap is disabled

2017-07-24 12:01:48 36859 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2017-07-24 12:01:48 36859 [Note] InnoDB: Memory barrier is not used

2017-07-24 12:01:48 36859 [Note] InnoDB: Compressed tables use zlib 1.2.3

2017-07-24 12:01:48 36859 [Note] InnoDB: Using Linux native AIO

2017-07-24 12:01:48 36859 [Note] InnoDB: Using CPU crc32 instructions

2017-07-24 12:01:48 36859 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2017-07-24 12:01:48 36859 [Note] InnoDB: Completed initialization of buffer pool

2017-07-24 12:01:48 36859 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!

2017-07-24 12:01:48 36859 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB

2017-07-24 12:01:48 36859 [Note] InnoDB: Database physically writes the file full: wait...

2017-07-24 12:01:48 36859 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB

2017-07-24 12:01:48 36859 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB

2017-07-24 12:01:48 36859 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0

2017-07-24 12:01:48 36859 [Warning] InnoDB: New log files created, LSN=45781

2017-07-24 12:01:48 36859 [Note] InnoDB: Doublewrite buffer not found: creating new

2017-07-24 12:01:48 36859 [Note] InnoDB: Doublewrite buffer created

2017-07-24 12:01:48 36859 [Note] InnoDB: 128 rollback segment(s) are active.

2017-07-24 12:01:48 36859 [Warning] InnoDB: Creating foreign key constraint system tables.

2017-07-24 12:01:48 36859 [Note] InnoDB: Foreign key constraint system tables created

2017-07-24 12:01:48 36859 [Note] InnoDB: Creating tablespace and datafile system tables.

2017-07-24 12:01:48 36859 [Note] InnoDB: Tablespace and datafile system tables created.

2017-07-24 12:01:48 36859 [Note] InnoDB: Waiting for purge to start

2017-07-24 12:01:48 36859 [Note] InnoDB: 5.6.37 started; log sequence number 0

A random root password has been set. You will find it in '/root/.mysql_secret'.

2017-07-24 12:01:49 36859 [Note] Binlog end

2017-07-24 12:01:49 36859 [Note] InnoDB: FTS optimize thread exiting.

2017-07-24 12:01:49 36859 [Note] InnoDB: Starting shutdown...

2017-07-24 12:01:50 36859 [Note] InnoDB: Shutdown completed; log sequence number 1625977

2017-07-24 12:01:50 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2017-07-24 12:01:50 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.

2017-07-24 12:01:50 0 [Note] /usr/sbin/mysqld (mysqld 5.6.37) starting as process 36885 ...

2017-07-24 12:01:50 36885 [Note] InnoDB: Using atomics to ref count buffer pool pages

2017-07-24 12:01:50 36885 [Note] InnoDB: The InnoDB memory heap is disabled

2017-07-24 12:01:50 36885 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins

2017-07-24 12:01:50 36885 [Note] InnoDB: Memory barrier is not used

2017-07-24 12:01:50 36885 [Note] InnoDB: Compressed tables use zlib 1.2.3

2017-07-24 12:01:50 36885 [Note] InnoDB: Using Linux native AIO

2017-07-24 12:01:50 36885 [Note] InnoDB: Using CPU crc32 instructions

2017-07-24 12:01:50 36885 [Note] InnoDB: Initializing buffer pool, size = 128.0M

2017-07-24 12:01:50 36885 [Note] InnoDB: Completed initialization of buffer pool

2017-07-24 12:01:50 36885 [Note] InnoDB: Highest supported file format is Barracuda.

2017-07-24 12:01:50 36885 [Note] InnoDB: 128 rollback segment(s) are active.

2017-07-24 12:01:50 36885 [Note] InnoDB: Waiting for purge to start

2017-07-24 12:01:50 36885 [Note] InnoDB: 5.6.37 started; log sequence number 1625977

2017-07-24 12:01:50 36885 [Note] Binlog end

2017-07-24 12:01:50 36885 [Note] InnoDB: FTS optimize thread exiting.

2017-07-24 12:01:50 36885 [Note] InnoDB: Starting shutdown...

2017-07-24 12:01:52 36885 [Note] InnoDB: Shutdown completed; log sequence number 1625987

A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !

You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,

no other statement but 'SET PASSWORD' will be accepted.

See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.

This is strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/
The latest information about MySQL is available on the web at

  http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
New default config file was created as /usr/my.cnf and

will be used by default by the server when you start it.

You may edit this file to change server settings

[root@master install]# rpm -ivh MySQL-client-5.6.37-1.el6.x86_64.rpm 

warning: MySQL-client-5.6.37-1.el6.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY

Preparing...                ########################################### [100%]

   1:MySQL-client           ########################################### [100%]

   

   

[root@master mysql]# id mysql

uid=27(mysql) gid=27(mysql) groups=27(mysql)

[root@master mysql]# 

第二、配置主从复制

master节点:

修改/etc/my.cnf

[mysqld]

datadir = /u02/mysql

server_id = 1

port = 3306

socket = /var/lib/mysql/mysql.sock

log-error=/u02/mysql/mysqld.log

log-bin = /u02/mysql/master-bin

binlog_format = row

skip-name-resolve

sync_binlog = 1

default_storage_engine = innodb

log_slave_updates = 1

read_only = 0

[root@master mysql]# service mysql start

Starting MySQL.Logging to '/var/lib/mysql/master.err'.

[OK]

新增测试数据:

-bash-4.1$ mysql -p -u root

Enter password: 

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 6

Server version: 5.6.37

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database dtxx;

mysql> show databases;

+---------------------+

| Database            |

+---------------------+

| information_schema  |

| dtxx                |

| hello               |

| #mysql50#master-bin |

| mysql               |

| performance_schema  |

| test                |

+---------------------+

8 rows in set (0.00 sec)

mysql> use dtxx

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

Database changed

mysql> show tables;

+----------------+

| Tables_in_dtxx |

+----------------+

| test           |

+----------------+

1 row in set (0.00 sec)

创建具有复制权限的账号

mysql>grant replication slave on *.* to repluser@'192.168.20.%' identified by 'repluser';

Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

备份master数据库

[root@master mysql]$ mysqldump --single-transaction --all-databases --master-data=2 > myback.sql   #完全备份

[root@master mysql]$scp myback.sql root@192.168.20.11:/u02/mysql

配置从库:

[mysqld]

datadir = /u02/mysql

port = 3306

server_id = 2

socket = /var/lib/mysql/mysql.sock

sync_binlog = 1

skip-name-resolve

relay-log = /u02/mysql/slave-relay   #启用中继日志

sync_master_info = 1

sync_relay_log = 1

sync_relay_log_info = 1

read_only = on   #只读

在从库上恢复主库:

[root@slave ~]# service mysqld start

Starting MySQL SUCCESS!

[root@slave ~]# mysql -p12345 -uroot < myback.sql   #先将主库的备份数据恢复至从库

查看备份日志里主库的log_file跟log_pos:

-bash-4.1$ grep "^-- CHANGE" myback.sql
-- CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000004', MASTER_LOG_POS=1361;

使用有复制权限的账号连接master:

mysql> change master to master_host='192.168.20.10',master_user='repluser',master_password='repluser',\

    -> master_log_file='master-bin.000004',master_log_pos=1361;

Query OK, 0 rows affected, 2 warnings (0.01 sec)

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: 10.81.35.2

                  Master_User: repluser

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000004
          Read_Master_Log_Pos: 1361

               Relay_Log_File: slave-relay.000002

                Relay_Log_Pos: 284

        Relay_Master_Log_File: master-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: 1361

              Relay_Log_Space: 453

              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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8

             Master_Info_File: /u02/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)

-bash-4.1$ more /u02/mysql/master.info

23

master-bin.000004
1361

10.81.35.2

repluser

repluser

3306

60

0

0

1800.000

0

cd2fab5f-7025-11e7-b37c-e8611f1a5ff8

86400

0

测试:

mysql> create database testdb;

Query OK, 1 row affected (0.00 sec)

mysql> use testdb;

Database changed

mysql> create table students (Name char(30),Gender enum('m','f'));

Query OK, 0 rows affected (0.00 sec)

mysql> show databases;

+---------------------+

| Database            |

+---------------------+

| information_schema  |

| dtxx                |

| #mysql50#master-bin |

| mysql               |

| performance_schema  |

| test                |

| testdb              |

+---------------------+

7 rows in set (0.00 sec)

mysql> show tables from testdb;

+------------------+

| Tables_in_testdb |

+------------------+

| students         |

+------------------+

1 row in set (0.00 sec)

mysql> show processlist;

+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+

| Id | User     | Host                     | db     | Command     | Time | State                                                                 | Info             |

+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+

| 16 | repluser | master.cdterp.com:22298 | NULL   | Binlog Dump |  195 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |

| 17 | root     | localhost                | testdb | Query       |    0 | init                                                                  | show processlist |

+----+----------+--------------------------+--------+-------------+------+-----------------------------------------------------------------------+------------------+

2 rows in set (0.00 sec)

从库:

-bash-4.1$ more /u02/mysql/master.info

23

master-bin.000004
1597

10.81.35.2

repluser

repluser

3306

60

0

0

1800.000

0

cd2fab5f-7025-11e7-b37c-e8611f1a5ff8

86400

0

-bash-4.1$ mysql -p12345 -uroot

Warning: Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 10

Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show slave status \GG

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 10.81.35.2

                  Master_User: repluser

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: master-bin.000004

          Read_Master_Log_Pos: 1597  --这个从1361到了1597

               Relay_Log_File: slave-relay.000002

                Relay_Log_Pos: 520

        Relay_Master_Log_File: master-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: 1597

              Relay_Log_Space: 689

              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: cd2fab5f-7025-11e7-b37c-e8611f1a5ff8

             Master_Info_File: /u02/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)

mysql> SHOW DATABASES;

+----------------------+

| Database             |

+----------------------+

| information_schema   |

| dtxx                 |

| #mysql50#master-bin  |

| mysql                |

| performance_schema   |

| #mysql50#slave-relay |

| test                 |

| testdb               |

+---------------------+

7 rows in set (0.00 sec)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: