MySQL数据库主从同步

环境描述

主数据库master  ip:192.168.1.205:3306

从数据库slave     ip:192.168.1.206:3306

 

源码安装Mysql-5.5.46请移步:http://linuxzkq.blog.51cto.com/9379412/1700082,这里不在描述

###############################################################################

配置步骤:

a.主数据库配置步骤

1、开启主数据库的binlog(二进制日志功能),并设置server-id

2、创建用于同步数据的账号rep

3、锁表并查看当前日志名称和位置(pos)

4、备份当前主数据库的全部数据(全备)

5、解锁主数据库,让主数据库继续提供服务

6、继续往主数据库写数据

 

b.从数据库配置步骤

1、把主数据库备份的全备数据恢复到从数据库

2、设置从数据库server-id,并确保这个ID没有被别的MySQL服务所使用。

3、在从数据库上设置主从同步的相关信息,如主数据库服务器的IP地址、端口号、同步账号、密码、binlog文件名、binlog位置(pos)点

4、开始主从同步start slave;

5、查看是否同步成功,show slavestatus\G;

############################################################################

 

 

 

主数据库配置:

##################################################################

开启主数据库的binlog(二进制日志功能),并设置server-id

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

server-id       = 1

log-bin=mysql-bin

[root@web1 ~]# egrep "server-id|log-bin" /etc/my.cnf

server-id       = 1

log-bin=mysql-bin

################################################################## 

重启数据库

[root@web1 ~]# service mysqld restart

Shutting down MySQL.......... SUCCESS!

Starting MySQL.......... SUCCESS!

[root@web1 ~]# netstat -tunlp|grep mysqld

tcp        0      0 0.0.0.0:3306                0.0.0.0:*                   LISTEN      16530/mysqld 

[root@web1 ~]# lsof -i:3306

COMMAND   PID  USER  FD   TYPE DEVICE SIZE/OFF NODENAME

mysqld      16530 mysql 11u  IPv4  52627     0t0  TCP *:mysql (LISTEN)

################################################################## 

查看是否记录二进制日志

[root@web1 ~]# ll /application/data/mysql-bin.000001

-rw-rw----. 1 mysql mysql 2629 Oct  2 15:35 /application/data/mysql-bin.000001  #有此文件,证明在记录二进制日志

################################################################## 

主数据库中创建测试用的数据库和表

mysql> create database test;

Query OK, 1 row affected (0.02 sec)

mysql> use test;

Database changed

mysql> create table test(id int(3),namechar(10));

Query OK, 0 rows affected (0.14 sec)

mysql> insert into test values(001,'mytest');

Query OK, 1 row affected (0.14 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.02 sec)

################################################################## 

查看是否开启binlog(二进制日志功能)

mysql> show variables like 'log_bin';

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

| Variable_name | Value |

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

| log_bin     | ON   |

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

1 row in set (0.00 sec)

##################################################################  

创建用于同步数据的账号rep

mysql> grant replication slave on *.* to 'rep'@'192.168.1.%'identified by 'rep123456';

mysql> flush privileges;

mysql> select user,host from mysql.user;                

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

| user | host      |

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

| root | 127.0.0.1   |

| rep  | 192.168.1.% |

| root | localhost   |

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

3 rows in set (0.04 sec)

##################################################################

锁表

mysql> flush table withread lock; 

Query OK, 0 rows affected (0.00 sec)

##################################################################

查看当前二进制日志名称和位置(pos)

mysql> show master status;

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

| File         | Position | Binlog_Do_DB |Binlog_Ignore_DB |

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

| mysql-bin.000001 |    107 |         |             |

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

1 row in set (0.03 sec)

##################################################################

备份当前主数据库的数据

[root@web1 ~]# mysqldump-uroot  -p87003891214 -A -B -F--master-data=2 --events | gzip > /tmp/mysql_bak.$(date +%F).sql.gz 

[root@web1 ~]# ll /tmp

total 156

drwxrwxrwt. 2 tomcat tomcat   4096 Oct 2 09:33 hsperfdata_tomcat

-rw-r--r--. 1 root   root  151317 Oct  2 20:08mysql_bak.2015-10-02.sql.gz

-rwxrwxrwt. 1 root   root       0 Aug 13 11:55 yum.log

解锁主数据库

mysql> unlock tables;

Query OK, 0 rows affected (0.00 sec)

################################################################## 

 

 

从数据库配置:

######################################################

把主数据库备份的全备数据恢复到从数据库

 

[root@web1 ~]# scp /tmp/mysql_bak.2015-10-02.sql.gzroot@192.168.1.206:~

root@192.168.1.206's password:

mysql_bak.2015-10-02.sq 100% 148KB 147.8KB/s   00:00

[root@web2 ~]# ll mysql_bak.2015-10-02.sql.gz

-rw-r--r--. 1 root root 151317 Aug 19 07:37 mysql_bak.2015-10-02.sql.gz

[root@web2 ~]# gzip -d mysql_bak.2015-10-02.sql.gz

[root@web2 ~]# mysql -uroot -p87003891214 < mysql_bak.2015-10-02.sql

[root@web2 ~]# mysql -uroot -p87003891214 -e "showdatabases;"

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

| Database        |

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

| information_schema |

| mysql         |

| performance_schema |

| test          |

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

[root@web2 ~]# mysql -uroot -p87003891214 -e "usetest;select * from test;"       

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

| id   | name  |

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

|    1 | mytest|

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

###############################################################

设置从数据库server-id,并确保这个ID没有被别的MySQL服务所使用

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

server-id       = 2

[root@web2 ~]# grep "server-id" /etc/my.cnf

server-id       = 2

##############################################################

在从数据库上设置主从同步的相关信息,如主数据库服务器的IP地址、端口号、同步账号、密码、binlog文件名、binlog位置(pos)点

mysql> CHANGE MASTER TO

MASTER_HOST='192.168.1.205',

MASTER_PORT=3306,

MASTER_USER='rep',

MASTER_PASSWORD='rep123456',

MASTER_LOG_FILE='mysql-bin.000001',

MASTER_LOG_POS=107;

#############################################################

开始主从同步start slave;

mysql> start slave;

查看是否同步成功,show slave status\G;

mysql> show  slavestatus\G;

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

Slave_IO_State: Waiting formaster to send event

Master_Host: 192.168.1.205

Master_User: rep

Master_Port: 3306

Connect_Retry: 60

Master_Log_File:mysql-bin.000008

Read_Master_Log_Pos: 107

Relay_Log_File:web2-relay-bin.000014

Relay_Log_Pos: 253

Relay_Master_Log_File:mysql-bin.000008

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

Relay_Log_Space: 451

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

1 row in set (0.01 sec)

 

ERROR:

No query specified

其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,代表主从同步成功了。

#############################################################

继续往test表中插入数据

mysql> use test;

Database changed

mysql> insert into test values(002,'mytest2');

Query OK, 1 row affected (0.01 sec)

 

mysql> insert into test values(003,'mytest3');

Query OK, 1 row affected (0.05 sec)

 

mysql> insert into test values(004,'mytest4');

Query OK, 1 row affected (0.04 sec)

 

查看从数据库的表

 

mysql> use test;

Database changed

mysql> select * from test;

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

| id   | name   |

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

|    1 | mytest |

|    2 | mytest2 |

|    3 | mytest3 |

|    4 | mytest4 |

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

4 rows in set (0.00 sec)

 

posted @ 2015-10-05 09:09  linuxzkq  阅读(195)  评论(0编辑  收藏  举报