主要功能:
* 读写分离
* 从库负载均衡
* IP过滤
* SQL语句黑白名单
* 自动分表
注:1.在centos中下载mysql,会出现无法登陆的问题,下面有解决方法。
2.配置主从的时候,my.cnf配置文件需要注意
3.主从配置好之后,如果从库的状态是connecting,可能是以下问题
主从同步出现下面的错误:
Slave_IO_Running: Connecting
Slave_SQL_Running: Yes
解决方法:
导致lave_IO_Running 为connecting 的原因主要有以下 4个方面:
1、 网络不通
2、 密码不对
3、 pos不对
4、 是否那个容器的mysql服务停止了
4.atlas下载安装之后,如果登不上去,主要检查配置文件。
5.搭建时需要细心,理解配置文件,一点都不难
开始搭建
1.docker 创建容i器(需要几个就run 几次,这里需要三个)
docker run -i -t centos:6 (启动并创建一个容器) 缺点 没有端口映射外部无法访问
docker run -p 0.0.0.0:8080:80 -i -t sentos:6 /bin/bash
2. 在centos 容器中下载mysql (三个容器都需装mysql)
yum install wget
wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
rpm -ivh mysql-community-release-el6-5.noarch.rpm
yum install mysql-server
mysql 安装好了
启动mysql服务
service mysqld start
centos默认没有密码
mysql -u root
首先进入MySQL数据库中,创建用户“buck”设置密码为“hello”下列标红的是用户与密码。
grant all on *.* to buck@'127.0.0.1' identified by "hello";
修改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 | +------+-----------------------+
更新数据库信息,如果没更新数据库的信息,修改不会立即生效,那就需要重启数据库了。这边直接更新数据库的信息,可以避免重启。
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
退出数据库
exit;
此时在用刚刚授权的用户登录
mysql -u buck -p
hello
你会发现无法登陆,即使输入正确的密码你也无法登陆
从新用root用户登陆
mysql -u root
use mysql
delete from mysql.user where host='localhost' and user='';
flush privileges;
exit;
这时在登陆就好用了;
配置主库
配置主从服务器需要编写MySQL的配置文件,详情配置步骤如下:
vim /etc/my.cnf
#主从复制配置 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
重启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)
查看主数据库信息,记住下面的“File”与“Position”的信息,它们是用来配置从数据库的关键信息。可以看到下面同步的数据库的“test”数据库,主从数据库一样
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)
配置从库
也需要使用vim进行配置,只需要在[mysqld]下面加入server-id=2就可以,
server-id=2
进入数据库,配置从数据库的信息,这里输入刚才记录下来的“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
Atlas 配置
下载非分表的安装包
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
下载好了之后,进行安装
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
安装好了,它会默认在”/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
#管理接口的用户名 admin-username = user #管理接口的密码 admin-password = pwd
#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密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码! pwds = buck:RePBqJ+5gI4=
#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 -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,用户名,密码就可以
. 读写分离测试
public class test {
// 创建静态全局变量
static Connection conn;
static Statement st;
public static void main(String[] args) {
// insert(); //插入添加记录
// update(); //更新记录数据
// delete(); //删除记录
query(); //查询记录并显示
}
/* 插入数据记录,并输出插入的数据记录数*/
public static void insert() {
conn = getConnection(); // 首先要获取连接,即连接到数据库
try {
String sql = "INSERT INTO love(id, name, pwd)"
+ " VALUES ('1', 32, 'M')"; // 插入数据的sql语句
st = (Statement) conn.createStatement(); // 创建用于执行静态sql语句的Statement对象
int count = st.executeUpdate(sql); // 执行插入操作的sql语句,并返回插入数据的个数
System.out.println("向yl表中插入 " + count + " 条数据"); //输出插入操作的处理结果
conn.close(); //关闭数据库连接
} catch (SQLException e) {
System.out.println("插入数据失败" + e.getMessage());
}
}
/* 更新符合要求的记录,并返回更新的记录数目*/
public static void update() {
conn = getConnection(); //同样先要获取连接,即连接到数据库
try {
String sql = "update love set id='111' where name = '32'";// 更新数据的sql语句
st = (Statement) conn.createStatement(); //创建用于执行静态sql语句的Statement对象,st属局部变量
int count = st.executeUpdate(sql);// 执行更新操作的sql语句,返回更新数据的个数
System.out.println("yl表中更新 " + count + " 条数据"); //输出更新操作的处理结果
conn.close(); //关闭数据库连接
} catch (SQLException e) {
System.out.println("更新数据失败");
}
}
/* 查询数据库,输出符合要求的记录的情况*/
public static void query() {
conn = getConnection(); //同样先要获取连接,即连接到数据库
try {
String sql = "select * from love"; // 查询数据的sql语句
st = (Statement) conn.createStatement(); //创建用于执行静态sql语句的Statement对象,st属局部变量
ResultSet rs = st.executeQuery(sql); //执行sql查询语句,返回查询数据的结果集
System.out.println("最后的查询结果为:");
while (rs.next()) { // 判断是否还有下一个数据
// 根据字段名获取相应的值
String name = rs.getString("name");
String id = rs.getString("id");
String pwd = rs.getString("pwd");
//输出查到的记录的各个字段的值
System.out.println(name + " " + pwd + " " + id +"");
}
conn.close(); //关闭数据库连接
} catch (SQLException e) {
System.out.println("查询数据失败");
}
}
/* 删除符合要求的记录,输出情况*/
public static void delete() {
conn = getConnection(); //同样先要获取连接,即连接到数据库
try {
String sql = "delete from yl where name = 'lili'";// 删除数据的sql语句
st = (Statement) conn.createStatement(); //创建用于执行静态sql语句的Statement对象,st属局部变量
int count = st.executeUpdate(sql);// 执行sql删除语句,返回删除数据的数量
System.out.println("staff表中删除 " + count + " 条数据\n"); //输出删除操作的处理结果
conn.close(); //关闭数据库连接
} catch (SQLException e) {
System.out.println("删除数据失败");
}
}
/* 获取数据库连接的函数*/
public static Connection getConnection() {
Connection con = null; //创建用于连接数据库的Connection对象
try {
Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动
con = DriverManager.getConnection(
"jdbc:mysql://192.168.218.138:8882/yl", "yl", "yl");// 创建数据连接atlas的数据库
} catch (Exception e) {
System.out.println("数据库连接失败" + e.getMessage());
}
return con; //返回所建立的数据库连接
}