zoukankan      html  css  js  c++  java
  • MySQL环境部署

    阅读目录

    1. 数据库用户配置

    2. 主从数据库连接

    3. Atlas配置

    4. 读写分离测试

     

    序章

    Atlas是360团队弄出来的一套基于MySQL-Proxy基础之上的代理,修改了MySQL-Proxy的一些BUG,并且优化了很多东西。而且安装方便。配置的注释写的蛮详细的,都是中文。英文不好的同学有福了。

    Atlas官方链接: https://github.com/Qihoo360/Atlas/blob/master/README_ZH.md

    Atlas下载链接: https://github.com/Qihoo360/Atlas/releases

    环境:

    系统

    IP

    配置

    CentOS 6.7

    192.168.246.143

    Atlas代理服务

    CentOS 6.7

    192.168.246.134

    主MySQL数据库

    CentOS 6.7

    192.168.246.135

    从MySQL数据库

     

    1. 数据库的配置

    需要进入134与135数据库中配置用户名与密码,用户必须是远程可以访问的用户,配置方法如下:

    首先进入到134的MySQL数据库中,创建用户“buck”设置密码为“hello”下列标红的是用户与密码。

    mysql> grant all on *.* to buck@'127.0.0.1' identified by "hello";
    Query OK, 0 rows affected (0.00 sec)

    修改buck的访问权限,首先得进入mysql数据库,才能修改host权限的信息

    复制代码
    # 进入数据库
    mysql> use mysql
    Database changed
    

    修改host权限为"%"

    mysql> update user set host = '%' where user = 'buck';
    Query OK,
    1 row affected (0.00 sec)
    Rows matched:
    1 Changed: 1 Warnings: 0

    复制代码

    查看一下user表,看看修改成功了没有。可以看到,buck的用户,host已经修改成百分号了。

    复制代码
    mysql> select user, host from user;
    +------+-----------------------+
    | user | host                  |
    +------+-----------------------+
    | buck | %                     |
    | root | 127.0.0.1             |
    |      | localhost             |
    | root | localhost             |
    |      | localhost.localdomain |
    | root | localhost.localdomain |
    +------+-----------------------+
    6 rows in set (0.00 sec)
    复制代码

    更新数据库信息,如果没更新数据库的信息,修改不会立即生效,那就需要重启数据库了。这边直接更新数据库的信息,可以避免重启。

    mysql>  flush privileges;
    Query OK, 0 rows affected (0.00 sec)

    主从MySQL都需要创建一个数据库,我这创建的数据库是“test”,为了方便测试读写分离

    mysql> create database test;
    Query OK, 1 row affected (0.00 sec)

    【主意:135数据库与134的数据库同样配置, 记得要创建同样的数据库哦】

     

    2. 主从数据库连接

    配置主从服务器需要编写MySQL的配置文件,详情配置步骤如下:

    主服务器 ( 192.168.246.134 ),使用vim进行配置

    复制代码
    [mysqld]  
    datadir=/data/mysql  
    socket=/var/lib/mysql/mysql.sock  
    user=mysql  
    

    主从复制配置

    innodb_flush_log_at_trx_commit=1
    sync_binlog
    =1

    需要备份的数据库

    binlog-do-db=test

    不需要备份的数据库

    binlog-ignore-db=mysql

    启动二进制文件

    log-bin=mysql-bin

    服务器ID

    server-id=1

    Disabling symbolic-links is recommended to prevent assorted security risks

    symbolic-links=0

    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid
    -file=/var/run/mysqld/mysqld.pid

    复制代码

    【主意:若没有配置binlog-do-db和binlog_ignore_db,表示备份全部数据库。】

    重启mysqld服务

    [root@localhost bin]# /etc/init.d/mysqld restart

    进入数据库,配置主从复制的权限

    mysql> grant replication slave on *.* to 'buck'@'127.0.0.1' identified by 'hello';
    Query OK, 0 rows affected (0.00 sec)

    锁定数据库

    mysql> flush tables with read lock;
    Query OK, 0 rows affected (0.00 sec)

    查看主数据库信息,记住下面的“File”与“Position”的信息,它们是用来配置从数据库的关键信息。可以看到下面同步的数据库的“test”数据库,主从数据库如果数据不一样,首先需要手动去同步一下数据,我在Windows环境下只用Navicat复制的数据,这里就不演示了。

    复制代码
    mysql> show master status;
    +------------------+----------+--------------+------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    +------------------+----------+--------------+------------------+
    | mysql-bin.000002 | 17620976 | test         | mysql            |
    +------------------+----------+--------------+------------------+
    1 row in set (0.00 sec)
    复制代码

    从服务器 ( 192.168.246.135 ),也需要使用vim进行配置,只需要在[mysqld]下面加入server-id=2就可以,全部配置如下:

    复制代码
    [mysqld]
    datadir=/var/lib/mysql
    socket=/var/lib/mysql/mysql.sock
    user=mysql
    

    server-id=2

    Disabling symbolic-links is recommended to prevent assorted security risks

    symbolic-links=0

    [mysqld_safe]
    log-error=/var/log/mysqld.log
    pid
    -file=/var/run/mysqld/mysqld.pid

    复制代码

    进入数据库,配置从数据库的信息,这里输入刚才记录下来的“File”与“Position”的信息,并且在从服务器上执行:

    mysql> change master to master_host='192.168.246.134'

    主服务器的IP

    master_user='buck'

    配置主服务器的用户名

    master_password='hello'

    对应用户的密码

    master_port=3306

    主服务器的mysql端口

    master_log_file='mysql-bin.000002'

    日志文件的名称,需要与主服务器对应

    master_log_pos=17620976

    日志位置,需要与主服务器对应

    master_connect_retry=10

    重连次数

    复制代码
    mysql> change master to master_host='192.168.246.134',
        -> master_user='buck',
        -> master_password='hello',
        -> master_port=3306,
        -> master_log_file='mysql-bin.000002',
        -> master_log_pos=17620976,
        -> master_connect_retry=10;
    Query OK, 0 rows affected (0.01 sec)
    复制代码

    启动进程

    mysql> start slave;
    Query OK, 0 rows affected (0.00 sec)

    检查主从复制状态,要看到下列标红的信息中,两个都是Yes,才说明主从连接正确,如果有一个是No,需要重新确定刚才记录的日志信息,停掉“stop slave”重新进行配置主从连接。

    复制代码
    mysql> show slave status G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.246.134
                      Master_User: buck
                      Master_Port: 3306
                    Connect_Retry: 10
                  Master_Log_File: mysql-bin.000002
              Read_Master_Log_Pos: 17620976
                   Relay_Log_File: mysqld-relay-bin.000002
                    Relay_Log_Pos: 251
            Relay_Master_Log_File: mysql-bin.000002
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 
               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: 17620976
                  Relay_Log_Space: 407
                  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: 
    1 row in set (0.00 sec)
    

    ERROR:
    No query specified

    复制代码

     

    3. Atlas配置

    下载Atlas会有两个版本,其中有个分表的版本,但是这个需要其他的依赖,我这边不需要分表这种需求,所以安装普通的版本

         Atlas (普通) : Atlas-2.2.1.el6.x86_64.rpm

         Atlas (分表) : Atlas-sharding_1.0.1-el6.x86_64.rpm

       

    首先进入Linux的Home目录下,下载非分表的安装包

    [root@localhost ~]# cd /home/
    [root@localhost home]# wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm

    下载好了之后,进行安装

    [root@localhost home]# rpm -ivh Atlas-2.2.1.el6.x86_64.rpm 
    Preparing...                ########################################### [100%]
       1:Atlas                  ########################################### [100%]

    安装好了,它会默认在”/usr/local/mysql-proxy”下给你生成4个文件夹,以及需要配置的文件,如下:

    复制代码
    [root@localhost home]# ll /usr/local/mysql-proxy/
    total 16
    drwxr-xr-x. 2 root root 4096 Dec 28 10:47 bin
    drwxr-xr-x. 2 root root 4096 Dec 28 10:47 conf
    drwxr-xr-x. 3 root root 4096 Dec 28 10:47 lib
    drwxr-xr-x. 2 root root 4096 Dec 17  2014 log
    复制代码

    bin目录下放的都是可执行文件

    1. “encrypt”是用来生成MySQL密码加密的,在配置的时候会用到

    2. “mysql-proxy”是MySQL自己的读写分离代理

    3. “mysql-proxyd”是360弄出来的,后面有个“d”,服务的启动、重启、停止。都是用他来执行的

    conf目录下放的是配置文件

    1. “test.cnf”只有一个文件,用来配置代理的,可以使用vim来编辑

    lib目录下放的是一些包,以及Atlas的依赖

    log目录下放的是日志,如报错等错误信息的记录

    进入bin目录,使用encrypt来对数据库的密码进行加密,我的MySQL数据的用户名是buck,密码是hello,我需要对密码进行加密

    [root@localhost bin]# ./encrypt hello
    RePBqJ+5gI4=

    配置Atlas,使用vim进行编辑

    [root@localhost conf]# cd /usr/local/mysql-proxy/conf/
    [root@localhost conf]# vim test.cnf

    进入后,可以在Atlas进行配置,360写的中文注释都很详细,根据注释来配置信息,其中比较重要,需要说明的配置如下:

    这是用来登录到Atlas的管理员的账号与密码,与之对应的是“#Atlas监听的管理接口IP和端口”,也就是说需要设置管理员登录的端口,才能进入管理员界面,默认端口是2345,也可以指定IP登录,指定IP后,其他的IP无法访问管理员的命令界面。方便测试,我这里没有指定IP和端口登录。

    复制代码
    #管理接口的用户名
    admin-username = user
    

    管理接口的密码

    admin-password = pwd

    复制代码

    这是用来配置主数据的地址与从数据库的地址,这里配置的主数据库是135,从数据库是134

    复制代码
    #Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
    proxy-backend-addresses = 192.168.246.134:3306
    

    Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔

    proxy-read-only-backend-addresses = 192.168.246.135:3306@1

    复制代码

    这个是用来配置MySQL的账户与密码的,我的MySQL的用户是buck,密码是hello,刚刚使用Atlas提供的工具生成了对应的加密密码

    #用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
    pwds = buck:RePBqJ+5gI4=

    这是设置工作接口与管理接口的,如果ip设置的”0.0.0.0”就是说任意IP都可以访问这个接口,当然也可以指定IP和端口,方便测试我这边没有指定,工作接口的用户名密码与MySQL的账户对应的,管理员的用户密码与上面配置的管理员的用户密码对应。

    复制代码
    #Atlas监听的工作接口IP和端口
    proxy-address = 0.0.0.0:1234
    

    Atlas监听的管理接口IP和端口

    admin-address = 0.0.0.0:2345

    复制代码

    启动Atlas

    [root@localhost bin]# ./mysql-proxyd test start
    OK: MySQL-Proxy of test is started

    测试一下Atlas服务器的MySQL状态,要确认它是关闭状态,并且使用mysql命令,进不去数据库

    复制代码
    [root@localhost bin]# /etc/init.d/mysqld status
    mysqld is stopped
    
    [root@localhost bin]# mysql
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    复制代码

    确认系统中自带的MySQL进不去了,使用如下命令,进入Atlas的管理模式“mysql -h127.0.0.1 -P2345 -uuser -ppwd ”,能进去说明Atlas正常运行着呢,因为它会把自己当成一个MySQL数据库,所以在不需要数据库环境的情况下,也可以进入到MySQL数据库模式。

    复制代码
    [root@localhost bin]# mysql -h127.0.0.1 -P2345 -uuser -ppwd
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 1
    Server version: 5.0.99-agent-admin
    

    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql>

    复制代码

    可以访问“help”表,来看MySQL管理员模式都能做些什么。可以使用SQL语句来访问

    复制代码
    mysql> select * from help;
    +----------------------------+---------------------------------------------------------+
    | command                    | description                                             |
    +----------------------------+---------------------------------------------------------+
    | SELECT * FROM help         | shows this help                                         |
    | SELECT * FROM backends     | lists the backends and their state                      |
    | SET OFFLINE $backend_id    | offline backend server, $backend_id is backend_ndx's id |
    | SET ONLINE $backend_id     | online backend server, ...                              |
    | ADD MASTER $backend        | example: "add master 127.0.0.1:3306", ...               |
    | ADD SLAVE $backend         | example: "add slave 127.0.0.1:3306", ...                |
    | REMOVE BACKEND $backend_id | example: "remove backend 1", ...                        |
    | SELECT * FROM clients      | lists the clients                                       |
    | ADD CLIENT $client         | example: "add client 192.168.1.2", ...                  |
    | REMOVE CLIENT $client      | example: "remove client 192.168.1.2", ...               |
    | SELECT * FROM pwds         | lists the pwds                                          |
    | ADD PWD $pwd               | example: "add pwd user:raw_password", ...               |
    | ADD ENPWD $pwd             | example: "add enpwd user:encrypted_password", ...       |
    | REMOVE PWD $pwd            | example: "remove pwd user", ...                         |
    | SAVE CONFIG                | save the backends to config file                        |
    | SELECT VERSION             | display the version of Atlas                            |
    +----------------------------+---------------------------------------------------------+
    16 rows in set (0.00 sec)
    

    mysql>

    复制代码

    也可以使用工作接口来访问,使用命令“mysql -h127.0.0.1 -P1234 -ubuck -phello”

    复制代码
    [root@localhost bin]# mysql -h127.0.0.1 -P1234 -ubuck -phello
    Welcome to the MySQL monitor.  Commands end with ; or g.
    Your MySQL connection id is 1
    Server version: 5.0.81-log
    

    Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

    mysql>

    复制代码

    如果工作接口可以进入了,就可以在Windows平台下,使用Navicat来连接数据库,填写对应的host,Port,用户名,密码就可以

    image_thumb

     

     

    4. 读写分离测试

    这里测试读写分离需要使用到Jmeter了,它是Java写第一套开源的压力测试工具,因为这个比较方便。他有专门测试MySQL的模块,需要使用MySQL的JDBC驱动jar包,配置很简单,东西很好很强大很好用。

    Jmeter下载地址:http://jmeter.apache.org/download_jmeter.cgi

    MySQL的JDBC  :http://dev.mysql.com/downloads/connector/j/

    下载下来后,分别都解压开来,打开Jmeter ( 在bin路面下的jmeter.bat ) ,在测试计划中,导致JDBC的jar包

    image_thumb2

    配置JDBC的驱动

    image_thumb4

    分别做查询与插入语句

    image_thumb3

    配置好了以后,就先运行查询操作,然后分别监控主数据库与从数据库所在机器的流量,来确定是否读写,使用“sar -n DEV 1 10000”命令来监控读写

    先来测试写,目前数据库里面一条信息都没有,开启配置好了的Jmeter,进行写入数据测试

    image_thumb10

    主数据库 ( 192.168.246.134 )

    从数据库 ( 192.168.246.135 )

    可以看到测试插入数据的操作时,主数据库的网卡流量很大,从数据库的流量很小,是应为主数据是主要负责写入的,而从数据库主要是负责同步的。

    image_thumb9

    查看数据库,发现已经插入了6W多条数据了。

    image_thumb11

    进行读取数据的测试,只需要执行查询就好,执行“select *from sbtest;”来查询数据表

    主数据库 ( 192.168.246.134 )

    从数据库 ( 192.168.246.135 )

    可以看到135数据库的流量非常大,134没有什么流量,这下就可以确定了数据是从数据库读取的。已经实现了读写分离。

    image_thumb12

  • 相关阅读:
    centos 下建用户 shell编程
    vmware centos下配置ip
    centos7下快速安装mysql
    CentOS-7.0.中安装与配置Tomcat-7的方法
    CentOS下安装JDK1.7
    CentOS6.5安装配置SVN
    Spring mvc 下Ajax获取JSON对象问题 406错误
    Freemarker中通过request获得contextPath
    java进阶书籍推荐
    致命错误:jemalloc/jemalloc.h:没有那个文件或目录
  • 原文地址:https://www.cnblogs.com/R-bear/p/15048871.html
Copyright © 2011-2022 走看看