MySQL主从复制

1、修改主库配置文件my.cnf,开启二进制日志、修改server-id及要同步的数据库名字

server-id=1

log-bin=binlog

binlog-do-db=relearn

binlog-ignore-db=mysql

binlog-ignore-db=test

binlog-ignore-db=information_schema


2、重启主库让更改生效,并赋予从库权限帐号、允许用户在主库上读取二进制日志。

[root@SRV1 ~]# service mysqld restart

Stopping mysqld:   [  OK  ]

Starting mysqld:   [  OK  ]

mysql> GRANT replication slave ON *.* TO  'adminsrv'@'192.168.2.105' IDENTIFIED BY '678srv1';

Query OK, 0 rows affected (0.00 sec)


3、锁主库表、并查看主库信息

mysql> flush tables with read lock;

Query OK, 0 rows affected (0.00 sec)

mysql> show master status;

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

| File         |  Position | Binlog_Do_DB | Binlog_Ignore_DB              |

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

| binlog.000005 |    220| relearn |  mysql,test,information_schema

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

1 row in set (0.00 sec)


4、把主库的relearn数据库备份,传输到从库上

[root@SRV1 ~]# mysqldump –master-data -uroot -p relearn  > relearn.sql

[root@SRV1 ~]# scp  relearn.sql [email protected]:/root/


5、登录从库,建立relearn数据库,并导入主库上传来的sql脚本

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

mysql> CREATE DATABASE relearn;

Query OK, 1 row affected (0.00 sec)

[root@SRV2 ~]# mysql -uroot -p relearn < ./relearn.sql


6、解锁主库表

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)


7、修改从库配置文件my.cnf,并在从库上设置同步、查看从库的status状态

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

server-id=2

replicate-do-db=relearn

replicate-ignore-db=mysql

replicate-ignore-db=information_schema

replicate-ignore-db=test


mysql> slave stop;

Query OK, 0 rows affected (0.00 sec)


mysql> change master tomaster_host='192.168.2.109',master_user='adminsrv',master_password='678srv1',master_log_file='binlog.000005',master_log_pos=220;

Query OK, 0 rows affected (0.03 sec)


mysql> slave start;

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.2.109

              Master_User: adminsrv

              Master_Port: 3306

            Connect_Retry: 60

          Master_Log_File: binlog.000005

      Read_Master_Log_Pos: 220

           Relay_Log_File: mysqld-relay-bin.000006

            Relay_Log_Pos: 354

    Relay_Master_Log_File: binlog.000005

Slave_IO_Running: Yes

Slave_SQL_Running: Yes

           Replicate_Do_DB:relearn

      Replicate_Ignore_DB: mysql,information_schema,test

       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: 220

          Relay_Log_Space: 354

          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

1 row in set (0.00 sec)


8、测试:

查看主库表,并插入条记录:

[root@SRV1 ~]# mysql -uroot -proot

mysql> select * from relearn.student;

Empty set (0.00 sec)


mysql> insert into relearn.student values(6,'RELEARN','男','','');

Query OK, 1 row affected, 1 warning (0.00 sec)


查看从库:(已经更新)

[root@SRV1 ~]# mysql -uroot -proot

mysql> select * from relearn.student;

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

| num | name    | sex  | birthday            | address |

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

|   6 | RELEARN | 男 |  0000-00-00 00:00:00 |         |

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

1 row in set (0.00 sec)