Mysql 主从复制配置以及相关的操作

来源:互联网 发布:淘宝品牌男装店 编辑:程序博客网 时间:2024/06/16 23:15

Mysql 主从复制配置

1、配置Master主服务器

(1)在Master MySQL上创建一个用户‘repl’,并允许其他Slave服务器可以通过远程访问Master,通过该用户读取二进制日志,实现数据同步。

1 mysql>create user repl; //创建新用户2 //repl用户必须具有REPLICATION SLAVE权限,除此之外没有必要添加不必要的权限,密码为mysql。说明一下192.168.0.%,这个配置是指明repl用户所在服务器,这里%是通配符,表示192.168.0.0-192.168.0.255的Server都可以以repl用户登陆主服务器。当然你也可以指定固定Ip。3 mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.0.%' IDENTIFIED BY 'mysql';

(2)找到MySQL安装文件夹修改my.Ini文件。mysql中有好几种日志方式,这不是今天的重点。我们只要启动二进制日志log-bin就ok。

 在[mysqld]下面增加下面几行代码

1 server-id=1   //给数据库服务的唯一标识,一般为大家设置服务器Ip的末尾号2 log-bin=master-bin3 log-bin-index=master-bin.index

(3)查看日志

mysql> SHOW MASTER STATUS;
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000001 | 1285 | | |
+-------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

重启MySQL服务

3、配置Slave从服务器(windows)

(1)找到MySQL安装文件夹修改my.ini文件,在[mysqld]下面增加下面几行代码

1 [mysqld]2 server-id=23 relay-log-index=slave-relay-bin.index4 relay-log=slave-relay-bin 

重启MySQL服务

(2)连接Master

change master to master_host='192.168.0.104', //Master 服务器Ip
master_port=3306,
master_user='repl',
master_password='mysql', 
master_log_file='master-bin.000001',//Master服务器产生的日志
master_log_pos=0;

(3)启动Slave

start slave;

4、Slave从服务器(Ubuntu)

(1)找到MySQL安装文件夹修改my.cnf文件,vim my.cnf

 s

 

(2) ./support-files/myql.server restart 重启MySQL服务  ,  ./bin/mysql 进入MySQL命令窗口 

(3)连接Master

change master to master_host='192.168.0.104', //Master 服务器Ip
master_port=3306,
master_user='repl',
master_password='mysql', 
master_log_file='master-bin.000001',//Master服务器产生的日志
master_log_pos=0;

(4)启动Slave

start slave;

OK所有配置都完成了,这时候大家可以在Master Mysql 中进行测试了,因为我们监视的时Master mysql  所有操作日志,所以,你的任何改变主服务器数据库的操作,都会同步到从服务器上。创建个数据库,表试试吧。。。

Mysql数据库的导出sql语句Dump,导入到slave中

mysql>flush tables with read lock;  将mysql的数据库锁表,仅仅允许读,以保证数据的一致性

[root@localhost ~]# mysqldump -uroot -p cuichunchi(库) > blog.sql 将blog这个库以脚本的形式导出来,便于导入slave

mysql> unlock tables; 解锁

在slave机器中操作

使用mysql目录中bin里面的mysql命令进行导入

[root@wordpress ~]# mysql -u root -p123456 < blog.sql


Master-Master架构模式:

之间不会重复复制,因为每个master都有唯一的server-id,最终不是两个master同时写入,不然会出现更新问题,会出现同时更新记录操作,

所以只会开启一台来写入,另一个master来充当slave(stand by)来读。这就避免更新的问题。正常情况下,需要维护,一下就是停机维护,操作执行master的切换操作:

(1):停止当前Master的所有写操作

(2):在Master上执行set global read_only=1,同时更新MySQL配置文件中相应的配置,避免重启时失效

(3):在Master上执行show master status 已记录Binary log坐标

(4):使用master上的binary log 坐标,在stand by 的master上执行select master_pos_wait(),等待stand by master的Binarg log跟上Master

的Binary log。

(5):在stand by master 开启写入时,设置read_only=0.

(6):修改应用程序的配置,使其写入到新的master。


原创粉丝点击