mysql读写分离,主从配置

根据数据库服务层

原理主master服务器更新操作后生成构建日志,然后从服务器获取此日志写入从服务器日志。然后同步至从服务器

linux 配置

登录多台Linux服务器
//设置主mysql打开二进制日志
sudo vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
#增加如下:
server-id=1
log-bin=master-bin
log-bin-index=master-bin.index
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
default-character-set=utf-8
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
//重启service mysql restart
//进入数据库查看
show master status
//给从服务器添加ip和用户
GRANT REPLICATION SLAVE ON *.* TO 'repl(用户名)'@'从服务器ip' IDENTIFIED BY '密码';
//刷新
flush privileges;
//进入从服务器配置文件
sudo vim /etc/my.cnf
[mysqld]
server-id=2
relay-log-index=slave-relay-bin.index
relay-log=slave-relay-bin
//重启
/etc/init.d/mysql stop
/etc/init.d/mysql start
mysql -u root -p
change master to master_host='120.24.64.163',master_port=3306,master_user='repl',master_password='mysql',master_log_file='master-bin.0000001',master_log_pos=0;
//开启主从跟踪
start slave
//查看启动后状态是否正常
show slave status \G;

推荐阅读更多精彩内容