mysql-proxy的用处就不再说了
mysql-proxy依赖libevent,lua,glib2等几个软件
所以在安装之前先
yum install lua-devel
yum install glib2-devel
libevent我采用源代码安装,
下载的
libevent-1.4.13-stable.tar.gz
tar zvfx libevent-1.4.13-stable.tar.gz
./configure
进去make
make install
现在就可以安装mysql-proxy了
上mysql-proxy
下载源码
还是先
tar zvfx
./configure
make
make check
make install
ldconfig
现在就可以使用mysql-proxy了
如现在我们数据库在192.168.1.245,本机地址192.168.1.201
我们可以这样
mysql-proxy -P 192.168.1.201:3308 --proxy-backend-addresses=192.168.1.245:3306
然后客户端连192.168.1.201:3308即可访问245的数据库,用户名密码用245的。
有些只能内网访问的,又用不惯命令行的,可以这么干
原文:http://my.oschina.net/gotham/blog/344823#OSC_h1_1
生产服务器111.29.115.11安装mysql-proxy
1,安装glib2,版本为2.22.5
提示找不到gettext,因此yum install gettext library
yum -y install gcc gcc-c++ autoconf libtool pkgconfig ncurses ncurses-devel
配置glib2的环境变量
export GLIB_CFLAGS="-I/usr/local/glib-2.22.5/include/glib-2.0"
export GLIB_LIBS="-L/usr/local/glib-2.22.5/lib/glib-2.0"
export GMODULE_CFLAGS="-I/usr/local/glib-2.22.5/include"
export GMODULE_LIBS="-L/usr/local/glib-2.22.5/lib"
export GTHREAD_CFLAGS="-I/usr/local/glib-2.22.5/include"
export GTHREAD_LIBS="-L/usr/local/glib-2.22.5/lib"
安装readline
wget http://fossies.org/linux/misc/readline-6.3.tar.gz
./configure --prefix=/usr/local/readline
make && make install
yum install readline-devel
重新安装lua
tar zvfx lua-5.1.4.tar.gz
cd lua-5.1.4
make linux
make test
make install
ldconfig -v
设置lua环境变量
export PATH=$PATH:/usr/local/lua/bin
配置pkg-config 环境变量,命令如下所示:
cp etc/lua.pc /usr/local/lib/pkgconfig/
export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
配置LUA参数
export LUA_CFLAGS="-I/usr/local/lua/include"
LUA_LIBS="-L/usr/local/lua/lib -llua -ldl"
LDFLAGS="-L/usr/local/libevent/lib -lm"
启动-------------------------------------------------------------
cp etc/lua.pc /usr/local/lib/pkgconfig/
(重要)export GLIB_CFLAGS="-I/usr/local/glib-2.2/include/glib-2.0"
(重要)export GLIB_LIBS="-L/usr/local/glib-2.2/lib/glib-2.0"
(重要)export GMODULE_CFLAGS="-I/usr/local/glib2/include"
(重要)export GMODULE_LIBS="-L/usr/local/glib-2.2/lib"
(重要)export GTHREAD_CFLAGS="-I/usr/local/glib-2.2/include"
(重要)export GTHREAD_LIBS="-L/usr/local/glib-2.2/lib"
(重要)export LUA_CFLAGS="-I/usr/local/include" LUA_LIBS="-L/usr/local/lib -llua -ldl" LDFLAGS="-lm"
或者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 LUA_LIBS="-L/usr/local/lib -llua -ldl"
export LDFLAGS="-lm"
(重要)export PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
在安装mysql-proxy之前
export CPPFLAGS="-I/usr/local/libevent/include"
export CFLAGS="-I/usr/local/libevent/include"
./configure --with-lua --with-mysql=/usr/local/mysql/bin/mysql_config --prefix=/usr/local/mysql-proxy
make
make install
1,
在/usr/local/mysql-proxy下建立文件夹script,run,log
mkdir -p /usr/local/mysql-proxy/script
mkdir -p /usr/local/mysql-proxy/run
mkdir -p /usr/local/mysql-proxy/log
复制rw-splitting.lua
cp -rf /home/sunto/soft/mysql-proxy-0.8.2/lib/rw-splitting.lua /usr/local/mysql-proxy/script
vim /usr/local/mysql-proxy/script/rw-splitting.lua 修改如下
=============================
-- connection pool
if not proxy.global.config.rwsplit then
proxy.global.config.rwsplit = {
min_idle_connections = 1, //默认为4
max_idle_connections = 1, //默认为8
is_debug = false
}
end
=============================
复制admin-sql.lua
cp -rf /home/sunto/soft/mysql-proxy-0.8.2/lib/admin-sql.lua /usr/local/mysql-proxy/script/
2,
在/usr/local/mysql-proxy/log/下新建日志文件mysqlproxy.log
在/usr/local/mysql-proxy下建立文件夹conf
/usr/local/mysql-proxy/conf下添加一个配置文件mysql-proxy.cnf
编辑mysql-proxy.cnf,内容是:
[mysql-proxy]
log-file = /usr/local/mysql-proxy/log/mysql-proxy.log(日志文件)
proxy-address=111.41.86.64:3308(proxy所在IP)
proxy-backend-addresses=124.40.120.87:3306
--proxy-read-only-backend-addresses=111.41.86.64:3306
--proxy-lua-script=/usr/local/mysql-proxy/script/rw-splitting.lua
--admin-lua-script = /usr/local/proxy-mysql/script/admin-sql.lua
admin-username=proxy
admin-password=123456
log-level=debug
keep-alive=true
下面为参数说明:
Proxy服务常用选项
选项 说明
proxy-address=$host:$port mysql-proxy的ip和port,默认port是4040
daemon=$bool 是否使用daemon模式启动
event-threads=$count event-handing线程数,默认值是1
keep-alive=$bool proxy服务崩溃后自动重启
log-file=$file_name 日志文件
log-level=$level 日志级别:error|warning|info|message|debug
log-use-syslog=$bool 日志使用syslog
max-open-files=$count 最大文件句柄数
pid-file=$file_name pid文件
3,
启动mysql-proxy
方法1:
./mysql-proxy --proxy-read-only-backend-addresses=111.41.86.64:3306
--proxy-backend-addresses=124.40.120.87:3306 -P 111.41.86.64:3308
--proxy-lua-script=/usr/local/mysql-proxy/script/rw-splitting.lua &
方法2:
./mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.cnf &
4,
方法1:
在111.41.86.64和124.40.120.87两台数据库机器上,分别运行mysql命令
grant all on *.* to root@111.41.86.64
flush privileges
这样可以保证111.41.86.64(mysql-proxy所在机器)有权限访问和更新数据
方法2:
124.40.120.87是主,111.41.86.64是从,124.40.120.87是读写,111.41.86.64是只读。
myql-proxy的IP是111.41.86.64,创建用于读写分离的数据库连接用户
在124.40.120.87主库服务器上操作以下命令:
mysql -uroot -p123456
新建或编
grant all on *.* to 'proxy'@'111.41.86.64' identified by '123456';
(111.41.86.64为mysql-proxy机器IP,proxy为proxy账号,123456为proxy密码)
在主从库服务器上mysql执行
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
查看是否有用户proxy
5,
执行./mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.cnf &
错误 (critical) mysql-proxy-cli.c:326: loading config from ‘./mysql-proxy.cnf’
failed: permissions of ./mysql-proxy.cnf aren’t secure (0660 or stricter required)
解决办法:
由于安全要求,必须将配置文件权限设为660(创建人可读写,同组人可读),否则不允许启动。
chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.cnf
在执行 ./mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.cnf &
启动成功!
6,
设置Mysql-proxy环境变量
export PATH=$PATH:/usr/local/mysql-proxy/bin
7,
打开3308端口防火墙端口,因为mysql-proxy监听端口是3308再也不是3306了。
/sbin/iptables -I INPUT -p tcp --dport 3308 -j ACCEPT
/etc/rc.d/init.d/iptables save
service iptables save
8,
登录mysql-proxy
mysql -uproxy -p123456 -h111.41.86.64 -P3308