美文网首页
mysql主从复制实战

mysql主从复制实战

作者: 4a873e424089 | 来源:发表于2018-11-21 20:07 被阅读0次

    主从复制的配置

    软件版本

    1.双方的MySQL要一致

    2.如果不一致:主的要低于从的

    从哪儿开始复制:

    1.都从0开始

    2.主服务器已经运行一段时间,并且存在不小的数据集:把主服务器备份,然后在从服务器恢复,从主服务器上备份处的位置开始复制

    配置过程:

    主服务器:

    1.改server-id

    2.启用二进制日志

    3.创建有复制权限的账号

    grant replication slave,replication client on *.* to 'uername'@'hostname' identified by 'password'

    flush privileges

    从服务器:

    1.改server-id

    2.启用中继日志

    relay-log = /mydata/relaylogs/slave-bin

    3.连接主服务器

    连接主服务器的命令:

    CHANGE MASTER TO MASTER_HOST = '', MASTER_USER='', MASTER_PASSWORD='', MASTER_LOG_FILE='', MASTER_LOG_POS=;

    MASTER_HOST = 'host_name'  主服务器地址

    MASTER_USER = 'user_name' 以那个用户的身份复制

    MASTER_PASSWORD = 'password' 密码

    MASTER_LOG_FILE = 'master_log_name' 复制开始文件

    MASTER_LOG_POS = master_log_pos 那个节点开始

    4.启动复制线程

    START SLAVE [thread_types]

    START SLAVE [SQL_THREAD] UNTIL

    MASTER_LOG_FILE = 'log_name', MASTER_LOG_POS = log_pos

    START SLAVE [SQL_THREAD] UNTIL

    RELAY_LOG_FILE = 'log_name', RELAY_LOG_POS = log_pos

    thread_types:

    [thread_type [, thread_type] ... ]

    thread_type: IO_THREAD | SQL_THREAD

    配置实例:主从复制之从零开始

    主服务器

    service mysqld stop

    vim /etc/my.cnf

    server-id = 10

    log-bin=/mydata/binlog/master-bin

    chown -R mysql.mysql /mydata/binlog

    service mysqld start

    mysql

    GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.130.%' IDENTIFIED BY 'replpass';

    FLUSH PRIVILEGES;

    从服务器

    service mysqld stop

    vim /etc/my.cnf

    #log-bin=mysql-bin

    #binlog_format=mixed

    server-id = 20

    relay-log = /mydata/binlog/relay-bin

    chown -R mysql.mysql  /mydata/binlog/

    service mysqld start

    mysql

    SHOW GLOBAL VARIABLES LIKE '%relay%'; 

    连接主服务器

    mysql

    CHANGE MASTER TO MASTER_HOST = '192.168.130.61', MASTER_USER='repluser', MASTER_PASSWORD='replpass';

    START SLAVE;

    主服务创建数据库测试主从复制

    CREATE DATABASE testdb;

    USE testdb;

    CREATE TABLE t1(ID INT);

    INSERT INTO t1 VALUES(1),(2);

    主服务器

    SHOW MASTER STATUS\G;

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

                File: master-bin.000002

            Position: 780

        Binlog_Do_DB: 

    Binlog_Ignore_DB: 

    1 row in set (0.00 sec)

    ERROR: No query specified

    从服务器

    SHOW SLAVE STATUS\G;

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

                   Slave_IO_State: Waiting for master to send event

                      Master_Host: 192.168.130.61

                      Master_User: repluser

                      Master_Port: 3306

                    Connect_Retry: 60

                  Master_Log_File: master-bin.000002

              Read_Master_Log_Pos: 780

                   Relay_Log_File: relay-bin.000003

                    Relay_Log_Pos: 1080

            Relay_Master_Log_File: master-bin.000002

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

                  Relay_Log_Space: 2102

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

                   Master_SSL_Crl: 

               Master_SSL_Crlpath: 

                       Using_Gtid: No

                      Gtid_IO_Pos: 

          Replicate_Do_Domain_Ids: 

      Replicate_Ignore_Domain_Ids: 

                    Parallel_Mode: conservative

                        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

    1 row in set (0.00 sec)

    配置实例:主从复制之半道复制

    主服务器

    service mysqld stop

    vim /etc/my.cnf

    server-id = 10

    log-bin=/mydata/binlog/master-bin

    chown -R mysql.mysql /mydata/binlog

    service mysqld start

    mysql

    GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'repluser'@'192.168.130.%' IDENTIFIED BY 'replpass';

    FLUSH PRIVILEGES;

    SHOW MASTER STATUS\G;

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

                File: master-bin.000002

            Position: 780

        Binlog_Do_DB: 

    Binlog_Ignore_DB: 

    1 row in set (0.00 sec)

    ERROR: No query specified

    mysqldump --all-databases --flush-logs --master-data=2 --lock-all-tables > /mydata/backups/all.sql 

    scp /mydata/backups/all.sql  root@192.168.130.62:/mydata/backups

    从服务器

    service mysqld stop

    vim /etc/my.cnf

    #log-bin=mysql-bin

    #binlog_format=mixed

    server-id = 20

    relay-log = /mydata/binlog/relay-bin

    chown -R mysql.mysql  /mydata/binlog/

    service mysqld start

    mysql

    SHOW GLOBAL VARIABLES LIKE '%relay%';

    mysql < /mydata/backups/all.sql 

    主服务创建数据库测试主从复制

    CREATE DATABASE testdb1;

    USE testdb1;

    CREATE TABLE t1(ID INT);

    INSERT INTO t1 VALUES(1),(2);

    连接主服务器

    mysql

    CHANGE MASTER TO MASTER_HOST = '192.168.130.61', MASTER_USER='repluser', MASTER_PASSWORD='replpass', MASTER_LOG_FILE='master-bin.000002', MASTER_LOG_POS=780;

    START SLAVE;

    主服务器  APP开发找上海捌跃网络科技有限公司 17621291122

    SHOW MASTER STATUS\G;

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

                File: master-bin.000003

            Position: 828

        Binlog_Do_DB: 

    Binlog_Ignore_DB: 

    1 row in set (0.00 sec)

    ERROR: No query specified

    从服务器

     SHOW SLAVE STATUS\G;

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

                   Slave_IO_State: Waiting for master to send event

                      Master_Host: 192.168.130.61

                      Master_User: repluser

                      Master_Port: 3306

                    Connect_Retry: 60

                  Master_Log_File: master-bin.000003

              Read_Master_Log_Pos: 828

                   Relay_Log_File: relay-bin.000004

                    Relay_Log_Pos: 1128

            Relay_Master_Log_File: master-bin.000003

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

                  Relay_Log_Space: 1479

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

                   Master_SSL_Crl: 

               Master_SSL_Crlpath: 

                       Using_Gtid: No

                      Gtid_IO_Pos: 

          Replicate_Do_Domain_Ids: 

      Replicate_Ignore_Domain_Ids: 

                    Parallel_Mode: conservative

                        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

    1 row in set (0.00 sec)

    转自:http://blog.51cto.com/kaiyuandiantang/2320090

    相关文章

      网友评论

          本文标题:mysql主从复制实战

          本文链接:https://www.haomeiwen.com/subject/dwkhqqtx.html