MySQL主从同步、读写分离配置步骤
2022-11-12 09:23:51
内容摘要
这篇文章主要为大家详细介绍了MySQL主从同步、读写分离配置步骤,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!现在使用的两台服务器已经
文章正文
这篇文章主要为大家详细介绍了MySQL主从同步、读写分离配置步骤,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
现在使用的两台服务器已经安装了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.11、登录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/mysqltar data.tar.gz data接着直接执行了远程scp;scp ./data.tar.gz root@192.168.0.2:/var/lib/mysql5、登录Slave数据库服务器,修改my.cnf;server-id = 3 //2已经被用在另一个服务器上了,如果以后要再加Slave号接着往后数就OK了;log-bin=mysql-binmaster-host = 192.168.0.1master-user = testmaster-password = ******master-port = 3306master-connect-retry=60 //如果发现主服务器断线,重新连接的时间差;replicate-ignore-db=mysql //不需要备份的数据库;replicate-do-db=data //需要备份的数据库log-slave-updateslave-skip-errors6、解压刚才从Master scp过来的文件,此处不用改权限、属主,默认没有改变,可以根据实际情况进行修改;7、上述完成后,可以启动slave了;查看slave状态;mysql>slave start;mysql>show slave status\G;*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.0.1Master_User: testMaster_Port: 3306Connect_Retry: 60Master_Log_File: updatelog.000001Read_Master_Log_Pos: 106Relay_Log_File: onlinevc-relay-bin.000013Relay_Log_Pos: 1069Relay_Master_Log_File: updatelog.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: dataReplicate_Ignore_DB: mysqlReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 106Relay_Log_Space: 1681Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:1 row in set (0.00 sec)ERROR:No query specified8、查看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 eventMaster_Host: 192.168.0.1Master_User: testMaster_Port: 3306Connect_Retry: 60Master_Log_File: updatelog.000012Read_Master_Log_Pos: 717039Relay_Log_File: onlinevc-relay-bin.000013Relay_Log_Pos: 1222Relay_Master_Log_File: updatelog.000012Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: dataReplicate_Ignore_DB: mysqlReplicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno: 0Last_Error:Skip_Counter: 0Exec_Master_Log_Pos: 717039Relay_Log_Space: 1834Until_Condition: NoneUntil_Log_File:Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master: 0Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error:Last_SQL_Errno: 0Last_SQL_Error:1 row in set (0.00 sec)ERROR:No query specified#################################### 如下是MySQL数据库读写分离操作步骤 ##########################################此处使用MySQL自己(Mysql-proxy)的代理实现数据库的读写分离;所需要安装包如下;1、check-0.9.82、glib-2.18.43、libevent-2.0.6-rc4、lua-5.1.4wget http://www.lua.org/ftp/lua-5.1.4.tar.gz5、pkg-config-0.236、mysql-5.0.567、mysql-proxy-0.8.0http://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.gzcd check-0.8.4./configuremakemake install2、tar -zxvf glib-2.18.4.tar.gz //系统rpm包可能版本低出现了问题3;./configuremakemake install3、tar -zxvf libevent-2.0.6-rc.tar.gzcd libevent-2.0.6-rc./configure --prefix=/usr/local/libeventmake && make install4、tar -zxvf lua-5.1.4.tar.gzINSTALL_TOP= /usr/local/lua // 为了把lua安装到/var/lib/lua下,故要修改其下的Makefile;或者直接执行:sed -i 's#INSTALL_TOP= /usr/local#INSTALL_TOP= /usr/local/lua#' Makefileroot@testmysql [/software/lua-5.1.4]# makePlease domake PLATFORMwhere PLATFORM is one of these:aix ansi bsd freebsd generic linux macosx mingw posix solarisSee 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.gzcd pkg-config-0.23./configuremakemake 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 MySQLMySQL-client-5.1.48-0.glibc23MySQL-bench-5.0.91-0.glibc23MySQL-test-5.1.48-0.glibc23MySQL-shared-5.1.48-0.glibc23MySQL-server-5.1.48-0.glibc23MySQL-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-servermake && make install7、tar xvf mysql-proxy-0.8.0.tar.gzcd mysql-proxy-0.8.0./configure --prefix=/usr/local/mysql-proxy --with-mysql=/usr/local/mysql --with-lua //问题解决处4有介绍;Make && Make install8、在/var/lib/bin创建mysql-proxy.sh,内容如下;#!/bin/bashLUA_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:* LISTENtcp 0 0 0.0.0.0:4041 0.0.0.0:* LISTEN10、测试读写分离,此步略过,所有配置已经完成。&&&&&&&&&&&&&&&&&&&&&&&&&&& 问题解决区 &&&&&&&&&&&&&&&&&&&&&&&&&&&&1、在安装的第四步执行make linux时报错如下:root@testmysql [/software/lua-5.1.4]# make linuxcd src && make linuxmake[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.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lcode.o lcode.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ldebug.o ldebug.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ldo.o ldo.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ldump.o ldump.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lfunc.o lfunc.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lgc.o lgc.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o llex.o llex.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lmem.o lmem.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lobject.o lobject.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lopcodes.o lopcodes.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lparser.o lparser.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lstate.o lstate.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lstring.o lstring.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ltable.o ltable.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ltm.o ltm.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lundump.o lundump.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lvm.o lvm.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lzio.o lzio.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lauxlib.o lauxlib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lbaselib.o lbaselib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ldblib.o ldblib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o liolib.o liolib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lmathlib.o lmathlib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o loslib.o loslib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o ltablib.o ltablib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o lstrlib.o lstrlib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o loadlib.o loadlib.cgcc -O2 -Wall -DLUA_USE_LINUX -c -o linit.o linit.car 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.oranlib liblua.agcc -O2 -Wall -DLUA_USE_LINUX -c -o lua.o lua.cIn file included from lua.h:16,from lua.c:15:luaconf.h:275:31: error: readline/readline.h: No such file or directoryluaconf.h:276:30: error: readline/history.h: No such file or directorylua.c: In function 鈥榩ushline鈥?lua.c:182: warning: implicit declaration of function 鈥榬eadline鈥?lua.c:182: warning: assignment makes pointer from integer without a castlua.c: In function 鈥榣oadline鈥?lua.c:210: warning: implicit declaration of function 鈥榓dd_history鈥?make[2]: *** [lua.o] Error 1make[2]: Leaving directory `/software/lua-5.1.4/src'make[1]: *** [linux] Error 2make[1]: Leaving directory `/software/lua-5.1.4/src'make: *** [linux] Error 2解决方法:yum install libtermcap-develyum install ncurses-develyum install libevent-develyum install readline-devel2、安装MySQL-proxy时报错:checking for LUA... configure: error: Package requirements (lua5.1 >= 5.1) were not met:No package 'lua5.1' foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables LUA_CFLAGSand 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.pc3、安装MySQL-proxy时报错:checking for GLIB... configure: error: Package requirements (glib-2.0 >= 2.16.0) were not met:No package 'glib-2.0' foundConsider adjusting the PKG_CONFIG_PATH environment variable if youinstalled software in a non-standard prefix.Alternatively, you may set the environment variables GLIB_CFLAGSand 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.4glibc-headers-2.5-49.el5_5.4glib2-2.12.3-4.el5_3.1glibc-common-2.5-49.el5_5.4glibc-devel-2.5-49.el5_5.42、下载安装glib-2.18.4.tar.gz4、刚开始使用系统里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步。注:关于MySQL主从同步、读写分离配置步骤的内容就先介绍到这里,更多相关文章的可以留意
代码注释