时间:2021-07-01 10:21:17 帮助过:24人阅读
1 2 3 4 5 6 7 8 9 10 11 12 13 | master服务器配置: vi /usr/local/mysql/etc/my.cnf [mysqld] server-id=202 #设置服务器唯一的id,默认是1,我们设置ip最后一段,slave设置203 log-bin=mysql-bin # 启用二进制日志 binlog-ignore-db = mysql,information_schema #忽略写入binlog的库 slave服务器配置: vi /usr/local/mysql/etc/my.cnf [mysqld] server-id=203 replicate-do-db = abc #只同步abc库 slave-skip-errors = all #忽略因复制出现的所有错误 |
3、重启主从服务器mysql
Shell1 | /etc/init.d/mysqld restart |
4、在主服务器上建立帐户并授权slave
Shell1 2 | mysql> mysql -u root -p123.com mysql> GRANT REPLICATION SLAVE ON *.* to ‘sync’@‘192.168.1.2’ identified by ‘1234.com’; #replication:复制 |
5、查看主数据库状态
Shell1 2 3 4 5 6 | mysql> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000002 | 263 | | | +------------------+----------+--------------+------------------+ |
6、配置从数据库
Shell1 2 3 4 5 6 7 | mysql> change master to -> master_host=‘192.168.0.202‘, -> master_user=‘sync‘, -> master_password=‘1234.com‘, -> master_log_file=‘mysql-bin.000002‘, -> master_log_pos=263; #Log和pos是master上随机获取的。这段也可以写到my.cnf里面。 |
7、启动slave同步进程并查看状态
Shell1 | mysql> start slave; |
其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。
8、验证主从同步
在主mysql创建数据库abc,再从mysql查看已经同步成功!
Shell1 2 3 4 5 6 7 8 9 10 11 | mysql> create database abc; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | abc | | mysql | | performance_schema | | test | +--------------------+ |
在slave启动报错:
“Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’”
解决:报错的原因主要是slave设置master的二进制文件名或pos值不对应!
先flush logs;清空日志,在查看下主数据库的状态 show master status;看下日志文件名字和position值;
再在slave中,执行:CHANGE MASTER TO MASTER_LOG_FILE=‘二进制日志名’,MASTER_LOG_POS=值;
最后启动同步进程:start slave;
二、mysql-proxy实现读写分离
1、安装mysql-proxy
实现读写分离是有lua脚本实现的,现在mysql-proxy里面已经集成,无需再安装
下载:http://dev.mysql.com/downloads/mysql-proxy/
Shell1 2 | tar zxvf mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit.tar.gz mv mysql-proxy-0.8.3-linux-glibc2.3-x86-64bit /usr/local/mysql-proxy |
2、配置mysql-proxy,创建主配置文件
Shell1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | cd /usr/local/mysql-proxy mkdir lua #创建脚本存放目录 mkdir logs #创建日志目录 cp share/doc/mysql-proxy/rw-splitting.lua ./lua #复制读写分离配置文件 cp share/doc/mysql-proxy/admin-sql.lua ./lua #复制管理脚本 vi /etc/mysql-proxy.cnf #创建配置文件 [mysql-proxy] user=root #运行mysql-proxy用户 admin-username=proxy #主从mysql共有的用户 admin-password=123.com #用户的密码 proxy-address=192.168.0.204:4000 #mysql-proxy运行ip和端口,不加端口,默认4040 proxy-read-only-backend-addresses=192.168.0.203 #指定后端从slave读取数据 proxy-backend-addresses=192.168.0.202 #指定后端主master写入数据 proxy-lua-script=/usr/local/mysql-proxy/lua/rw-splitting.lua #指定读写分离配置文件位置 admin-lua-script=/usr/local/mysql-proxy/lua/admin-sql.lua #指定管理脚本 log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log #日志位置 log-level=info #定义log日志级别,由高到低分别有(error|warning|info|message|debug) daemon=true #以守护进程方式运行 keepalive=true #mysql-proxy崩溃时,尝试重启 保存退出! chmod 660 /etc/mysql-porxy.cnf |
3、修改读写分离配置文件
Shell1 2 3 4 5 6 7 8 | vi /usr/local/mysql-proxy/lua/rw-splitting.lua if not proxy.global.config.rwsplit then proxy.global.config.rwsplit = { min_idle_connections = 1, #默认超过4个连接数时,才开始读写分离,改为1 max_idle_connections = 1, #默认8,改为1 is_debug = false } end |
4、启动mysql-proxy
Shell1 2 3 4 | /usr/local/mysql-proxy/bin/mysql-proxy --defaults-file=/etc/mysql-proxy.cnf netstat -tupln | grep 4000 #已经启动 tcp 0 0 192.168.0.204:4000 0.0.0.0:* LISTEN 1264/mysql-proxy 关闭mysql-proxy使用:killall -9 mysql-proxy |
5、测试读写分离
1>.在主服务器创建proxy用户用于mysql-proxy使用,从服务器也会同步这个操作
Shell1 | mysql> grant all on *.* to ‘proxy‘@‘192.168.0.204‘ identified by ‘123.com‘; |
2>.使用客户端连接mysql-proxy
Shell1 | mysql -u proxy -h 192.168.0.204 -P 4000 -p123.com |
创建数据库和表,这时的数据只写入主mysql,然后再同步从slave,可以先把slave的关了,看能不能写入,这里我就不测试了,下面测试下读的数据!
Shell1 2 3 | mysql> create table user (number INT(10),name VARCHAR(255)); mysql> insert into test values(01,‘zhangsan‘); mysql> insert into user values(02,‘lisi‘); |
3>.登陆主从mysq查看新写入的数据如下,
Shell1 2 3 4 5 6 7 8 9 | mysql> use test; Database changed mysql> select * from user; +--------+----------+ | number | name | +--------+----------+ | 1 | zhangsan | | 2 | lisi | +--------+----------+ |
4>.再登陆到mysql-proxy,查询数据,看出能正常查询
Shell1 2 3 4 5 6 7 8 9 | mysql -u proxy -h 192.168.0.204 -P 4000 -p123.com mysql> use test; mysql> select * from user; +--------+----------+ | number | name | +--------+----------+ | 1 | zhangsan | | 2 | lisi | +--------+----------+ |
5>.登陆从服务器关闭mysql同步进程,这时再登陆mysql-proxy肯定会查询不出数据
Shell1 | slave stop; |
6>.登陆mysql-proxy查询数据,下面看来,能看到表,查询不出数据
Shell1 2 3 4 5 6 7 8 9 10 | mysql> use test; Database changed mysql> show tables; +----------------+ | Tables_in_test | +----------------+ | user | +----------------+ mysql> select * from user; ERROR 1146 (42S02): Table ‘test.user‘ doesn‘t exist |
配置成功!真正实现了读写分离的效果!
来源http://www.cnblogs.com/luckcs/articles/2543607.html
MySQL Proxy 实现 MySQL 读写分离提高并发负载
标签:could only 基本 bar server plain warning ace tcp