MySQL主從同步原理 部署,mysql主從原理部署

來源:互聯網
上載者:User

MySQL主從同步原理 部署【轉】,mysql主從原理部署

一.主從的作用:
1.可以當做一種備份方式
2.用來實現讀寫分離,緩解一個資料庫的壓力
二.MySQL主從備份原理
master 上提供binlog ,
slave 通過 I/O線程從 master拿取 binlog,並複製到slave的中繼日誌中
slave 通過 SQL線程從 slave的中繼日誌中讀取binlog ,然後解析到slave中
部署主從環境:主伺服器:192.168.1.110(編譯好的MySQL5.1版本的資料庫)
從伺服器:192.168.1.120(編譯好的MySQL5.1版本的資料庫)
(溫馨提示:主和從資料庫版本必須是一樣。或者主庫的資料庫版本必須比從庫高,不然會導致很多故障的發生。)
三:生產環境應用MySQL主從同步情境:
1. 一般用主庫做為提供業務使用者寫操作(比如:在互連網上寫一條微博,這時候就會寫到mysql資料庫的主庫中)
2. 一般用從庫做為提供業務使用者讀操作(比如:在互連網上,我想看一條微博,這時候裡面提供資料就是MySQL資料庫的從庫中。)
(1)在主伺服器(192.168.1.110)上操作。
[root@Andy ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:5E:6F:A7
inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe5e:6fa7/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:141354 errors:0 dropped:0 overruns:0 frame:0
TX packets:140807 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:142083379 (135.5 MiB) TX bytes:17815696 (16.9 MiB)
Interrupt:193 Base address:0x2000
[root@Andy ~]# vi /etc/my.cnf
[mysqld]在mysqld下添加以上兩行。
server-id = 1
log-bin= Andy -bin
[root@Andy ~]# /etc/init.d/mysqld restart
Shutting down MySQL[ OK ]
Starting MySQL.[ OK ]
[root@Andy ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.1.44 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show master status;
+--------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+--------------------+----------+--------------+------------------+
| Andy-bin.000001 | 106 | | |
+--------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
請記住:File裡的Andy-bin.000001 和 Position 106 。
Mysql> grant replication slave on *.* to 'python'@'192.168.1.%' identified by '123456';
mysql> quit
Bye
在從伺服器(192.168.1.120)上操作:
[root@Andy ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:2B:8E:D2
inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe2b:8ed2/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:180290 errors:0 dropped:0 overruns:0 frame:0
TX packets:146169 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:223411069 (213.0 MiB) TX bytes:15504427 (14.7 MiB)
Interrupt:193 Base address:0x2000
[root@Andy ~]# vi /etc/my.cnf
把server-id = 1 改為:server-id = 2
然後重啟Mysql服務:[root@Andy ~]# /etc/init.d/mysqld restart
Shutting down MySQL........[ OK ]
Starting MySQL.[ OK ]
[root@Andy ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.1.44 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.110', MASTER_PORT=3306, MASTER_USER='python', MASTER_PASSWORD='123456', MASTER_LOG_FILE=' Andy-bin .000001', MASTER_LOG_POS=106;
mysql> start slave;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.110
Master_User: python
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: Andy-bin.000001
Read_Master_Log_Pos: 106
Relay_Log_File: Andy-relay-bin.000002
Relay_Log_Pos: 253
Relay_Master_Log_File: Andy-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: 106
Relay_Log_Space: 410
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:
1 row in set (0.01 sec)
如果: Slave_IO_Running: Yes (主從I/O正確)
Slave_SQL_Running: Yes(主從進程正確)

聯繫我們

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