1、系统环境

CentOS release 6.6 x86_64

2、试验环境

Master: 10.170.13.174

Slave1: 10.116.31.26

 

3、安装mysql

Ø 安装方式可选择RPM安装、YUM在线安装、编译安装等。分别对三台机器都安装mysql server 和 mysql client 。版本和根据需求选择,这里我们选择mysql 5.5的RPM安装方式。

[root@localhost nagios]# rpm -vhi MySQL-server-5.5.16-1.linux2.6.x86_64.rpm

[root@localhost nagios]# rpm -vhi MySQL-client-5.5.16-1.linux2.6.x86_64.rpm

Ø 安装晚上之后在mysql的安装目录,根据服务器实际硬件配置选择配置文件复制到/etc目录下,更名为my.cnf

[root@localhost mysql]# cp my-huge.cnf /etc/my.cnf

Ø 复制完之后启动mysql服务,并且使用mysql client登录管理后台。

[root@localhost mysql]# service mysql start

Starting MySQL..........                                   [确定]

[root@localhost mysql]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 78

Server version: 5.5.16-log MySQL Community Server (GPL)

 

Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql>

 

至此mysql可以正常连接,安装完成。

4、Master的配置

Ø 配置/etc/my.cnf文件,在mysqldhue节点中添加

back_log = 1024 //当短时间内连接数为1024时新开一个线程

key_buffer_size = 38400M //索引的缓冲区大小128G内存应该设置8192M-12288M

sort_buffer_size = 240M  //查询排序时所能使用的缓冲区大小128G内存应该设置192M-256M

read_buffer_size = 180M  //读查询操作所能使用的缓冲区大小128G内存应该设置192M-256M

read_rnd_buffer_size = 180M //128G内存应该设置192M-256M

myisam_sort_buffer_size = 1824M      //128G内存应该设置2048M以下

thread_cache_size = 512

query_cache_size = 4096M //MySQL查询缓冲区的大小

wait_timeout = 120 //请求的最大连接时间

thread_concurrency = 64 //服务器逻辑CPU数量×2

max_connections=1000 //最大连接数限制

log-bin=mysql-bin       //日志为2进制,不需要更改

server-id    =1         //为1就是Master,不需要更改

binlog-do-db=igs        //要同步的库

binlog-ignore-db=mysql,test,information_schema   //是不要同步的数据库名,多个数据库中间用逗号(,)隔开

然后重启数据库,让配置文件生效

[root@localhost mysql]# service mysql start

 

Ø master服务器添加要同步的用户

#mysql -u root -p

mysql>grant replication slave on . to myigs@' 10.116.31.26' identified by '!igs2015@';

mysql>flush privileges;

 

格式:GRANT REPLICATION SLAVE ON . TO '帐号'@'从服务器IP或主机名' IDENTIFIED BY '密码';

 

Ø 备份master数据库数据

mysql> flush tables with read lock;  //不要退出这个终端,否则这个锁就不生效了。从服务器的数据库建好后。在主服务器执行解锁

同时要记录下mysql-bin.000003和1271

mysql> show master status;

+------------------+----------+--------------+-------------------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB              |

+------------------+----------+--------------+-------------------------------+

| mysql-bin.000002 |      106 | blog,www     | mysql,test,information_schema |

+------------------+----------+--------------+-------------------------------+

1 row in set (0.01 sec)

此时在主库解开table的锁定

mysql> unlock tables;

5、Slave1的配置 

安装mysql方法如上

Ø 配置/etc/my.cnf文件修改以下几项

server-id  = 2 //为2就是Slave,默认是1

replicate-do-db=igs   //告诉slave只做www数据库的更新 

replicate-do-db=blog  //告诉slave只做blog数据库的更新 

log-slave-updates

 

Ø 把从主数据库服务器备份出来的数据库导入到从服务器中

先用scp把主服务器上的backup.tar.gz拷贝过来,解压到/data/mysqldata/ 目录

#cd /data/mysqldata/ 

#tar -zxvf backup.tar.gz

 

启动从库服务器

#service mysqld start

停止slave服务,设置主服务器的各种参数

#mysql

mysql> stop slave;

mysql> change master to master_host=' 10.170.13.174',master_user='myigs',master_password='!igs2015@',master_log_file=' mysql-bin.000002',master_log_pos=120;

mysql> start slave;

 

然后在master上插入记录,会发现在slave上也会出现

 

mysql> show slave status\G

*************************** 1. row ***************************

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.21

                  Master_User: repluser

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysql-bin.000002

          Read_Master_Log_Pos: 1271

               Relay_Log_File: squid2-relay-bin.000002

                Relay_Log_Pos: 106

        Relay_Master_Log_File: mysql-bin.000002

             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:

              Relay_Log_Space:

              Until_Condition:

               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.01 sec)

6、Mysql-proxy代理安装配置

6.1 安装LUA

tar zxvf lua-5.1.tar.gz

cd lua-5.1

用vi修改Makefile,使"INSTALL_TOP=/usr/local/lua",这样做的目的是为了是lua的所有文件都安装在目录/usr/local/lua/

make posix

make install

 

6.2 安装 libevent

tar zxvf libevent-1.1a.tar.gz

cd libevent-1.1a

./configure --prefix=/usr/local/libevent

make

make install

 

6.3 安装check

tar zxvf check-0.8.4.tar.gz

cd check-0.8.4

./configure

make

make install

 

6.4 设置环境变量

把下面的内容追加到/etc/profile中

export LUA_CFLAGS="-I/usr/local/lua/include" LUA_LIBS="-L/usr/local/lua/lib -llua -ldl" LDFLAGS="-L/usr/local/libevent/lib -lm"

export CPPFLAGS="-I/usr/local/libevent/include"

export CFLAGS="-I/usr/local/libevent/include"

然后执行 source /etc/profile (安装完mysql-proxy不再需要这些变量,可以删除之)

 

6.5 安装mysql(只安装mysql客户端即可)

tar zxvf  mysql-5.0.45.tar.gz

cd mysql-5.0.45

./configure --prefix=/usr/local/mysql  --without-server

make

make install

6.6 启动与安装mysql-proxy

tar zxvf mysql-proxy-0.8.3-linux-rhel5-x86-64bit.tar.gz
mv mysql-proxy-0.8.3-linux-rhel5-x86-64bit /usr/local

mysql-proxy --proxy-address=$admin --proxy-backend-addresses=$slave --proxy-read-only-backend-addresses=$master --proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua >> /var/log/mysql-proxy.log &


6.7 其它说明
--help-all显示所有的帮助选项
--admin-address=host:port 管理主机及端口,默认是4041
--proxy-address=host:port 代理服务器的监听地址及端口,默认4040
--proxy-read-only-address=host:port 只读连接时,代理服务器的监听地址及端口。默认4042
--proxy-backend-addresses=host:port连接真实服务器的地址及监听端口,默认是3306,这是mysql代理最重要的选项,多个主机之间用空格隔开。使用rr算法。
--proxy-lua-script=file 指定lua脚本的名称
6.8 测试

使用客户端连接上mysql-proxy服务器,进行写和读操作,对比两台服务器之间的数据变化。