MySQL主从Replication同步
时间:2013年4月2日
环境准备:
用途 |
IP |
主数据库 |
192.168.15.150 |
从数据库 |
192.168.15.151 |
6块300G SAS300G 做成RAID1+0
mysql版本:5.5.30
mysql安装cmake 编译选项
http://dev.mysql.com/doc/internals/en/autotools-to-cmake.html
1. 开始安装
1) 初始化系统环境
# yum -y install gcc gcc-c++autoconf libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-develncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-libskrb5-devel libidn libidn-devel openssl openssl-devel cmake
#; yum groupinstall"Development tools" "Development Libraries" -yt
源码编译安装过程
#groupadd mysql
#useradd -r -g mysql mysql
# tar -zxf mysql-5.5.30.tar.gz
#mkdir -p /usr/local/mysql
#mkdir -p /data/data
#cd mysql-5.5.30
#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DEXTRA_CHARSETS=complex \
-DWITH_EMBEDDED_SERVER=1 \
-DENABLED_LOCAL_INFILE=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1
# make&& make install
#chown -R mysql:mysql /usr/local/mysql
#chown -R mysql:mysql /data/data
以mysql用户帐号的身份建立数据库,让/data/data成为mysql存放数据的目录
# /usr/local/mysql/scripts/mysql_install_db\
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data \
--user=mysql
2. 设置主库
1) 修改主库my.cnf,主要是设置个不一样的id和logbin
# cp /usr/local/mysql/support-files/my-medium.cnf ../my.cnf
# vim /usr/local/mysql/my.cnf#蓝色为修改相应值
server-id = 1
log-bin= binlog
binlog-do-db=iea
启动mysql数据库
#nohup /usr/local/mysql/bin/mysqld_safe \
--defaults-file=/usr/local/mysql/my.cnf&
登陆主库
#/usr/local/mysql/bin/mysql -uroot
赋予从库权限账号,允许用户在主库上读取日志
mysql>grant replication slave on *.* to 'admin'@'192.168.15.151' identified by'12345678';
GRANT REPLICATION SLAVE,REPLICATIONCLIENT,RELOAD,SUPER ON *.* TO 'backup'@'192.168.105.150' IDENTIFIED BY 'ccdbpassword';
检查是否创建成功
mysql> select User,Hostfrom mysql.user;
锁主库表
mysql> flush tables withread lock;
显示主库信息(从库加入时需要用到)
mysql>show master status;
slave端机器获取master端数据库的快照
#/usr/local/mysql/bin/mysqldump --master-data -uroot iea > /root/iea.sql
3. 设置从库(从库安装过程和主库一样)
1) rsync传输数据库快照
#rsync -vzrtopg /root/iea.sql [email protected]:/root
登陆从库创建数据库,导入iea.sql
mysql> create database iea;
# /usr/local/mysql/bin/mysql -uroot < /root/iea.sql
解锁主库表
mysql> unlocktables;
修改从库my.cnf
# vim /usr/local/mysql/my.cnf #蓝色部分为修改内容
servser-id = 2
在从库上设置同步(如果设置错误可以使用reset slave)
mysql>slave stop;
mysql> change master tomaster_host='192.168.15.150',master_user='admin',master_password='12345678',master_log_file='binlog.000002',master_log_pos=816;
change master to master_host='192.168.105.149',master_user='backup',master_password='ccdbpassword',master_log_file='binlog.000002',master_log_pos=816;
mysql> slave start;
mysql> show slave status \G; #查看从库状态,Slave_IP_Runing:Yes,Slave_SQL_Running:Yes 为正常状态
4. 环境测试
1) 在主库建立表,并插入数据
mysql> CREATETABLE `lee` (
`id` INT( 5 )UNSIGNED NOT NULL AUTO_INCREMENT ,
`username`VARCHAR( 20 ) NOT NULL ,
`password` CHAR(32 ) NOT NULL ,
`time` DATETIMENOT NULL ,
`number` FLOAT(10 ) NOT NULL ,
`content` TEXTNOT NULL ,
PRIMARY KEY (`id` )
) ENGINE =MYISAM ;
mysql>insertinto lee (`username`,`password`,`time`,`number`,`content`) values('wenshan.li',password('ipsec123'),now(),6452,'hello world');
在slave机上查看表格和数据
mysql > use iea;
mysql > select * from lee; #如果正常就配置完成