Linux mysql 5.7.23 主從複製(非同步複製)

來源:互聯網
上載者:User

標籤:測試   error:   end   systemctl   orm   amp   command   database   out   

docker容器主節點: 172.17.0.9 docker容器子節點: 172.17.0.10非同步複製:首先確認主庫和從庫是否一致,最好都是剛剛初始化的乾淨的資料庫如果主庫正在使用不能初始化,就要先將主庫的內容複寫到從庫上再做同步1、先把master做一個備份,把備份出的檔案傳給slave[[email protected] /]# mysqldump -uroot -p -B test > test.dumpEnter password: [[email protected] data]# mysql -uroot -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.23 MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> source /data/test.dumpQuery OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Database changedQuery OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.01 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 1 row affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected, 1 warning (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || sys                || test               |+--------------------+5 rows in set (0.00 sec)再到slave上將資料恢複mysql> source /mysql/test3.sqlmysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test1              || test2              || test3              |+--------------------+6 rows in set (0.00 sec)2.開啟主節點Bin-log日誌

vi /etc/my.cnf

添加bin-log配置 

server-id=1log-bin=mysql-binbinlog_format=row

 systemctl restart mysqld 

 

3.建立複製使用者,賦予使用者權限mysql> grant replication slave on *.* to ‘repl‘@‘%‘ identified by ‘mysql‘;mysql> flush privileges;mysql> show master status;       要記住position+------------------+----------+--------------+--------------------------+-------------------+| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |+------------------+----------+--------------+--------------------------+-------------------+| mysql-bin.000003 |     683 |              | mysql,information_schema |                   |+------------------+----------+--------------+--------------------------+-------------------+1 row in set (0.00 sec)4.再到slavemysql>  change master to master_host=‘172.17.0.9‘,master_port=3306,master_user=‘repl‘,    ->  master_password=‘mysql‘’,master_log_file=‘mysql-bin.000003‘,master_log_pos=683;Query OK, 0 rows affected, 2 warnings (0.01 sec)mysql>  start slave;Query OK, 0 rows affected (0.09 sec)mysql> show slave status\G;*************************** 1. row ***************************               Slave_IO_State: Waiting for master to send event                  Master_Host: 172.17.0.9                  Master_User: repl                  Master_Port: 3306                Connect_Retry: 60              Master_Log_File: mysql-bin.000003          Read_Master_Log_Pos: 154               Relay_Log_File: 72bd50b77c93-relay-bin.000002                Relay_Log_Pos: 320        Relay_Master_Log_File: mysql-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: 154              Relay_Log_Space: 534              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: 0Master_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: 8ce1a2b2-b01c-11e8-90a0-0242ac11000a             Master_Info_File: /usr/local/mysql/data/master.info                    SQL_Delay: 0          SQL_Remaining_Delay: NULL      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates           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         Replicate_Rewrite_DB:                  Channel_Name:            Master_TLS_Version: 1 row in set (0.00 sec)看到兩個yes 就基本成功了 可以在主庫上進行一些操作來測試一下

 

5.測試結果:

 主庫插入資料

[[email protected] mysql]# mysql -urootEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 5Server version: 5.7.23-log MySQL Community Server (GPL)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+------+------+| id   | name | age  |+------+------+------+|    1 | dd   |   10 |+------+------+------+1 row in set (0.00 sec)mysql> insert into t values(2,‘2222‘,11);Query OK, 1 row affected (0.00 sec)mysql> 

 

 

登入slave資料庫

mysql> use test;Reading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> select * from t;+------+------+------+| id   | name | age  |+------+------+------+|    1 | dd   |   10 ||    2 | 2222 |   11 |+------+------+------+2 rows in set (0.00 sec)mysql> mysql> mysql> 

 

Linux mysql 5.7.23 主從複製(非同步複製)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.