码迷,mamicode.com
首页 > 数据库 > 详细

Mysql主从复制

时间:2014-11-04 07:03:45      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:mysql主从复制



Mysql主从复制的原理:

    从库以一定的频率去读取主库的二进制日志文件,并按照主库的二进制的记录对从库进行同样的操作,以达到从库与主库内容同步的效果。


Mysql怎么安装我就不多说了,大家可以参考:

http://alipay.blog.51cto.com/7119970/1570454


安装过程中出现错误:

http://alipay.blog.51cto.com/7119970/1571229


本文数据表创建参考文章:

http://alipay.blog.51cto.com/7119970/1570488


主服务器:192.168.159.130

从服务器:192.168.159.131


一、主服务器配置:


[root@rhel64 ~]# service iptables stop;

iptables: Setting chains to policy ACCEPT: filter          [  OK  ]

iptables: Flushing firewall rules:                         [  OK  ]

iptables: Unloading modules:                               [  OK  ]


[root@rhel64 ~]# /etc/rc.d/init.d/mysql stop

Shutting down MySQL..                                      [  OK  ]


[root@rhel64 ~]# vi /etc/my.cnf

[mysqld]

log_bin = mysql-bin #必须开启二进制日志

server_id = 1       #服务器id号,必须是唯一数。


[root@rhel64 ~]# /etc/rc.d/init.d/mysql start

Starting MySQL....                                         [  OK  ]


[root@rhel64 ~]# mysql -uroot -p


mysql> create user gupt11@‘localhost‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)


mysql> grant replication slave on *.* to gupt11@‘192.168.159.131‘ identified by ‘123456‘;

Query OK, 0 rows affected (0.00 sec)


mysql> show master status;

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

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

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

| mysql-bin.000001 |      583 |              |                  |                   |

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

1 row in set (0.00 sec)





二、从服务器配置


[root@gupt11 ~]# /etc/rc.d/init.d/mysql stop

Shutting down MySQL.                                       [  OK  ]


[root@gupt11 ~]# vi /etc/my.cnf

[mysqld]

log_bin = mysql-bin  #必须开启二进制日志

server_id = 2        #确保这个id号没有被其他服务器使用


[root@gupt11 ~]# /etc/rc.d/init.d/mysql start

Starting MySQL....                                         [  OK  ]


[root@gupt11 ~]# mysql -uroot -p

Enter password: 

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

Your MySQL connection id is 1

Server version: 5.6.16-log Source distribution


Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.


Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.


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


mysql> change master to master_host=‘192.168.159.130‘,master_user=‘gupt11‘,master_password=‘123456‘,master_log_file=‘mysql-bin.000001‘,master_log_pos=583;

Query OK, 0 rows affected, 2 warnings (0.03 sec)


##Slave_IO_RunningSlave_SQL_Running必须是Yes,见下面:


mysql> show slave status\G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.159.130

                  Master_User: gupt11

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000001

          Read_Master_Log_Pos: 583

               Relay_Log_File: gupt11-relay-bin.000002

                Relay_Log_Pos: 283

        Relay_Master_Log_File: mysql-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: 583

              Relay_Log_Space: 457

              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: 5c77474b-63d6-11e4-9337-000c2941ae03

             Master_Info_File: /usr/local/mysql/data/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.00 sec)


ERROR: 

No query specified


mysql> 



三、在主服务器测试



mysql> create database scott;

Query OK, 1 row affected (0.00 sec)


mysql> use scott

Database changed

mysql> CREATE TABLE dept

    -> (

    ->  deptno INT PRIMARY KEY,

    ->  dname VARCHAR(14),

    ->  loc VARCHAR(13)

    -> );

Query OK, 0 rows affected (0.02 sec)


mysql> INSERT INTO dept VALUES

    ->  (10,‘ACCOUNTING‘,‘NEW YORK‘);

Query OK, 1 row affected (0.01 sec)


mysql> INSERT INTO dept VALUES

    ->  (20,‘RESEARCH‘,‘DALLAS‘);

Query OK, 1 row affected (0.00 sec)


mysql> INSERT INTO dept VALUES

    ->  (30,‘SALES‘,‘CHICAGO‘);

Query OK, 1 row affected (0.01 sec)


mysql> INSERT INTO dept VALUES

    ->  (40,‘OPERATIONS‘,‘BOSTON‘);

Query OK, 1 row affected (0.00 sec)


mysql> select * from dept;

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

| deptno | dname      | loc      |

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

|     10 | ACCOUNTING | NEW YORK |

|     20 | RESEARCH   | DALLAS   |

|     30 | SALES      | CHICAGO  |

|     40 | OPERATIONS | BOSTON   |

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

4 rows in set (0.00 sec)


mysql> 


四、在从服务器上看


mysql> show databases;

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

| Database           |

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

| information_schema |

| mysql              |

| performance_schema |

| scott              |

| test               |

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

5 rows in set (0.00 sec)


mysql> use scott;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed

mysql> select * from dept;

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

| deptno | dname      | loc      |

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

|     10 | ACCOUNTING | NEW YORK |

|     20 | RESEARCH   | DALLAS   |

|     30 | SALES      | CHICAGO  |

|     40 | OPERATIONS | BOSTON   |

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

4 rows in set (0.00 sec)


mysql> 

 

 

 


本文出自 “xbc's homepage” 博客,请务必保留此出处http://alipay.blog.51cto.com/7119970/1571244

Mysql主从复制

标签:mysql主从复制

原文地址:http://alipay.blog.51cto.com/7119970/1571244

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!