数据库–MySQL主从同步备份设置

原创作品,转载请标明:http://blog.geekcome.com/archives/249

1首先设置主数据库

修改配置文件中

1 # 日志文件名
2 log-bin = /var/log/mysql/mysql-bin.log
3 # 主数据库端ID号
4 server-id = 1

 2并查看配置文件中,确保已经被注释,否则即赋予账号远程登录的账号也无法登录成功,会提示:解决ERROR 2003 (HY000): Can’t connect to MySQL

1 #bind-address       = 127.0.0.1

 3重启mysql服务器,然后新建账户并赋予远程登录的权限。

1 # 创建slave帐号slave_account,密码sa
2 mysql>grant replication slave on *.* to 'slave_account'@'%' identified by 'sa';
3  
4 # 更新数据库权限
5 mysql>flush privileges;

 4查看master的状态

1 mysql> show master status;
2 +------------------+----------+--------------+------------------+
3 | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
4 +------------------+----------+--------------+------------------+
5 | mysql-bin.000001 |      196 |              |                  |
6 +------------------+----------+--------------+------------------+
7 1 row in set

到这里,请不要再操作master数据库,直到从数据库配置完成。

5修改slave数据库配置文件

1 # 从数据库端ID号
2 server-id =2

 6执行同步命令

1 # 执行同步命令,设置主数据库ip,同步帐号密码,同步位置
2 mysql>change master to master_host='192.168.137.8',master_user='slave_account',master_password='sa',master_log_file='mysql-bin.000001',master_log_pos=196;
3  
4 # 开启同步功能
5 mysql>start slave;

 7查看slave数据库的状态

01 mysql> show slave status\G;
02 *************************** 1. row ***************************
03                Slave_IO_State: Waiting for master to send event
04                   Master_Host: 192.168.137.8
05                   Master_User: slave_account
06                   Master_Port: 3306
07                 Connect_Retry: 60
08               Master_Log_File: mysql-bin.000001
09           Read_Master_Log_Pos: 291
10                Relay_Log_File: mysqld-relay-bin.000002
11                 Relay_Log_Pos: 437
12         Relay_Master_Log_File: mysql-bin.000001
13              Slave_IO_Running: Yes
14             Slave_SQL_Running: Yes
15               Replicate_Do_DB:
16           Replicate_Ignore_DB:
17            Replicate_Do_Table:
18        Replicate_Ignore_Table:
19       Replicate_Wild_Do_Table:
20   Replicate_Wild_Ignore_Table:
21                    Last_Errno: 0
22                    Last_Error:
23                  Skip_Counter: 0
24           Exec_Master_Log_Pos: 291
25               Relay_Log_Space: 594
26               Until_Condition: None
27                Until_Log_File:
28                 Until_Log_Pos: 0
29            Master_SSL_Allowed: No
30            Master_SSL_CA_File:
31            Master_SSL_CA_Path:
32               Master_SSL_Cert:
33             Master_SSL_Cipher:
34                Master_SSL_Key:
35         Seconds_Behind_Master: 0
36 Master_SSL_Verify_Server_Cert: No
37                 Last_IO_Errno: 0
38                 Last_IO_Error:
39                Last_SQL_Errno: 0
40                Last_SQL_Error:
41   Replicate_Ignore_Server_Ids:
42              Master_Server_Id: 1
43 1 row in set (0.00 sec)

到这里主从数据库已经可以同步了,在主数据库的操作会自动同步到从数据库。

猜你喜欢

转载自blog.csdn.net/yming0221/article/details/23376691