Mysql主从复制

(一)安装mysql(主从服务器皆相同) 先创建用户 useradd mysql -s /sbin/nologin
tar zxvf mysql-5.1.49.tar.gz
cd mysql-5.1.49
[root@centos mysql-5.1.49]# ./configure --prefix=/usr/local/mysql --localstatedir=/opt/data --with-extra-charsets=utf8,gb2312,gbk --with-pthread --enable-thread-safe-client
注:配置过程指定数据文件的位置及另加的字符集.
make
make install
cp support-files/my-large.cnf /etc/my.cnf
cd /usr/local/mysql
chgrp -R mysql .
生成基本的数据库和表: /usr/local/mysql/bin/mysql_install_db --user=mysql
成功执行后察看数据目录/opt/data,看是否有文件或目录生成.
chown -R mysql:mysql /opt/data
记得给每个数据库设置root密码.
(二)修改配置文件
不同的地方就是server-id,主服务器配置文件不用修改,从服务器的配置文件server-id=10.其他的内容基本相同.
(三)启动服务
/usr/local/mysql/bin/mysqld_safe --user=mysql&
这个过程主辅服务器都相同.
(四)授权(在主服务器上进行)
GRANT REPLICATION SLAVE ON *.* to 'rep1'@192.168.0.17 identified by 'mysql';
(五)查询主数据库状态(主服务器上进行)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      240 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记下file及position的值,后面做从服务器操作的时候需要用.
(六)配置从服务器
 mysql> change master to master_host='192.168.0.16', master_user='rep1', master_password='mysql', master_log_file='mysql-bin.000003', master_log_pos=240;
正确执行后再执行:
mysql> start slave;
就启用了复制功能.这里我们运行一下 mysql> show slave status\G 来检查一下,一个正常的输出结果应该如下面的形式:
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.16
                  Master_User: rep1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 240
               Relay_Log_File: mysqlS-relay-bin.000003
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000003
             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: 240
              Relay_Log_Space: 552
              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>

(七)主数据库有数据的情况:
1、数据库锁表操作,不让数据再进行写入动作。mysql> FLUSH TABLES WITH READ LOCK;
2、察看主数据库的状态 mysql> show master status; 照第(五)步记录输出值。
3、把主服务器数据文件复制到从服务器,最好先用tar处理一下。
4、取消主数据库锁定 mysql> UNLOCK TABLES;
5、从服务器的操作。跟前面的步骤一样(略过)

mysql代理安装配置
一、安装mysql-proxy.需要按下列顺序安装其所依赖的包:
(一)安装LUA
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
用vi修改Makefile,使"INSTALL_TOP=/usr/local/lua",这样做的目的是为了是lua的所有文件都安装在目录/usr/local/lua/
make posix
make install
 
(二)安装 libevent
tar zxvf libevent-1.4.14b-stable.tar.gz
cd libevent-1.4.14b-stable
./configure --prefix=/usr/local/libevent
make
make install
 
(三)安装check
tar zxvf check-0.9.8.tar.gz
cd check-0.9.8
./configure
make
make install
 
(四)设置安装mysql-proxy所需的环境变量.把下面的内容追加到/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不再需要这些变量,可以删除之)
 
(五)安装mysql(只安装mysql客户端即可)
tar zxvf  mysql-5.1.49.tar.gz
cd mysql-5.1.49
./configure --prefix=/usr/local/mysql  --without-server
make
make install
编译安装glib-2.24.2.tar.gz
[root@mysqlM ~]# cd glib-2.24.2
[root@mysqlM glib-2.24.2]# ./configure
[root@mysqlM glib-2.24.2]# make&make install
#export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
如果上面的不行,应该
#export PKG_CONFIG_PATH=/usr/lib/pkgconfig

(六)安装mysql-proxy
tar zxvf  mysql-proxy-0.8.0.tar.gz
cd  mysql-proxy-0.8.0
[root@mysqlM mysql-proxy-0.8.0]# ./configure --prefix=/usr/local/mysql-proxy --with-mysql=/usr/local/mysql --with-lua
[root@mysqlM mysql-proxy-0.8.0]# make
[root@mysqlM mysql-proxy-0.8.0]# make install

把路径加入path中
[root@mysqlM bin]# pwd
/usr/local/mysql-proxy/bin
[root@mysqlM bin]# vi /etc/profile
export PATH=$PATH:/usr/local/mysql-proxy/bin
[root@mysqlM bin]# source /etc/profile

[root@xutest opt]#mysql-proxy -V
mysql-proxy: error while loading shared libraries: /usr/local/mysql-proxy/lib/libmysql-chassis.so.0: cannot restore segment prot after reloc: Permission denied
解决办法:
编辑/etc/selinux/config,找到这段:
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - SELinux is fully disabled.
SELINUX=enforcing
把 SELINUX=enforcing 注释掉:#SELINUX=enforcing ,然后新加一行为:
SELINUX=disabled
保存,关闭
在你保证SElinux 被disable后.还执行下:chcon -t texrel_shlib_t
例:
[root@testserver mysql-proxy-0.8.0]# chcon -t texrel_shlib_t /usr/local/mysql-proxy/lib/libmysql-chassis.so.0

二:使用:
[root@xutest opt]#mysql-proxy --help-all           #查看所有的设置选项              
                 
管理功能选项

--admin-address=host:port -- 指定一个mysqo-proxy的管理端口,缺省是 4041
代理功能选项
--proxy-address=host:port -- 这个是mysql-proxy 服务器端的监听端口,缺省是 4040
--proxy-read-only-backend-addresses=<host:port> -- 远程只读Slave服务器的地址和端口,缺省为不设置(本选项在mysql-proxy0.5.0版本中没有)
--proxy-backend-addresses=host:port -- 指定远程MySQL服务器地址和端口,可以设置多个,
--proxy-skip-profiling -- 关闭查询分析功能,缺省是打开的
--proxy-fix-bug-25371 -- 修正 mysql的libmysql版本大于5.1.12的一个#25371号bug
--proxy-lua-script=file -- 指定一个Lua脚本程序来控制mysql-proxy的运行和设置,这个脚本在每次新建连接和脚本发生修改的的时候将重新调用
其他选项
--daemon -- mysql-proxy以守护进程方式运行
--pid-file=file -- 设置mysql-proxy的存储PID文件的路径

看来mysql-proxy只能研究到这儿了,很多人都不建议这个东西应用到生产环境中,我也很害怕业务应用中我会死在这上面,还是等他成熟之后再说吧,听取秋香姐的建议,读写分离还是采用程序(他使用的是正则表达式)来实现吧。