MySQL一主多从配置(以MySQL5.5为例)

理论知识

mysql 同步原理

mysql同步方案

读写分离方案

 

 

 mysql linux 免安装配置


安装前必须删除原来的安装

需要检查 以下文件是否存在 ,如果存储则要删除之

/etc/my.cnf

/etc/init.d/mysqld 

mysql 依赖的库

  1. shell> yum search libaio # search for info 
  1. shell> yum install libaio # install library

 

 创建mysql   与用户组,-s /bin/false 表示该用户不能登录 

  1. shell> groupadd mysql
  1. shell> useradd -r -g mysql -s /bin/false mysql

 

解压安装包至指定目录

  1. shell> tar zxvf /path/to/mysql-VERSION-OS.tar.gz 
  1. shell> ln -s full-path-to-mysql-VERSION-OS mysql 
  1. shell> cd mysql

为 mysql 用户添加权限

  1. shell> chown -R mysql ./
  1. shell> chgrp -R mysql ./ 
  1. #创建data目录并添加权限
  1. shell> mkdir -p /data/mysql
  1. shell> chown -R mysql:mysql /data/mysql

 

拷贝配置文件

  1. shell> cp ${basedir}/support-files/my-medium.cnf /etc/my.cnf
  1. #修改配置
  1. [mysqld]
  1. basedir=/home/cbt/svr/mysql
  1. datadir=/data/mysql
  1. character-set-server=utf8
  1. shell> cp support-files/mysql.server /etc/init.d/mysql

 

初始化 mysql 库

  1. shell> ./scripts/mysql_install_db --user=mysql --basedir=/home/cbt/svr/mysql --datadir=/data/mysql

#添加环境变量

  1. shell> vi /etc/profile
  1. PATH=/home/cbt/svr/mysql/bin:$PATH
  1. export PATH 
  1. #让刚才的修改生效
  1. shell> source /etc/profile

启动及其它配置  #启动数据库

  1. service mysql start
  1.  
  1. #开机启动
  1. chkconfig mysqld on
  1.  
  1. #初始化mysql的一些设置
  1. mysql_secure_installation
  1. #回车
  1. Enter current password for root (enter for none):
  1. #y,设置mysql的root密码
  1. Set root password?[Y/n] y
  1. #以下都yes
  1. Remove anonymous users?[Y/n] y
  1. Disallow root login remotely?[Y/n] y
  1. Remove test database and access to it?[Y/n] y
  1. Reload privilege tables now?[Y/n] y
  1. ThanksforusingMySQL!

 

允许远程登陆

  1. mysql> use mysql;   
  1. mysql> select host,user,password from user;   
  1. mysql> update user set password=password('123456') where user='root';  
  1. mysql> update user set host='%' where user='root' and host='localhost';  
  1. mysql> flush privileges;  

安装时的一些错误

-bash: ./scripts/mysql_install_db: /usr/bin/perl: bad interpreter: 没有那个文件或目录

解决: yum -y install perl perl-devel

Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory

解决:yum -y install libaio-devel

 

一主多从配置


master my.cnf 配置

  1. #需要同步的二进制数据库名;
  1. binlog-do-db=tuling
  1. #不同步的二进制数据库名,如果不设置可以将其注释掉;
  1. binlog-ignore-db=information_schema
  1. binlog-ignore-db=mysql
  1. binlog-ignore-db=personalsite
  1. binlog-ignore-db=test
  1.  

 

  1. #以下参数可选----------

 

  1. #binlog 格式 
  1. binlog-format=ROW 
  1. log-bin=mysql-master-bin 
  1. #slave更新时是否记录到日志中;
  1. log-slave-updates=true
  1. #开启半同步,需要另外安装插来支持 
  1. rpl_semi_sync_master_enabled=ON

 创建用户用于主从同步的数据库

  1. grant replication slave,super,reload on *.* to slave1@192.168.17.201 identified by '123456';

查看主节点状态

  1. mysql> show master status 
  1. #在主库上查看已连接的slave主机
  1. mysql> show slave hosts;
  1. #查看所有binlog日志 
  1. mysql> show binary logs;
  1. #查看所有binlog 事件
  1. mysql> show binlog events in 'mysql-bin.000003' from 145 \G;

 

slave my.cnf 配置-----

  1. server-id = 2
  1. log-bin=mysql-slave-bin
  1. replicate-do-db=tuling
  1. replicate-ignore-db=information_schema
  1. replicate-ignore-db=mysql
  1. replicate-ignore-db=personalsite
  1. replicate-ignore-db=test

 

slave 节点修改master 配置

  1. mysql>change master to master_host='192.168.0.101', master_user='slave1', master_password='123456' ,MASTER_AUTO_POSITION = 2887;

 

 

Slave 相关操作

  1. #启动slave
  1. mysql>start slave;
  1. mysql>stop slave;
  1. #查看 slave 状态
  1. show slave status\G;
  1. #跳过指定数量错误

 

  1. SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;

 

  1. #查看 relaylog 事件
  1. show relaylog events in 'localhost-relay-bin.000019'

 

读写分离实现   360 Atlas


安装与卸载Atlas

atlas下载

链接:https://pan.baidu.com/s/1zXCYzayszOBCAxZPnLOMVw  提取码:2s8b 

  1. #安装
  1. shell> rpm -i  Atlas-2.2.1.el6.x86_64.rpm
  1. #卸载
  1. shell> rpm -e  Atlas-2.2.1.el6.x86_64.rpm

安装目录在 /usr/local/mysql-proxy/ 当中

Atlass配置

 

  1. #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔

 

  1. proxy-backend-addresses = 127.0.0.1:3306
  1. #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
  1. #proxy-read-only-backend-addresses = 127.0.0.1:3305@1
  1. #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密。用户密码所有主库和从库都必须一至
  1. pwds = root:/iZxz+0GRoA=
  1. #Atlas监听的工作接口IP和端口
  1. proxy-address = 0.0.0.0:1234

启动与关闭Atlas

  1. ./mysql-proxyd test start
  1. ./mysql-proxyd test stop

以代理方式进行管理员登录

  1. mysql -h127.0.0.1 -P2345 -uroot -proot
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值