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@Jiechao ~]# ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:0C:29:5E:6F:A7inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe5e:6fa7/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:141354 errors:0 dropped:0 overruns:0 frame:0TX packets:140807 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:142083379 (135.5 MiB) TX bytes:17815696 (16.9 MiB)Interrupt:193 Base address:0x2000[root@Jiechao ~]# vi /etc/f[mysqld]在mysqld下添加以上两行。
server-id = 1log-bin=jiechao-bin[root@Jiechao ~]# /etc/init.d/mysqld restartShutting down MySQL[ OK ]Starting MySQL.[ OK ][root@Jiechao ~]# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.1.44 Source distributionType '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 |+--------------------+----------+--------------+------------------+| jiechao-bin.000001 | 106 | | |+--------------------+----------+--------------+------------------+1 row in set (0.00 sec)请记住:File里的jiechao-bin.000001 和 Position 106 。
Mysql> grant replication slave on *.* to 'python'@'192.168.1.%' identified by '123456';mysql> quitBye在从服务器(192.168.1.120)上操作:[root@jiechao ~]# ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:0C:29:2B:8E:D2inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2b:8ed2/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:180290 errors:0 dropped:0 overruns:0 frame:0TX packets:146169 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:223411069 (213.0 MiB) TX bytes:15504427 (14.7 MiB)Interrupt:193 Base address:0x2000[root@jiechao ~]# vi /etc/f +46把server-id = 1 改为:server-id = 2然后重启Mysql服务:[root@jiechao ~]# /etc/init.d/mysqld restartShutting down MySQL........[ OK ]Starting MySQL.[ OK ][root@jiechao ~]# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 1Server version: 5.1.44 Source distributionType '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='mysql-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 eventMaster_Host: 192.168.1.110Master_User: pythonMaster_Port: 3306Connect_Retry: 60Master_Log_File: jiechao-bin.000001Read_Master_Log_Pos: 106Relay_Log_File: jiechao-relay-bin.000002Relay_Log_Pos: 253Relay_Master_Log_File: jiechao-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 106Relay_Log_Space: 410Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_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@Jiechao ~]# ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:0C:29:5E:6F:A7inet addr:192.168.1.110 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe5e:6fa7/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:142562 errors:0 dropped:0 overruns:0 frame:0TX packets:141697 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:142192520 (135.6 MiB) TX bytes:17915582 (17.0 MiB)Interrupt:193 Base address:0x2000[root@Jiechao ~]# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.1.44-log Source distributionType 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> create database jc;Query OK, 1 row affected (0.00 sec)mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || jc || mysql || test |+--------------------+4 rows in set (0.00 sec)mysql> use jc;Database changedmysql> create table jc(id int(3),name char(10));Query OK, 0 rows affected (0.01 sec)mysql> insert into jc values (001,'imysql');Query OK, 1 row affected (0.00 sec)(提示:我刚刚在主服务器上创建了一个jc数据库和表,我现在在从服务器上查看结果,是否有jc数据库和表?)在从服务器(192.168.1.120)上操作:[root@jiechao ~]# ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:0C:29:2B:8E:D2inet addr:192.168.1.120 Bcast:192.168.1.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe2b:8ed2/64 Scope:LinkUP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1RX packets:180884 errors:0 dropped:0 overruns:0 frame:0TX packets:146585 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000RX bytes:223461833 (213.1 MiB) TX bytes:15555296 (14.8 MiB)Interrupt:193 Base address:0x2000[root@jiechao ~]# mysqlWelcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.1.44 Source distributionType 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> show databases;+--------------------+| Database |+--------------------+| information_schema || jc || mysql || test |+--------------------+4 rows in set (0.00 sec)mysql> use jc;Database changedmysql> select * from jc;+------+--------+| id | name |+------+--------+| 1 | imysql |+------+--------+1 row in set (0.00 sec)(呵呵,结果正确了,主从复制,同步成功。