mysql的实时同步-双机互备

来源:互联网 发布:python写文件 编辑:程序博客网 时间:2024/04/30 17:27

安装mysql的实时同步-双机互备

设置方法: 

1: 

A 服务服 (192.168.1.198) 上 用户为 root, 123456 , 同步的数据库为test; 

B 服务服 (192.168.1.62) 上 用户为 root, 123456, 同步的数据库为test; 

 

2:配置 mysql.ini: 

A服务器 

 

#Replication master 

server-id = 10 

log-bin="E:/MySQL/logs/mysql_binary_log" 

binlog-do-db=test 

binlog-Ignore-db=information_schema 

# 单向备份时 A只需上面部分 B只需下面部分 下面部分同样需加server-id 

# Replication slave 

master-host="192.168.1.62" 

master-user=root 

master-password="123456" 

master-port=3306 

master-connect-retry=60 

replicate-do-db=test 

replicate-Ignore-db=information_schema 

 

B服务器 

 

#Replication master 

server-id = 2 

log-bin="c:/mysql5/logs/mysql_binary_log" 

binlog-do-db=test 

binlog-Ignore-db=information_schema 

 

# Replication slave 

master-host="192.168.1.198" 

master-user=root

master-password=123456 

master-port=3306 

master-connect-retry=60 

replicate-do-db=test 

replicate-Ignore-db=information_schema 

 

============================================================= 

解释: 

 

3)binlog-do-db=test 表示需要备份的数据库是test这个数据库, 

如果需要备份多个数据库,那么应该写多行,如下所示: 

binlog-do-db=backup1 

binlog-do-db=backup2 

binlog-do-db=backup3 

 

解释: 

1) server-id=2表示本机器的序号, A,B的server-id 不能相同; 

2)log-bin表示打开binlog,打开该选项才可以通过I/O写到Slave的relay-log,也是可以进行replication的前提; 

其中mysql_binary_log是日志文件的名称,mysql将建立不同扩展名,文件名为mysql_binary_log的几个日志文件. 

3) master-host="192.168.1.23" 表示A做slave时的master为192.168.1.23; 

4) master-user=root 这里表示master上开放的一个有权限的用户,使其可以从slave连接到master并进行复制; 

5) master-password=123456 表示授权用户的密码; 

6) master-port=3306 master上MySQL服务Listen3306端口; 

7) master-connect-retry=60 同步间隔时间; 

8) replicate-do-db=test 表示同步backup数据库; 

 

最后重新启动两台机器的mysql. 


3:赋权

增加一个用户同步使用的帐号:

GRANT FILE ON *.* TO ‘root’@'192.168.1.198' IDENTIFIED BY ‘123456’;

GRANTREPLICATION SLAVE ON *.* TO ‘root’@'192.168.1.198' IDENTIFIED BY ‘123456’;

赋予192.168.1.198也就是Slave机器有File权限,只赋予Slave机器有File权限还不行,还要给它REPLICATION SLAVE的权


4:mysql访问权限问题

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

方法一   

使用phpMyAdmin(图形化管理MySql数据库的工具),这是最简单的,直接用SQL语句修改mysql数据库库的user表,不过别忘了使用PASSWORD函数,插入用户用Insert命令,修改用户用Update命令,删除用Delete命令。在本节后面有数据表user字段的详细介绍。   


方法二   

使用mysqladmin。输入  

  mysqladmin -u root -p oldpassword newpasswd  

执行这个命令后,需要输入root的原密码,这样root的密码将改为newpasswd。同样,把命令里的root改为你的用户名,你就可以改你自己的密码了。

  当然如果你的mysqladmin连接不上mysql

  server,或者你没有办法执行mysqladmin,那么这种方法就是无效的,而且mysqladmin无法把密码清空。   

  下面的方法都在mysql提示符下使用,且必须有mysql的root权限:   


方法三   

  mysql> INSERT INTO mysql.user (Host,User,Password) VALUES('%','system', PASSWORD('manager'));

  mysql> FLUSH PRIVILEGES   

  确切地说这是在增加一个用户,用户名为system,密码为manager。注意要使用PASSWORD函数,然后还要使用FLUSH

  PRIVILEGES来执行确认。




配置:

一、主服务器

1.1、创建一个复制用户,具有replication slave 权限。

mysql>grant replication slave on *.* to 'repl'@'192.168.0.109' identified by 'repl';mysql>flush privileges;




mysql> stop slave;
Query OK, 0 rows affected (0.00 sec)

mysql> change master to 
   -> master_host='192.168.0.107',
   -> master_user='repl',
   -> master_password='repl',
   -> master_log_file='mysql-bin.000001',
   -> master_log_pos=713;

mysql> start slave;
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.0.107
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 1079
Relay_Log_File: mysqld-relay-bin.000004
Relay_Log_Pos: 251
Relay_Master_Log_File: mysql-bin.000001
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: 1079
Relay_Log_Space: 407
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)

ERROR: 
No query specified


0 0
原创粉丝点击