欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL主从同步、读写分离配置步骤

程序员文章站 2023-12-21 10:40:58
现在使用的两台服务器已经安装了mysql,全是rpm包装的,能正常使用。 为了避免不必要的麻烦,主从服务器mysql版本尽量保持一致; 环境:192.168.0.1 (ma...
现在使用的两台服务器已经安装了mysql,全是rpm包装的,能正常使用。
为了避免不必要的麻烦,主从服务器mysql版本尽量保持一致;
环境:192.168.0.1 (master)
192.168.0.2 (slave)
mysql version:ver 14.14 distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1
1、登录master服务器,修改my.cnf,添加如下内容;
server-id = 1 //数据库id号, 为1时表示为master,其中master_id必须为1到232–1之间的一个正整数值;
log-bin=mysql-bin //启用二进制日志;
binlog-do-db=data //需要同步的二进制数据库名;
binlog-ignore-db=mysql //不同步的二进制数据库名;这个同步后听说很麻烦,我没有同步;
log-bin=/var/log/mysql/updatelog //设定生成的log文件名;
log-slave-updates //把更新的记录写到二进制文件中;
slave-skip-errors //跳过错误,继续执行复制;
2、建立复制所要使用的用户;
mysql>grant replication slave on *.* to test@192.168.0.2 identified by '********'
3、重启mysql;
/usr/bin/mysqladmin -uroot shutdown;
/usr/bin/mysql_safe &
4、现在备份master上的数据;
锁定后我直接tar.gz data这个库文件;
mysql>flush tables with read lock;
cd /var/lib/mysql
tar data.tar.gz data
接着直接执行了远程scp;
scp ./data.tar.gz root@192.168.0.2:/var/lib/mysql
5、登录slave数据库服务器,修改my.cnf;
server-id = 3 //2已经被用在另一个服务器上了,如果以后要再加slave号接着往后数就ok了;
log-bin=mysql-bin
master-host = 192.168.0.1
master-user = test
master-password = ******
master-port = 3306
master-connect-retry=60 //如果发现主服务器断线,重新连接的时间差;
replicate-ignore-db=mysql //不需要备份的数据库;
replicate-do-db=data //需要备份的数据库
log-slave-update
slave-skip-errors
6、解压刚才从master scp过来的文件,此处不用改权限、属主,默认没有改变,可以根据实际情况进行修改;
7、上述完成后,可以启动slave了;查看slave状态;
mysql>slave start;
mysql>show slave status\g;
*************************** 1. row ***************************
slave_io_state: waiting for master to send event
master_host: 192.168.0.1
master_user: test
master_port: 3306
connect_retry: 60
master_log_file: updatelog.000001
read_master_log_pos: 106
relay_log_file: onlinevc-relay-bin.000013
relay_log_pos: 1069
relay_master_log_file: updatelog.000001
slave_io_running: yes
slave_sql_running: yes
replicate_do_db: data
replicate_ignore_db: mysql
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: 106
relay_log_space: 1681
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

8、查看master上面的状态;
mysql> show master status;
+------------------+----------+--------------+------------------+
| file | position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
| updatelog.000012 | 15016 | data | mysql |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
由此可见两者的file、position存在问题,所要要去slave上设置对应主库的master_log_file、read_master_log_pos;执行如下语句;
mysql>slave stop;
mysql>change master to master_host='192.168.0.1',master_user='test', master_password='******',master_log_file='updatelog.000012',master_log_pos=15016;
确保 slave_io_running: yes 、slave_sql_running: yes都要为yes才能证明slave的i/o和sql进行正常。
9、解锁主库表;
unlock tables;
到此主从mysql服务器配置完成,测试结果如下;
mysql> show master status;
+------------------+----------+--------------+------------------+
| file | position | binlog_do_db | binlog_ignore_db |
+------------------+----------+--------------+------------------+
| updatelog.000012 | 717039 | data | mysql |
+------------------+----------+--------------+------------------+
1 row 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.1
master_user: test
master_port: 3306
connect_retry: 60
master_log_file: updatelog.000012
read_master_log_pos: 717039
relay_log_file: onlinevc-relay-bin.000013
relay_log_pos: 1222
relay_master_log_file: updatelog.000012
slave_io_running: yes
slave_sql_running: yes
replicate_do_db: data
replicate_ignore_db: mysql
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: 717039
relay_log_space: 1834
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数据库读写分离操作步骤 ##########################################
此处使用mysql自己(mysql-proxy)的代理实现数据库的读写分离;
所需要安装包如下;
1、check-0.9.8
2、glib-2.18.4
3、libevent-2.0.6-rc
4、lua-5.1.4
wget http://www.lua.org/ftp/lua-5.1.4.tar.gz
5、pkg-config-0.23
6、mysql-5.0.56
7、mysql-proxy-0.8.0
http://mysql.cdpa.nsysu.edu.tw/downloads/mysql-proxy/mysql-proxy-0.8.0.tar.gz

别的安装包地址当时没有记下载地址,不过大部分都在这个网站上找的;http://sourceforge.net/
&&&&&&&&&& 安装开始 &&&&&&&&
1、tar -zxvf check-0.8.4.tar.gz
cd check-0.8.4
./configure
make
make install
2、tar -zxvf glib-2.18.4.tar.gz //系统rpm包可能版本低出现了问题3;
./configure
make
make install
3、tar -zxvf libevent-2.0.6-rc.tar.gz
cd libevent-2.0.6-rc
./configure --prefix=/usr/local/libevent
make && make install
4、tar -zxvf lua-5.1.4.tar.gz
install_top= /usr/local/lua // 为了把lua安装到/var/lib/lua下,故要修改其下的makefile;
或者直接执行:sed -i 's#install_top= /usr/local#install_top= /usr/local/lua#' makefile
root@testmysql [/software/lua-5.1.4]# make
please do
make platform
where platform is one of these:
aix ansi bsd freebsd generic linux macosx mingw posix solaris
see install for complete instructions.
这处是要你选择服务器所使用的平台;
执行:make linux //此处执行后出现了错误,解决办法在下面问题解决区1处,此处先跳过;
再执行:make install
设置环境变量:
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"

5、tar -zxvf pkg-config-0.23.tar.gz
cd pkg-config-0.23
./configure
make
make install
安装完之后要执行:cp etc/lua.pc /usr/local/lib/pkgconfig/lua5.1.pc //原因见下面的问题解决区2处;
6、安装mysql客户端;
因为此服务器系统是默认安装了mysql,没有安装客户端,我又装了client、devel如下所示已安装的rpm包;
root@testmysql [/software/lua-5.1.4]# rpm -qa | grep mysql
mysql-client-5.1.48-0.glibc23
mysql-bench-5.0.91-0.glibc23
mysql-test-5.1.48-0.glibc23
mysql-shared-5.1.48-0.glibc23
mysql-server-5.1.48-0.glibc23
mysql-devel-5.1.48-0.glibc23
此后的mysql-proxy时总是一直报错,编译不过去,无奈之下用源码包客户端;(此时的rpm包都没有卸载,直接执行了下面的安装)//此处问题见问题解决区4处;
tar zxvf mysql-5.0.56.tar.gz //此处我直接使用了mysql的5.0.56的源码包;
cd mysql-5.0.56
./configure --prefix=/usr/local/mysql --without-server
make && make install

7、tar xvf mysql-proxy-0.8.0.tar.gz
cd mysql-proxy-0.8.0
./configure --prefix=/usr/local/mysql-proxy --with-mysql=/usr/local/mysql --with-lua //问题解决处4有介绍;
make && make install

8、在/var/lib/bin创建mysql-proxy.sh,内容如下;
#!/bin/bash
lua_path="/usr/local/mysql-proxy/lib/mysql-proxy/lua/?.lua" /usr/local/mysql-proxy/bin/mysql-proxy --proxy-backend-addresses=192.168.0.1:3306 --proxy-read-only-backend-addresses=192.168.0.2:3306 --proxy-lua-script=/usr/local/mysql-proxy/lib/mysql-proxy/rw-splitting.lua >> /var/log/mysql-proxy.log &
然后加上可执行权限;
chmod a+x /var/lib/bin/mysql-proxy.sh
执行:/var/lib/bin/mysql-proxy.sh 启动服务;
9、验证是否开户了:4040、4041;
root@testmysql [/usr/local/bin]# netstat -an | grep 404*
tcp 0 0 0.0.0.0:4040 0.0.0.0:* listen
tcp 0 0 0.0.0.0:4041 0.0.0.0:* listen
10、测试读写分离,此步略过,所有配置已经完成。

&&&&&&&&&&&&&&&&&&&&&&&&&&& 问题解决区 &&&&&&&&&&&&&&&&&&&&&&&&&&&&
1、在安装的第四步执行make linux时报错如下:
root@testmysql [/software/lua-5.1.4]# make linux
cd src && make linux
make[1]: entering directory `/software/lua-5.1.4/src'
make all mycflags=-dlua_use_linux mylibs="-wl,-e -ldl -lreadline -lhistory -lncurses"
make[2]: entering directory `/software/lua-5.1.4/src'
gcc -o2 -wall -dlua_use_linux -c -o lapi.o lapi.c
gcc -o2 -wall -dlua_use_linux -c -o lcode.o lcode.c
gcc -o2 -wall -dlua_use_linux -c -o ldebug.o ldebug.c
gcc -o2 -wall -dlua_use_linux -c -o ldo.o ldo.c
gcc -o2 -wall -dlua_use_linux -c -o ldump.o ldump.c
gcc -o2 -wall -dlua_use_linux -c -o lfunc.o lfunc.c
gcc -o2 -wall -dlua_use_linux -c -o lgc.o lgc.c
gcc -o2 -wall -dlua_use_linux -c -o llex.o llex.c
gcc -o2 -wall -dlua_use_linux -c -o lmem.o lmem.c
gcc -o2 -wall -dlua_use_linux -c -o lobject.o lobject.c
gcc -o2 -wall -dlua_use_linux -c -o lopcodes.o lopcodes.c
gcc -o2 -wall -dlua_use_linux -c -o lparser.o lparser.c
gcc -o2 -wall -dlua_use_linux -c -o lstate.o lstate.c
gcc -o2 -wall -dlua_use_linux -c -o lstring.o lstring.c
gcc -o2 -wall -dlua_use_linux -c -o ltable.o ltable.c
gcc -o2 -wall -dlua_use_linux -c -o ltm.o ltm.c
gcc -o2 -wall -dlua_use_linux -c -o lundump.o lundump.c
gcc -o2 -wall -dlua_use_linux -c -o lvm.o lvm.c
gcc -o2 -wall -dlua_use_linux -c -o lzio.o lzio.c
gcc -o2 -wall -dlua_use_linux -c -o lauxlib.o lauxlib.c
gcc -o2 -wall -dlua_use_linux -c -o lbaselib.o lbaselib.c
gcc -o2 -wall -dlua_use_linux -c -o ldblib.o ldblib.c
gcc -o2 -wall -dlua_use_linux -c -o liolib.o liolib.c
gcc -o2 -wall -dlua_use_linux -c -o lmathlib.o lmathlib.c
gcc -o2 -wall -dlua_use_linux -c -o loslib.o loslib.c
gcc -o2 -wall -dlua_use_linux -c -o ltablib.o ltablib.c
gcc -o2 -wall -dlua_use_linux -c -o lstrlib.o lstrlib.c
gcc -o2 -wall -dlua_use_linux -c -o loadlib.o loadlib.c
gcc -o2 -wall -dlua_use_linux -c -o linit.o linit.c
ar rcu liblua.a lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o lundump.o lvm.o lzio.o lauxlib.o lbaselib.o ldblib.o liolib.o lmathlib.o loslib.o ltablib.o lstrlib.o loadlib.o linit.o
ranlib liblua.a
gcc -o2 -wall -dlua_use_linux -c -o lua.o lua.c
in file included from lua.h:16,
from lua.c:15:
luaconf.h:275:31: error: readline/readline.h: no such file or directory
luaconf.h:276:30: error: readline/history.h: no such file or directory
lua.c: in function 鈥榩ushline鈥?
lua.c:182: warning: implicit declaration of function 鈥榬eadline鈥?
lua.c:182: warning: assignment makes pointer from integer without a cast
lua.c: in function 鈥榣oadline鈥?
lua.c:210: warning: implicit declaration of function 鈥榓dd_history鈥?
make[2]: *** [lua.o] error 1
make[2]: leaving directory `/software/lua-5.1.4/src'
make[1]: *** [linux] error 2
make[1]: leaving directory `/software/lua-5.1.4/src'
make: *** [linux] error 2

解决方法:yum install libtermcap-devel
yum install ncurses-devel
yum install libevent-devel
yum install readline-devel

2、安装mysql-proxy时报错:
checking for lua... configure: error: package requirements (lua5.1 >= 5.1) were not met:

no package 'lua5.1' found

consider adjusting the pkg_config_path environment variable if you
installed software in a non-standard prefix.

alternatively, you may set the environment variables lua_cflags
and lua_libs to avoid the need to call pkg-config.
see the pkg-config man page for more details.
解决办法:
cp etc/lua.pc /usr/local/lib/pkgconfig/lua5.1.pc

3、安装mysql-proxy时报错:
checking for glib... configure: error: package requirements (glib-2.0 >= 2.16.0) were not met:

no package 'glib-2.0' found

consider adjusting the pkg_config_path environment variable if you
installed software in a non-standard prefix.

alternatively, you may set the environment variables glib_cflags
and glib_libs to avoid the need to call pkg-config.
see the pkg-config man page for more details.
解决办法:
1、查看系统已经安装glib的rpm包;
glibc-2.5-49.el5_5.4
glibc-headers-2.5-49.el5_5.4
glib2-2.12.3-4.el5_3.1
glibc-common-2.5-49.el5_5.4
glibc-devel-2.5-49.el5_5.4
2、下载安装glib-2.18.4.tar.gz

4、刚开始使用系统里rpm所指定的mysq_config时,mysql-proxy安装报错,信息如下;
configure: error: mysql_config not exists or not executable, use $ ./configure --with-mysql=/path/to/mysql_config
看到帮助安装文档里要求如下:
--with-mysql[=path] include mysql support. path is the path to 'mysql_config'。
解决办法就是:安装包中的第6步。

上一篇:

下一篇: