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(主從進程正確)


(3)測試主從數據同步:在主服務器(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:142562 errors:0 dropped:0 overruns:0 frame:0


TX packets:141697 errors:0 dropped:0 overruns:0 carrier:0


collisions:0 txqueuelen:1000


RX bytes:142192520 (135.6 MiB)  TX bytes:17915582 (17.0 MiB)


Interrupt:193 Base address:0x2000


[root@Andy  ~]# mysql


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


Your MySQL connection id is 3


Server version: 5.1.44-log Source distribution


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


mysql> create database Andy ;


Query OK, 1 row affected (0.00 sec)


mysql> show databases;


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


| Database           |


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


| information_schema |


| Andy                 |


| mysql              |


| test               |


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


4 rows in set (0.00 sec)


mysql> use  Andy ;


Database changed


mysql> create table  Andy (id int(3),name char(10));


Query OK, 0 rows affected (0.01 sec)


mysql> insert into  Andy  values (001,'imysql');


Query OK, 1 row affected (0.00 sec)


(提示:我剛剛在主服務器上創建了一個Andy 數據庫和表,我現在在從服務器上查看結果,是否有Andy數據庫和表?)


在從服務器(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:180884 errors:0 dropped:0 overruns:0 frame:0


TX packets:146585 errors:0 dropped:0 overruns:0 carrier:0


collisions:0 txqueuelen:1000


RX bytes:223461833 (213.1 MiB)  TX bytes:15555296 (14.8 MiB)


Interrupt:193 Base address:0x2000


[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 databases;


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


| Database           |


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


| information_schema |


| Andy                 |


| mysql              |


| test               |


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


4 rows in set (0.00 sec)


mysql> use  Andy ;


Database changed


mysql> select * from  Andy ;


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


| id   | name   |


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


|    1 | imysql |


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


1 row in set (0.00 sec)


你可能感兴趣的:(MySQL主從同步原理和部署)