日期:2014-05-16  浏览次数:20731 次

mysql主从复制,读写分离

From:http://hi.baidu.com/yzy888/blog/item/98784fd074381d84a0ec9cc3.html

?


mysql主从复制
主服务器IP:192.168.99.10
从服务器IP:192.168.99.20
(一)安装mysql(主从服务器操作相同)
yum -y install gcc gcc-c++ ncurses-devel
增加组和用户
#/usr/sbin/groupadd mysql????????
#/usr/sbin/useradd -g mysql mysql -s /sbin/nologin
解压、编译、安装
#tar xvf mysql-5.1.35.tar.gz
#cd mysql-5.1.35
#./configure --prefix=/usr/local/mysql --localstatedir=/data/mysql/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
修改权限
#chown -R mysql.mysql /usr/local/mysql
初始化数据库
#/usr/local/mysql/bin/mysql_install_db --user=mysql
改变权限
#chown -R mysql:mysql /data/mysql/data
(二)修改配置文件
主服务器
server-id = 1
从服务器
server-id = 2..3..4依次增加
(三)启动服务(主从相同)
#/usr/local/mysql/bin/mysqld_safe --user=mysql&
(四)授权(仅主服务器)
mysql>GRANT REPLICATION SLAVE ON *.* to?'rep'@'192.168.99.%'?identified by 'sasasa';
(五)查询主数据库状态(主服务器上)
mysql> show master status;
+------------------+----------+--------------+------------------+
| File???????????? | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |????? 771 |????????????? |????????????????? |?
+------------------+----------+--------------+------------------+
记住file和position的值
(六)配置从服务器
mysql> change master to master_host='192.168.99.10', master_user='rep', master_password='sasasa1', master_log_file='mysql-bin.000003', master_log_pos=771;
master_host主服务器ip,master_user连接帐户,master_password连接密码,maser_log_file填入上面的file值,master_log_pos填入上面的position值
mysql> start slave;??????? 启用了复制功能
mysql> show slave status\G;??????? 检查slave状态
*************************** 1. row ***************************
?????????????? Slave_IO_State: Waiting for master to send event
????????????????? Master_Host: 192.168.99.10
????????????????? Master_User: rep
????????????????? Master_Port: 3306
??????????????? Connect_Retry: 60
????????????? Master_Log_File: mysql-bin.000003
????????? Read_Master_Log_Pos: 771