MySql 主从配置

编辑配置 - Master

[root@localhost /]# vi /etc/my.cnf

log-bin=master-bin

log-slave-updates = true

server-id       = 1

重启服务

[root@localhost /]# systemctl reload mysqld

创建并授权同步账号

mysql> grant replication slave on *.* to 'myslave'@'192.168.242.188' identified by '123456';

mysql> flush privileges;

mysql> show master status;

+------------------+----------+--------------+------------------+

| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |

+------------------+----------+--------------+------------------+

| mysql-bin.000014 |      107 |              |                  |

+------------------+----------+--------------+------------------+

1 row in set (0.00 sec)

编辑配置 - Slave

[root@localhost data]#  vi /etc/my.cnf

server-id       = 2

srelay-log=relay-log-bin

relay-log-index=slave-relay-bin-index

重启服务

[root@localhost /]# systemctl reload mysqld

开启 Slave配置

mysql> change master to master_host='192.168.242.185', master_user='myslave', master_password='123456', master_log_file='mysql-bin.000014', master_log_pos=107;

mysql> start slave;

mysql> show slave status\G

             Slave_IO_Running: Yes

            Slave_SQL_Running: Yes

问题:

mysql> change master to master_host='192.168.242.185', master_user='myslave', master_password='123456', master_log_file='mysql-bin.000014', master_log_pos=107;

 ERROR 1201 (HY000): Could not initialize master info structure; more error messages can be found in the MySQL error log

解决:

mysql> slave stop;

mysql> reset slave

mysql> change master to master_host='192.168.242.185', master_user='myslave', master_password='123456', master_log_file='mysql-bin.000014', master_log_pos=107;

mysql> slave start;

主从好处

  1. 分摊服务器压力,提高机器系统的处理效率
  2. 增加冗余,提高服务可用性

原理

依赖二进制日志bin-log,其记录了引起数据库变化的语句(Insert、update、delete、create table)

Scale-up 与 Scale-out

Scale-out 是指Application在水平方向上的扩展,即当添加更多机器 时,应用可以利用这些资源来提升自己的效率

Scale-up 是指Application在垂直方向上的扩展,即当机器添加更多的CPU、内存等资源时,应该可以充分利用这些资源来提升其效率

可以考虑数据库中间件,来完成读写分离的转发