前言:Mysql在生产环境中的宕机率特别的高,一般我们在该机器上只安装mysql,不作他用。数据库本 身也是极易产生瓶颈的地方。Master:主(用于写入数据) Slave:从(用于读取数据),也是 实现读写分离的重要举措。国内很多的视频直播网站、bilibili等弹幕类的网站,在视频框架上 做了个透明的弹幕, 后台数据库每秒发生数以万计的读写操作,后台运维人员真是想 死的心都有。。。哈哈。。废话不多说,开始部署……
准备前的步骤:关闭selinux,关闭防火墙、已编译好的mysql5.5
实验环境:Centos6.5 IP:192.168.1.1 Mysql主
Centos6.5 IP:192.168.1.2 Mysql从
配置主
[[email protected] ~]# mysql -u root -predhat //mysql登录用户及密码
[[email protected] ~]# show dabaseses; //显示数据库
[[email protected] ~]# use mysql; //使用mysql库
[[email protected] ~]# show tables; //显示表
[[email protected] ~]# desc user; //查看user表的结构
[[email protected] ~]# select Host,User,Password form mysql.user; //在user表中查询主机,用户密码字段
[[email protected] ~]# insert into mysql.user(Host,User,Password) values(‘localhost‘,‘baidu‘,password(‘123.com‘)); //插入一条数据,用户百度,密码123.com
[[email protected] ~]# grant replication slave on *.* to ‘baidu‘@‘192.168.1.2‘ identified by ‘123.com‘ with grant option; //授权给192.168.1.2用户可以使用百度这个用户,密码123.com来进行复制操作
[[email protected] ~]# create database test; //创建测试的库,库名为test
[[email protected] ~]# vim /etc/my.cnf
大约49行log-bin=mysql-bin处
添加:binlog-do-db=test //将test库写入二进制日志
binlog-ignore-db=mysql //忽视mysql库写入二进制日志
修改:server-id = 1 每台做主从或主主的server id的值应唯一。
[[email protected] ~]# /etc/init.d/mysqld restart
[[email protected] ~]# mysql -u root -predhat
mysql> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 | 107 | test | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec) //上面两个值将在从Mysql中用到。。
下面配置从mysql
[[email protected] ~]# vim /etc/my.cnf
大约49行log-bin=mysql-bin处
添加:replicate-do-db=test //复制的库名test
replicate-ignore-db=mysql //不复制的库名mysql
修改:server-id = 2 值唯一
[[email protected] ~]# /etc/init.d/mysqld restart
[[email protected] ~]# mysql -u root -predhat
[[email protected] ~]# create database test; //创建和主上面一样的同步的库名
[[email protected] ~]# slave stop;
[[email protected] ~]# change master to master_host=‘192.168.1.1‘, master_user=‘baidu‘, master_password=‘123.com‘, master_log_file=‘mysql-bin.000005‘, master_log_pos=107;
//指定主的IP,可复制用户及密码。上面两个值在每次重启主Mysql都会变。如果重启,则需要重新change master to 了
[[email protected] ~]# slave start; //开启复制
[[email protected] ~]# show slave status\G
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.1
Master_User: baidu
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 107
Relay_Log_File: localhost-relay-bin.000003
Relay_Log_Pos: 253
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test
Replicate_Ignore_DB: mysql
两个Yes表示主从配置正确……
接下来我们到主mysql的test库上面创建一个表,再到从mysql上面看看是否同步过来了。
主mysql
mysql> use test;
Database changed
mysql> create table student(id int(11) not null, primary key(id));
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+----------------+
| Tables_in_user |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
从mysql
mysql> use test;
Database changed
mysql> show tables;
+----------------+
| Tables_in_user |
+----------------+
| student |
+----------------+
1 row in set (0.00 sec)
可以看到student表已经成功同步过来了,至此Mysql主从实验已完成。
PS:常见错误,防火墙阻塞了端口,授权密码填写错误 mysql-bin错误 每次重启服务会变更 change mater to 需要重新敲。
Mysql编译安装的都有一个小小的BUG,不管什么错误导致mysql重启失败都会报PID丢失,一般都是你的配置文件写错了,或者查找日志文件进行拍错。。