mysql-proxy代理加mysql主從實現讀寫分離

實驗環境搭建:

mysql-proxy   192.168.1.163

mysql-master  192.168.1.164 (主)

mysql-slave   192.168.1.162 (從)


配置:mysql-proxy   192.168.1.163

#yum install lua -y

lua-5.1.4-4.1.el6.x86_64(大部分都是默認安裝過的)

下載mysql-proxy最新版:MySQL Proxy 0.8.1 alpha

#yum install lrzsz -y

#rz (上傳mysql-proxy)

#tar zxvf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz

#mv mysql-proxy-0.8.5-linux-el6-x86-64bit /usr/local/mysql-proxy

#ls -l /usr/local/mysql-proxy

drwxr-xr-x. 2 7161 wheel 4096 Aug 19  2014 bin

drwxr-xr-x. 2 7161 wheel 4096 Aug 19  2014 include

drwxr-xr-x. 6 7161 wheel 4096 Aug 19  2014 lib

drwxr-xr-x. 2 7161 wheel 4096 Aug 19  2014 libexec

drwxr-xr-x. 7 7161 wheel 4096 Aug 19  2014 licenses

drwxr-xr-x. 3 7161 wheel 4096 Aug 19  2014 share

#ehho "PATH=$PATH:/usr/local/mysql-proxy/bin/">> /etc/bashrc

#export PATH=$PATH:/usr/local/mysql-proxy/bin/

#source /etc/bashrc 刷新

mysql-master  192.168.1.164 (主)

#yum install mysql mysql-server -y

#/etc/init.d/mysqld start

#mysql

mysql> create database db;

Query OK, 1 rowaffected (0.00 sec)

mysql> use db;

Database changed

mysql> show databases;

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

| Database           |

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

|information_schema |

| db                 |

| mysql              |

| test               |

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

4 rows in set(0.00 sec)

 

mysql>  create table test(id int);

Query OK, 0 rowsaffected (0.04 sec)

 

mysql>  insert into testvalues(6363);

Query OK, 1 rowaffected (0.00 sec)

 

mysql> grant all on db.* to user1@'%'  identified by '123456' ; 登錄的用戶及權限密碼。

Query OK, 0 rowsaffected (0.00 sec)

mysql-slave   192.168.1.162 (從)(步驟同上和主服務器一樣的配置)

#yum install mysql mysql-server -y

#/etc/init.d/mysqld start

#mysql

mysql> create database db;

Query OK, 1 rowaffected (0.00 sec)

mysql> use db;

Database changed

mysql> show databases;

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

| Database           |

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

|information_schema |

| db                 |

| mysql              |

| test               |

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

4 rows in set(0.00 sec)

 

mysql>  create table test(id int);

Query OK, 0 rowsaffected (0.04 sec)

 

mysql>  insert into testvalues(6363);

Query OK, 1 rowaffected (0.00 sec)

 

mysql> grant all on db.* to user1@'%'  identified by '123456' ; 登錄的用戶及權限密碼。

Query OK, 0 rowsaffected (0.00 sec)

配置:mysql-proxy   192.168.1.163

#mysql-proxy -r 192.168.0.162:3306 -b 192.168.0.164:3306 -s /usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua &

-r 指定讀服務器

-b 指定些服務器

-s 指定使用那個lua(我這們這邊做的讀寫分離所以選rw-splitting.lua)

執行命令後提示:

2016-03-18 00:32:38: (critical) plugin proxy 0.8.5 started

# netstat -antup|grep proxy

tcp   0   0   0.0.0.0:4040          0.0.0.0:*               LISTEN      3323/mysql-proxy

測試:

#mysql -u user1 -p123456 -P 4040 -h 192.168.0.163(拿mysql-proxy做pc機測試。)

mysql> use db ;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed

mysql> select *from test;

    server default db: 

    client default db: db

    syncronizing

+------+

| id   |

+------+

|   62 |

+------+

1 row in set (0.00 sec)

mysql> insert into test values(63);

Query OK, 1 row affected (0.00 sec)


mysql> select *from test;

+------+

| id   |

+------+

|   62 |

+------+

1 row in set (0.00 sec)

測試寫數據沒有問題,但是select *from test;查看不到,說明現在已經實現讀寫分離。

檢測數據登錄1.162從(只讀服務器)

mysql> use db;

Database changed

mysql> select *from test;

+------+

| id   |

+------+

|   62 |

+------+

1 row in set (0.00 sec)

檢測數據登錄1.164從(寫服務器)

mysql> use db;

Database changed

mysql> select *from test;

+------+

| id   |

+------+

|   64 |

|   63 |

+------+

2 rows in set (0.00 sec)

在1.163上面insert into test values(63);添加的63來到(1.64寫服務器)。

讀寫分離成功。

那下面我們配置mysql主從:

mysql-master  192.168.1.164 (主)

#vim /etc/my.conf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0


log-bin=mysqllog

server-id=1            添加這三行。

binlog-do-db=db 


[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

登錄數據庫:主服務器

mysql> grant all on *.* to user2@'%' identified by '123456'; 

Query OK, 0 rows affected (0.00 sec)

建個同步的賬戶.

mysql> drop table test; 刪除test表格

#service mysqld restart

mysql-slave   192.168.1.162 (從)

#vim /etc/my.conf

[mysqld]

datadir=/var/lib/mysql

socket=/var/lib/mysql/mysql.sock

user=mysql

# Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0


server-id=2 

master-host=192.168.0.164     t添加這4行

master-user=user2 

master-password=123456 


[mysqld_safe]

log-error=/var/log/mysqld.log

pid-file=/var/run/mysqld/mysqld.pid

登錄數據庫:從服務器

mysql> drop table test; 刪除test表格

#service mysqld restart

登錄:mysql-master  192.168.1.164 (主)

mysql> use db;

Database changed

mysql>  create table test (id int ); 

Query OK, 0 rows affected (0.03 sec)


mysql>  insert into test values(62); 

Query OK, 1 row affected (0.00 sec)

mysql>  insert into test values(63); 

Query OK, 1 row affected (0.00 sec)

建立test表。

登錄:mysql-slave   192.168.1.162 (從)

mysql> use db ;

Database changed

mysql> select *from test;

+------+

| id   |

+------+

|   62 |

|   63 |

+------+

2 rows in set (0.00 sec)

數據已同步過來。

mysql> show slave status \G;

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

               Slave_IO_State: Waiting for master to send event

                  Master_Host: 192.168.0.164

                  Master_User: user2

                  Master_Port: 3306

                Connect_Retry: 60

              Master_Log_File: mysqllog.000002

          Read_Master_Log_Pos: 622

               Relay_Log_File: mysqld-relay-bin.000002

                Relay_Log_Pos: 514

        Relay_Master_Log_File: mysqllog.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: 622

              Relay_Log_Space: 670

              Until_Condition: None

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


ERROR: 

No query specified


mysql> 





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章