ubuntu上的mysql数据库双机备份设置
配置环境:
myslq 5.5.3 + ubuntu server 12.04
一、配置MySQL主服务器(192.168.0.1)
1.增加一个账号专门用于同步
|
1
|
mysql>grant replication slave on *.* to user@192.168.0.2 identified by '123456'; |
2.接下来备份数据,首先执行如下SQL语句:
|
1
2
3
|
mysql>flush tables with read lock; mysql>mysqldump -u root -p esmartgo > /home/esmartgo.sql |
3.然后将这些数据拷贝到mysql从服务器(192.168.0.2)上,恢复数据,设置好正确的权限及属主等;之后,执行"UNLOCK TABLES"语句来释放锁。
|
1
|
mysql>unlock tables; |
4.修改mysql配置文件
|
1
|
vi /etc/my.cnf |
5.编辑配置文件,在[mysqld]部分添加下面内容
|
1
2
3
4
|
server-id=1 #设置服务器id,为1表示主服务器,注意:如果原来的配置文件中已经有这一行,就不用再添加了。log_bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。binlog-do-db=esmartgo #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行binlog-ignore-db=mysql #不同步mysql系统数据库 |
6.重启mysql
|
1
|
service mysqld restart #重启MySQL |
7.进入mysql控制台
|
1
|
mysql -u root -p #进入mysql控制台 |
8.查看主服务器
|
1
|
show master status; |
显示如下信息
|
1
2
3
4
5
6
|
+------------------+----------+--------------+------------------+| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |+------------------+----------+--------------+------------------+| mysql-bin.000001 | 40538 | esmartgo | |+------------------+----------+--------------+------------------+1 row in set (0.00 sec) |
注意:这里记住File的值:mysql-bin.000001和Position的值:40538,后面会用到。
二、配置MySQL从服务器(192.168.0.2)
1.修改mysql配置文件
vi /etc/my.cnf
2.编辑配置文件,在[mysqld]部分添加下面内容
|
1
2
3
4
|
server-id=2 #配置文件中已经有一行server-id=1,修改其值为2,表示为从数据库。log_bin=mysql-bin #启动MySQ二进制日志系统,注意:如果原来的配置文件中已经有这一行,就不用再添加了。replicate-do-db=esmartgo #需要同步的数据库名,如果有多个数据库,可重复此参数,每个数据库一行replicate-ignore-db=mysql #不同步mysql系统数据库 |
3.重启mysql
service mysqld restart #重启MySQL
4.进入mysql控制台
mysql -u root -p #进入mysql控制台
5.停止slave同步进程
|
1
|
slave stop; #停止slave同步进程 |
6.修改master配置属性
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
change master to master_host='192.168.0.1', master_port = 3306, master_user='user', master_password='123456', master_log_file='mysql-bin.000001', master_log_pos=40358; |
7.查看slave同步信息
|
1
|
show slave status\G |
8.显示结果( Slave_IO_Running: Yes Slave_SQL_Running: Yes说明已经同步成功)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 101.227.252.54 Master_User: user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 40538 Relay_Log_File: mysqld-relay-bin.000002 Relay_Log_Pos: 36464 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: esmartgo 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: 40538 Relay_Log_Space: 36621 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: 0Master_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: 11 row in set (0.00 sec) |
浙公网安备 33010602011771号