一.主从的作用:

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)