linux下mysql安装 主从复制和读写分离配置

    最近由于工作需求,在linux环境下完成mysql的安装,实现mysql主从复制结构和读写分离功能,实践过程并不顺利,但从中获颇多,特写此文来巩固自己的知识,也为了帮助更多人少走弯路。

一、mysql安装

   #准备mysql-5.5.55-linux2.6-x86_64.tar.gz压缩包,我用的是mysql 5.5.55版本,用xshell上传到root用户svr目录下
   解压安装包
   tar zxvf mysql-5.5.55-linux2.6-x86_64.tar.gz
   个人习惯使用软链接到mysql目录下
   ln -s mysql-5.5.55-linux2.6-x86_64 mysql
   #创建mysql与用户组,-s /bin/false 表示该用户不能登录
   groupadd mysql 
   useradd -r -g mysql -s /bin/false mysql
   #进入到mysql目录下
   cd mysql
   #为mysql用户添加权限
   chown -R mysql ./
   chgrp -R mysql ./
   #创建data目录并添加权限
   mkdir -p /data/mysql
   chown -R mysql:mysql /data/mysql
   #拷贝mysql配置文件
   cp support-files/my-medium.cnf /etc/my.cnf
   #修改my.cnf文件
   basedir = /svr/mysql
   datadir = /data/mysql
   character-set-server=utf8
   #拷贝配置文件
   cp support-files/mysql.server /etc/init.d/mysqld
   #初始化mysql库
   ./scripts/mysql_install_db --user=mysql --basedir=/svr/mysql --datadir=/data/mysql
   #添加mysql的环境变量
   vi /etc/profile
   PATH=/home/cbt/svr/mysql/bin:$PATH
   export PATH 
   #让刚才的修改生效
   source /etc/profile
   #启动及其它配置,启动数据库
   service mysqld start
 
   #开机启动
   chkconfig mysqld on


   #初始化mysql的一些设置
   mysql_secure_installation
   #回车
   Enter current password for root (enter for none):
   #y,设置mysql的root密码
   Set root password?[Y/n] y
   #以下都yes
   Remove anonymous users?[Y/n] y
   Disallow root login remotely?[Y/n] y
   Remove test database and access to it?[Y/n] y
   Reload privilege tables now?[Y/n] y
   ThanksforusingMySQL!看到这句话就代表mysql安装成功了!
   
   最后设置允许mysql远程登录
   允许远程登陆
   mysql> use mysql;   
   mysql> select host,user,password from user;   
   mysql> update user set password=password('123456') where user='root';  
   mysql> update user set host='%' where user='root' and host='localhost';  
   mysql> flush privileges; 

   

二、mysql一主多从配置

    这里我准备了两台虚拟机,ip分别为192.168.60.20和192.168.60.21,克隆完成。192.168.60.20这台机器为主库,192.168.60.21为从库,然后配置master的my.cnf文件,vi /etc/my.cnf,加入需要同步的数据库
   #需要同步的二进制数据库名;
   binlog-do-db=gaochao
   #不同步的二进制数据库名,如果不设置可以将其注释掉;
   binlog-ignore-db=information_schema
   binlog-ignore-db=mysql
   binlog-ignore-db=performance_schema
   修改了my.cnf配置我们需要重启mysql服务
   service mysqld restart
   #以下参数可选----------
   #binlog 格式 
   binlog-format=ROW 
   log-bin=mysql-master-bin 
   #slave更新时是否记录到日志中;
   log-slave-updates=true
   #开启半同步,需要另外安装插来支持 
   rpl_semi_sync_master_enabled=ON
   
   通过命令行登录管理MySQL服务器
   mysql -uroot -p


  授权给从数据库服务器192.168.60.21
  mysql> grant replication slave,super,reload on *.* to slave1@192.168.60.21 identified by '123456';
  查询主数据库状态
  mysql> show master status;
  +------------------+----------+--------------+---------------------------------------------+
  | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB                            |
  +------------------+----------+--------------+---------------------------------------------+
  | mysql-bin.000007 |     1698 | gaochao      | information_schema,mysql,performance_schema |
  +------------------+----------+--------------+---------------------------------------------+
  1 row in set (0.00 sec)
  配置从服务器,进入192.168.60.21
  vi /etc/my.cnf,加入以下配置
  server-id = 2
  log-bin=mysql-slave-bin
  replicate-do-db=gaochao
  replicate-ignore-db=information_schema
  replicate-ignore-db=mysql
  replicate-ignore-db=performance_schema
  这里我们要注意server-id要与master节点不一样,master节点server-id为1,配置完重启mysql服务
  slave 节点修改master 配置
  mysql>change master to master_host='192.168.60.20', master_user='slave1', master_password='123456'
  #启动slave
  mysql>start slave
  #查看slave的状态
  mysql>show slave status\G;
  *************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.60.20
                  Master_User: slave1
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 1698
               Relay_Log_File: bogon-relay-bin.000013
                Relay_Log_Pos: 1844
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: gaochao
          Replicate_Ignore_DB: information_schema,mysql,performance_schema
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 1698
              Relay_Log_Space: 2146
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
  Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
 1 row in set (0.00 sec)
 其中Slave_IO_Running 与 Slave_SQL_Running 的值都必须为YES,才表明状态正常。

 此时尝试向主库的user表插入一条数据,从库直接从主库同步此条数据,这是表明主从复制成功。



三、mysql读写分离实现

  读写分离楼主这里用的是360 Atlas,详细介绍可查看http://www.jianshu.com/p/b68e429d09c7。它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bug,添加了很多功能特性。目前该项目在360公司内部得到了广泛应用,很多MySQL业务已经接入了Atlas平台,每天承载的读写请求数达几十亿条。
  下载Atlas-2.2.1.el6.x86_64.rpm并安装,楼主这里在master节点上进行安装
  rpm -i  Atlas-2.2.1.el6.x86_64.rpm,卸载可用rpm -e  Atlas-2.2.1.el6.x86_64.rpm命令,默认安装到 /usr/local/mysql-proxy/ 当中,进入到mysql-proxy下conf目录,修改test.cnf文件
  #带#号的为非必需的配置项目
 #管理接口的用户名
 admin-username = root
 #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
 proxy-backend-addresses =192.168.60.20:3306
 #Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分 隔
 proxy-read-only-backend-addresses = 192.168.60.21:3306@1
 #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其  替换为你的MySQL的用户名和加密密码!
pwds = root:/iZxz+0GRoA=
#设置Atlas的运行方式,设为true时为守护进程方式,设为false时为前台方式,一般开发调试时设为false,线上运行时设为true,true后面不能>有空格。
daemon = true
#设置Atlas的运行方式,设为true时Atlas会启动两个进程,一个为monitor,一个为worker,monitor在worker意外退出后会自动将其重启,设为false时只有worker,没有monitor,一般开发调试时设为false,线上运行时设为true,true后面不能有空格。
keepalive = true
#工作线程数,对Atlas的性能有很大影响,可根据情况适当设置
event-threads = 8
#日志级别,分为message、warning、critical、error、debug五个级别
log-level = message
#日志存放的路径
log-path = /usr/local/mysql-proxy/log

#SQL日志的开关,可设置为OFF、ON、REALTIME,OFF代表不记录SQL日志,ON代表记录SQL日志,REALTIME代表记录SQL日志且实时写入磁盘,默认>为OFF
sql-log = ON
启动与关闭Atlas
./usr/local/mysql-proxy/bin/mysql-proxyd test start
./usr/local/mysql-proxy/bin/mysql-proxyd test stop

这是我们在navicat客户端连接代理库,端口为1234


操作代理类中的数据,查询user 表数据,删除user表数据,向user表插入数据,进去mysql-proxy下log,打印输出日志

cd /usr/local/mysql-proxy/log

tail -f sql_test.log 



从上图可以看出读数据,从192.168.60.21这个slave读取数据,当我们执行写入操作时,操作的是192.168.60.20这个master节点,就这正真实现了读写分离。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值