主host:www IP:192.168.9.124
從host:www1 IP:192.168.9.123
#下載mysql
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.58-linux-i686-glibc23.tar.gz/from/ftp://mirror.anl.gov/pub/mysql/
#安裝mysql
tar xzf mysql-5.1.58-linux-i686-glibc23.tar.gz
useradd mysql
mv mysql-5.1.58-linux-i686-glibc23 /usr/local/mysql
chown -R root .
chown -R mysql data
cp support-files/my-medium.cnf /etc/my.cnf
bin/mysqld_safe --user=mysql &
cp support-files/mysql.server /etc/init.d/mysql.server
cd
export PATH=$PATH:/usr/local/mysql/bin/ # 加入環境變量
分別更改主從root密碼
# mysql
mysql>update mysql.user set password=password('123456') where user="root" and host="localhost";
www:
mysql> grant all privileges on *.* to [email protected] identified by '123456'; #授權192.168.9.123 登陸mysql
mysql> flush privileges; #刷新權限表
mysql> exit;
在從服務器測試
www1
# mysql -u root -h 192.168.9.124 -p
www
mysql> FLUSH TABLES WITH READ LOCK; (主庫加鎖)
記錄日志和偏移量
mysql> show master status ;
mysqldump -p --all-database --lock-all-tables >db.sql
www1
scp -P [email protected]:/root/db.sql .
######### 修改從my.cnf ############
server-id=2
master-host=192.168.9.124
master-user=slave
master-password=123456
master-port=3306
log-bin=mysql-bin
-----------------------------------
重啟mysql
lsof -i:3306 |xargs kill -9
/usr/local/mysql/bin/mysqld_safe --user=mysql &
mysql -p < db.sql # 導入db.mysql
www
mysql>unlock tables ; (主庫解鎖)
mysql>show master status ;
www1
mysql> slave stop;
mysql> CHANGE MASTER TO MASTER_HOST='192.168.9.124', MASTER_USER='root',
MASTER_PASSWORD='123456',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=548; (後面log_file log_pos 參照主庫的日志名稱和偏移量)
mysql> slave start ;
查狀態
mysql> show master status;
mysql> show slave status;
mysql> show processlist ;
www
-A INPUT -s 192.168.9.123 -p tcp --dport 3306 -j ACCEPT 放行192.168.9.123連接3306端口
-A INPUT -p tcp --dport 22 -j ACCEPT #允許ssh
-A INPUT -j DROP #拒絕所有
作者 “xavier”