192.168.20.63 duwers63.space

192.168.20.60 duwers60.space

关闭iptables selinux

主从数据库版本一致

拓扑

mysql主从同步_第1张图片


主库配置【192.168.20.63

查看版本

[root@duwers63 ~]# mysql -uroot -p123456

mysql> show variables like '%version%';                   #查看版本

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

| Variable_name           | Value               |

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

| protocol_version        | 10                  |

| version                 | 5.1.73-log          |

| version_comment         | Source distribution |

| version_compile_machine | x86_64              |

| version_compile_os      | redhat-linux-gnu    |

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

5 rows in set (0.01 sec)

创建需要同步的库

mysql> create database duwers63;

mysql> use duwers63

mysql> create table test1(id int);

mysql> show databases;

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

| Database           |

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

| information_schema |

| duwers63           |

| mysql              |

| ucenter            |

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

4 rows in set (0.00 sec)

 

停止mysql服务

[root@duwers63 ~]# service mysqld stop

修改配置文件

[root@duwers63 ~]# vim /etc/my.cnf

添加

log-bin=mysqllog                        #开启二进制日志

server-id=63                                #本机数据库标识

binlog-do-db=duwers63    #二进制要同步的db

 

[root@duwers63 ~]# cat /etc/my.cnf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommendedto prevent assorted security risks

symbolic-links=0

log-bin=mysqllog

server-id=63

binlog-do-db=tree

 

[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

[root@duwers63 ~]#

重启mysql服务

[root@duwers63 ~]# service mysqld start

[root@duwers63 ~]# mysql -uroot -p123456

 

授权同步账户

mysql> grant replication slave on *.* [email protected] identified by "123456";

查看主库状态

mysql> show master status;

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

| File            | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

| mysqllog.000001 |      257 | tree         |                  |

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

1 row in set (0.00 sec)

 

查看二进制日志状态

[root@duwers63 ~]# ls /var/lib/mysql/

duwers63 ibdata1  ib_logfile0  ib_logfile1 mysql  mysqllog.000001  mysqllog.index  mysql.sock ucenter

[root@duwers63 ~]#

 

确保同步复制前两个数据库的一致性

[root@duwers63 ~]# mysqldump -u root-p123456 -A >all.sql

[root@duwers63 ~]# scp -P 44968 [email protected]:/root

 

从服务器配置【192.168.20.60

[root@duwers60 ~]# service mysqld start

测试连通

[root@duwers60 ~]# mysql -u slave -h192.168.20.63 -p123456 -A

mysql> show databases;

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

| Database           |

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

| information_schema |

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

1 row in set (0.00 sec)

 

导入数据库,和主数据库保持一致性

[root@duwers60 ~]# mysql -u root -p

修改配置文件

[root@duwers60 ~]# vim /etc/my.cnf

添加

server-id=60

master-host=192.168.20.63

master-user=slave

master-password=123456

重启服务

[root@duwers60 ~]# service mysqld restart

检查从服务器复制功能状态

[root@duwers60 ~]# mysql -u root -p123456

 

mysql> show slave status \G

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

               Slave_IO_State: Waiting formaster to send event

                  Master_Host: 192.168.20.63

                  Master_User: slave

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysqllog.000001

         Read_Master_Log_Pos: 257

               Relay_Log_File:mysqld-relay-bin.000002

                Relay_Log_Pos: 401

       Relay_Master_Log_File: mysqllog.000001

             Slave_IO_Running: Yes                              #负责与主机的IO通信Yes说明从服务器安装成功

           Slave_SQL_Running: Yes                             #负责自己slave  mysql进程  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: 257

              Relay_Log_Space: 557

              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.00 sec)

 

测试数据的同步

在主服务器master上写数据

mysql> use duwers63

mysql> show tables;

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

| Tables_in_duwers63 |

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

| test1              |

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

1 row in set (0.00 sec)

 

mysql> insert into test1 values(1);

 

在从服务器slave上读数据

 

mysql> use duwers63

mysql> select * from test1;

+------+

| id  |

+------+

|   1 |

+------+

1 row in set (0.00 sec)