posts - 36, comments - 30, trackbacks - 0, articles - 3

MySQL主从复制配置

Posted on 2016-02-23 20:41 笑看人生 阅读(347) 评论(0)  编辑  收藏
@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);
 主服务器  OS:CentOS   IP:192.168.8.130
 从服务器  OS:CentOS   IP:192.168.8.131

在主,从服务器上安装MySQL,安装方法如下:
[root@localhost Desktop]$ rpm -qa | grep mysql
   mysql-libs-5.1.73-5.el6_6.x86_64
[root@localhost Desktop]# rpm -e mysql-libs-5.1.73-5.el6_6.x86_64 --nodeps
[root@localhost Desktop]# yum -y install mysql-server mysql mysql-devel

启动MySQL
[root@localhost Desktop]# service mysqld start

#可以设置MySQL开机启动,运行命令chkconfig mysqld on

#给root账号设置密码
[root@localhost Desktop]# mysqladmin -u root password 'root'
[root@localhost Desktopps]# mysql -u root -p
给从服务器(192.168.8.131)授权,并且给从服务器创建访问主服务器的账号和密码 admin
mysql> grant replication slave on *.* to 'admin'@'192.168.8.131' identified by 'admin';
创建数据库contract
mysql> create database contract;
mysql>quit;

复制MySQL数据库配置模版覆盖/etc/my.cnf
[root@localhost Desktopps]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf   
[root@localhost Desktopps]#vi /etc/my.cnf
设置以下三个值

log-bin=mysql-bin   #指定从服务器读取的日志文件
server-id = 1        #主服务器必须设定为1,从服务器的值>1
binlog-do-db=contract #对contract数据库的操作日志会记录到mysql-bin

#原理:MySQL主从复制的原理是主服务器把对指定数据库操作的日志写到指定的日志文件中,从服务器
            读取这个日志文件,写到从服务器的指定日志文件中,然后在从服务器重新执行日志文件。

配置完之后,重启MySQL
[root@localhost Desktopps]#service mysqld restart
Stopping mysqld:                                          [  OK  ]
Starting mysqld:                                           [  OK  ]

[root@localhost Desktopps]# mysql -u root -p
查看主服务器的状态
mysql> show master status\G;
*************************** 1. row ***************************
                    File: mysql-bin.000005
              Position: 106
     Binlog_Do_DB: contract
Binlog_Ignore_DB: 
1 row in set (0.00 sec)

这里记好File和Position的值,配置从服务器的时候需要用到。File就是从服务器需要读取的日志文件,Position表示从日志文件的什么位置开始读起。

 下面开始配置从服务器
[root@localhost Desktop]# mysqladmin -u root password 'root'
[root@localhost Desktopps]# mysql -u root -p
创建数据库contract
mysql> create database contract;
mysql>quit;
[root@localhost Desktopps]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf   
[root@localhost Desktopps]#vi /etc/my.cnf
设置以下两个值

log-bin=mysql-bin   #指定主服务器读取的日志文件
server-id = 2       #主服务器必须设定为1,从服务器的值>1

[root@localhost Desktopps]# mysql -u root -p
mysql> CHANGE MASTER TO MASTER_HOST='192.168.8.130', MASTER_PORT=3306,
            MASTER_USER='admin', MASTER_PASSWORD='admin',
            MASTER_LOG_FILE='mysql-bin.000005'MASTER_LOG_POS=106; 
启动从服务器同步
mysql>start slave;
mysql>show slave status\G;

Slave_IO_Running: YES
Slave_SQL_Running: YES

如果输出以上内容,则表示MySQL主从复制配置成功。

验证
在主服务器上运行 
[root@localhost Desktopps]# mysql -u root -p
mysql> use contract;
Database changed
mysql> show tables;
Empty set (0.04 sec)

在从服务器上运行
[root@localhost Desktopps]# mysql -u root -p
mysql> use contract;
Database changed
mysql> show tables;
Empty set (0.04 sec)

确定主从服务器的数据库contract的下面都没有表。
在主服务器上运行建表命令,并往表里插入一条记录:
 mysql> create table `user` (`id` int not null auto_increment,`name` varchar (60),`password` varchar (20),`role` int not null,`email` varchar (30),`alertday` int,primary key (`id`));
Query OK, 0 rows affected (0.36 sec)
 mysql> insert into `user` (`name`,`password`,`role`,`email`,`alertday`) values('admin','admin',0,'xxxx@xxx.com',30);
Query OK, 1 row affected (0.08 sec)

在从服务器上运行查询语句。
mysql> select * from user;
+----+-------+----------+------+--------------+----------+
| id | name  | password | role | email        | alertday |
+----+-------+----------+------+--------------+----------+
|  1 | admin | admin    | 0    | xxxx@xxx.com |       30 |
+----+-------+----------+------+--------------+----------+
1 row in set (0.01 sec)

从输出结果可以看出,主服务器上的数据被同步到从服务器上了。

通过搭建MySQL主从复制结构,可以提高数据的安全性,同时可以实现读写分离,让写操作在主服务器上进行,
读操作在从服务器上进行,可以分担主服务器的负担。但是如果当主服务器宕机之后,数据库就只能提供
读操作了,不能做到故障转移,这时候,主主复制就应运而生了,有时间整理一下主主复制的配置。




@import url(http://www.blogjava.net/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

只有注册用户登录后才能发表评论。


网站导航: