分类: Mysql/postgreSQL
2012-03-30 00:08:44
OS: 2.6.18-238.el5
用到的软件:
mysql-5.5.17.tar
cmake-2.8.6.tar.gz
libmcrypt-2.5.7.tar.gz
master: 192.168.61.135 mysql-5.5.17
slave: 192.168.61.137 mysql-5.5.17
1. 分别在master和slave上安装mysql
1) 安装cmake //用来编译mysql
tar zxvf cmake-2.8.6.tar.gz
cd cmake-2.8.6
./configure && make && make install
2) 安装libmcrypt
tar zxvf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7
./configure && make && make install
3) 安装mysql
groupadd mysql
useradd -g mysql mysql -s /bin/false
tar zxvf mysql-5.5.17.tar.gz
cd mysql-5.5.17
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/var/lib/mysql -DSYSCONFDIR=/etc/
make && make install
cp ./support-files/my-huge.cnf /etc/my.cnf
vi /etc/my.cnf 在 [mysqld] 部分增加
datadir = /var/lib/mysql
/usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/var/lib/mysql & //生成mysql系统数据库
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld //把Mysql加入系统启动
chmod 755 /etc/init.d/mysqld
chkconfig mysqld on
vi /etc/rc.d/init.d/mysqld
basedir = /usr/local/mysql //MySQL程序安装路径
datadir = /data/mysql //MySQl数据库存放目录
service mysqld start
vi /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
下面这两行把myslq的库文件链接到系统默认的位置,这样你在编译类似PHP等软件时可以不用指定mysql的库文件地址
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
shutdown -r now //需要重启系统
重启完后
/usr/local/mysql/bin/mysqladmin -u root password "123456"
service mysqld restart
2. master和slave的相关配置
配置master
1) 修改master的my.cnf文件
vi /etc/my.conf 检查以下字段,没有的话添加
server-id = 1 //唯一ID,master/slave 集群中不能重复,默认master使用 1
log-bin=mysql-bin //同步事件的日志记录文件,master/slave通过该文件来达到同步的目的
#binlog-do-db=test //需要同步的数据库,如果没有本行,即表示同步所有的数据库
#binlog-ignore-db=mysql //被忽略的数据库
2) 在master上为slave添加一同步帐号
mysql -uroot -p123456
grant replication slave on *.* to 'sync'@'%' identified by 'sync';
3) 重启master的mysql服务
service mysqld restart
4) 查看master日志情况
show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 | 107 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
记下master_log_file和master_log_pos
配置slave
1) 修改slave的my.cnf文件
vi /etc/my.conf 检查以下字段,没有的话添加
server-id = 2
read_only
relay-log=mysqld-relay-bin
replicate-do-db=test //同步的数据库,不写本行 表示 同步所有数据库
2) 重启slave的mysql服务
service mysqld restart
3) 设置要同步的master的参数
change master to master_host='192.168.61.135',master_user='sync',master_password='sync', master_log_file=' mysql-bin.000001' ,master_log_pos=107;
4) 开启同步
start slave;
5) 查看同步情况
show slave status \G;
确保Slave_IO_Running为YES和Slave_SQL_Running 为 YES
6) 查看进程信息
mysql> show processlist;
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
| 1 | root | localhost | NULL | Query | 0 | NULL | show processlist |
| 2 | system user | | NULL | Connect | 4252 | Waiting for master to send event | NULL |
| 3 | system user | | NULL | Connect | 4252 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL |
+----+-------------+-----------+------+---------+------+-----------------------------------------------------------------------------+------------------+
3 rows in set (0.00 sec)