理解MysqL主从服务器
主从服务器解决了4个问题:
- 数据分布
- 负载平衡(load balancing)
- 备份
- 高可用性(high availability)和容错
工作原理
从高层来看,主从服务器复制分成三步:
1. master将改变记录到二进制日志(binary log)中,这些记录叫做二进制日志事件,binary log events
2. slave将master的binary log events 拷贝到它的中继日志relay log中
3. slave的sql thread读取relay log的数据并将数据写入到自己的数据库中
如图:
安装MysqL
> sudo apt-get install MysqL-server
配置MysqL
主MysqL服务器192.168.33.11。从MysqL服务器192.168.33.12。
配置主服务器
> sudo vim /etc/MysqL/MysqL.conf.d/MysqLd.cnf ======
#bind-address = 127.0.0.1
server-id = 1
log_bin = /var/log/MysqL/MysqL-master-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name binlog_ignore_db = MysqL ======
配置从服务器账户和权限
> MysqL -u root -p
MysqL> grant replication slave on *.* to 'username'@'192.168.33.%' identified by 'password';
MysqL> flush privileges;
> sudo service MysqL restart
note:192.168.33.%通配符,表示0-255的IP都可访问主服务器
查看主服务器状态
MysqL> show master status;
配置从服务器
> sudo vim /etc/MysqL/MysqL.conf.d/MysqLd.cnf ======
server-id = 2
relay_log = /var/log/MysqL/MysqL-slave-relay.log read_only = 1
log_slave_updates = 1 ======
> sudo service MysqL restart
> MysqL -u root -p
MysqL> change master to
-> master_host='192.168.33.11',
-> master_port=3306,
-> master_user='username',
-> master_password='password',
-> master_log_file='MysqL-master-bin.000001',
-> master_log_pos=154;
注:master_log_file和master_log_pos可以通过在主服务器使用 show master status; 获得。
启动从服务器
MysqL> start slave;
MysqL> show slave status \G;
注:启动后主要确认Slave_IO_Running和Slave_sql_Running是Yes状态。
测试主从服务器
登录到主MysqL服务器
MysqL> create database zztest;
MysqL> use zztest;
MysqL> create table test (name varchar(100));
MysqL> insert into test(name) values("zhe");
MysqL> insert into test(name) values("xiao");
MysqL> insert into test(name) values("zhexiao");
登录到从MysqL服务器
MysqL> show databases;
MysqL> use zztest;
MysqL> show tables;
MysqL> select * from test;
原文链接:https://www.f2er.com/ubuntu/351300.html