当前位置:Gxlcms > mysql > mysql主从同步配置_MySQL

mysql主从同步配置_MySQL

时间:2021-07-01 10:21:17 帮助过:18人阅读

bitsCN.com

主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 root@192.168.9.123 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 root@192.168.9.124:/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”

bitsCN.com

人气教程排行