mysql主從複製

來源:互聯網
上載者:User

標籤:

首先安裝mysql,在上文搭建lnmp環境中已經介紹了,這裡就不在贅述。

初始化主從資料庫

切換到mysql安裝目錄下:cd /app/local/mysql

./scripts/mysql_install_db --datadir=/app/local/data/var1 --user=mysql

./scripts/mysql_install_db --datadir=/app/local/data/var2 --user=mysql

 編輯my.cnf設定檔

vi /etc/my.cnf

在最下面加入:

[mysqld_multi]

mysqld = /app/local/mysql/bin/mysqld_safe

mysqladmin = /app/local/mysql/bin/mysqladmin

user = root  

#password = multipass

log = /app/local/data/mysqld_multi.log

 

[mysqld3307]

socket = /tmp/mysql3307.sock

port = 3307

pid-file = /tmp/mysql3307.pid

datadir = /app/local/data/var1

user = mysql

#log = /var/log/mysql1.log

 

log-bin = mysql-bin

binlog_do_db = m_test#要同步複製的資料庫

server-id = 1

 

[mysqld3308]

socket = /tmp/mysql3308.sock

port = 3308

pid-file = /tmp/mysql3308.pid

datadir = /app/local/data/var2

user = mysql

#log = /var/log/mysql2.log

 

#log-bin = /var/lib/mysql2/mysql-bin-3308

replicate_do_db = m_test

server-id = 2

儲存退出後重啟mysql服務

通過netstat -nptl 查看mysql是否啟動

啟動資料庫執行個體: mysqld_multi --defaults-extra-file=/etc/my.cnf start 3307

查看資料庫執行個體狀態: mysqld_multi --defaults-extra-file=/etc/my.cnf report

登入主庫:mysql -uroot -S /tmp/mysql3307.sock -p

建立同步複製的使用者:grant replication slave on *.* to ‘slave3307‘@‘127.0.0.1‘ identified by ‘3307‘;

flush privileges;#重新整理許可權

查看主從狀態:show master status;

mysql> show master status;

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

| File             | Position | Binlog_Do_DB              | Binlog_Ignore_DB | E                                                                                        xecuted_Gtid_Set |

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

| mysql-bin.000005 |      120 | m_test,m_test |                  |                                                                                                           |

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

1 row in set (0.07 sec)

 

>exit;#退出主庫

設定從庫:mysql -uroot -S /tmp/mysql3308.sock -p

change master to master_host=‘127.0.0.1‘,master_port=3307,master_user=‘slave3307‘,master_password=‘3307‘,master_log_file=‘mysql-bin.000005‘,master_log_pos=120;

start slave;

有時候在start slave時會提示錯誤需要先stop slave;然後再change master....;start slave;

show slave status\G查看狀態:

mysql> show slave status\G

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 127.0.0.1

                  Master_User: slave3307

                  Master_Port: 3307

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000005

          Read_Master_Log_Pos: 120

               Relay_Log_File: mysql3308-relay-bin.000008

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-bin.000005

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

              Replicate_Do_DB: m_test,m_test

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

              Relay_Log_Space: 623

              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: 5f043771-991e-11e5-b3fc-000c296ef339

             Master_Info_File: /app/local/data/var2/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.05 sec)

當以下兩個參數是yes時說明同步成功了

Slave_IO_Running: Yes
Slave_SQL_Running: Yes

在主庫中建立m_test資料庫,登入從庫可看見m_test,在主庫中通過向資料庫m_test添加資料可同步到從庫中去

mysql主從複製

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.