生产环境实践Mysql5.7主从+Atlas实现读写分离


Mysql主从搭建

主从复制可以使MySQL数据库主服务器的主数据库,复制到一个或多个MySQL从服务器从数据库,默认情况下,复制异步; 根据配置,可以复制数据库中的所有数据库,选定的数据库或甚至选定的表。
MySQL的工作方式是单进程多线程的方式,那么线程的多寡则会极大的影响到MySQL的效率,而在早期MySQL的主从都是由单线程进行的,使得主从复制除了相关的客观因素外还受到自身的影响;为此在MySQL的5.7版本中对多线程主从复制来进一步的改善,在MySQL 5.7中是按照逻辑时钟(类似CPU的处理机制)来处理多线程,甚至在半同步复制semisync中还是使用Performance Schema表来监控复制线程;

先卸载系统自带的mariadb数据库

     yum -y remove mariadb*

Yum安装Percona

     wget https://www.percona.com/downloads/Percona-Server-LATEST/Percona-Server-5.7.22-22/binary/redhat/7/x86_64/Percona-Server-server-57-5.7.22-22.1.el7.x86_64.rpm
     yum install -y  Percona-Server-server-57-5.7.22-22.1.el7.x86_64.rpm

192.168.253.188(主库)配置文件

[client]
default_character_set = utf8mb4
socket=/var/lib/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/lib/mysql/mysql.sock
basedir = /data/local/percona5.7.22
datadir = /data/local/percona5.7.22/data
pid_file = /var/lib/mysql/mysql-pid.pid
character_set_server = utf8mb4
default_storage_engine = InnoDB
explicit_defaults_for_timestamp
federated
skip-name-resolve
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON

#Innodb
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_lock_wait_timeout = 100
innodb_log_file_size = 1024M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_thread_concurrency = 8
innodb_buffer_pool_size = 5G
innodb_read_io_threads = 24
innodb_write_io_threads = 24
log_bin_trust_function_creators=1
innodb_page_cleaners=8
innodb_lru_scan_depth=256

innodb_locks_unsafe_for_binlog = 1
innodb_autoinc_lock_mode = 2
sql_mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
group_concat_max_len = 18446744073709551615

# MyISAM #
key_buffer_size = 1344M
myisam_recover_options = FORCE,BACKUP
lower_case_table_names=1
event_scheduler=1

# SAFETY #
max_allowed_packet = 1024M
max_connect_errors = 1000000
skip_name_resolve = 1

# Binary Logging #
server_id = 200
log_bin = mysql-bin
binlog_format = ROW
sync_binlog = 1

# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
max_connections = 1000
thread_cache_size = 50
open_files_limit = 65535
table_definition_cache = 4096
table_open_cache = 5000

# LOGGING #
log_error = /data/logs/mysql/mysql-error.log
log_queries_not_using_indexes = 0
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/logs/mysql/mysql-slow.log

# REPLICATION #
relay_log = relay-bin
slave_net_timeout = 60
symbolic-links = 0
transaction_isolation = READ-COMMITTED 

[mysql]
no-auto-rehash
default_character_set = utf8mb4

[xtrabackup]
default-character-set = utf8mb4

192.168.253.189(从库,190从库也是一样配置)配置文件

     [client]
default_character_set = utf8mb4
socket=/var/lib/mysql/mysql.sock

[mysqld]
port=3306
socket=/var/lib/mysql/mysql.sock
basedir = /data/local/percona5.7.22
datadir = /data/local/percona5.7.22/data
pid_file = /var/lib/mysql/mysql-pid.pid
character_set_server = utf8mb4
default_storage_engine = InnoDB
explicit_defaults_for_timestamp
federated
skip-name-resolve
gtid-mode = ON
enforce-gtid-consistency = ON
log-slave-updates = ON
slave_type_conversions="ALL_NON_LOSSY"

#Innodb
innodb_flush_method = O_DIRECT
innodb_log_files_in_group = 2
innodb_lock_wait_timeout = 100
innodb_log_file_size = 1024M
innodb_flush_log_at_trx_commit = 1
innodb_file_per_table = 1
innodb_thread_concurrency = 8
innodb_buffer_pool_size = 5G
innodb_read_io_threads = 24
innodb_write_io_threads = 24
log_bin_trust_function_creators=1
innodb_page_cleaners=4
innodb_lru_scan_depth=256
innodb_buffer_pool_instances=2

innodb_locks_unsafe_for_binlog = 1
innodb_autoinc_lock_mode = 2
sql_mode=NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
group_concat_max_len = 18446744073709551615

# MyISAM #
key_buffer_size = 1344M
myisam_recover_options = FORCE,BACKUP
lower_case_table_names=1
event_scheduler=1
slave_pending_jobs_size_max = 1344M

# SAFETY #
max_allowed_packet = 512M
max_connect_errors = 1000000
skip_name_resolve = 1
slave-skip-errors=1007,1008,1032,1062

# Binary Logging #
server_id = 201
log_bin = mysql-bin
binlog_format = ROW
sync_binlog = 1

# CACHES AND LIMITS #
tmp_table_size = 32M
max_heap_table_size = 32M
max_connections = 1000
thread_cache_size = 50
open_files_limit = 65535
table_definition_cache = 4096
table_open_cache = 5000

# LOGGING #
log_error = /data/logs/mysql/mysql-error.log
log_queries_not_using_indexes = 0
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /data/logs/mysql/mysql-slow.log

# REPLICATION #
relay_log = relay-bin
slave_net_timeout = 60
symbolic-links = 0

# slave
slave-parallel-type=LOGICAL_CLOCK
slave-parallel-workers=16
master_info_repository=TABLE
relay_log_info_repository=TABLE
relay_log_recovery=ON

[mysql]
no-auto-rehash
default_character_set = utf8mb4

[xtrabackup]
default-character-set = utf8mb4

server_id三台数据库要设置成不同

启动mysql

systemctl start mysql.service

查询初始化密码

grep "password" /var/log/mysqld.log     

修改root密码

ALTER USER ‘root‘@‘localhost‘ IDENTIFIED BY ‘new_password‘;

使用Atlas实现读写分离
环境安装
yum -y install libevent glib2 lua gcc gcc-c++ autoconf mysql-devel libtool pkgconfig ncurses ncurses-devel libevent-devel

下载Atlas包

wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm

安装atlas

yum install Atlas-2.2.1.el6.x86_64.rpm -y
#ll /usr/local/mysql-proxy/
total 4
-rw-r--r-- 1 root root 402 Sep 11 14:59 drwxr-xr-x 2 root root  75 Sep 10 14:24 bin
drwxr-xr-x 2 root root  22 Sep 11 14:59 conf
drwxr-xr-x 3 root root 331 Sep 10 14:14 lib
drwxr-xr-x 2 root root  58 Sep 11 14:59 log

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

  1. “encrypt”是用来生成MySQL密码加密的,在配置的时候会用到
  2. “mysql-proxy”是MySQL自己的读写分离代理
  3. “mysql-proxyd”是360弄出来的,后面有个“d”,服务的启动、重启、停止。都是用他来执行的
  4. conf目录下放的是配置文件

“test.cnf”只有一个文件,用来配置代理的,可以使用vim来编辑
lib目录下放的是一些包,以及Atlas的依赖
log目录下放的是日志,如报错等错误信息的记录
使用encrypt来对数据库的密码进行加密,账号:aadminproxy,密码:123456

/usr/local/mysql-proxy/bin/encrypt 123456
XXfVpJOzMdITLHv26rAgv

配置atlas

vim /usr/local/mysql-proxy/conf/test.cnf
[mysql-proxy]
admin-username = root
admin-password = admin123
proxy-backend-addresses = 192.168.253.188:3306
proxy-read-only-backend-addresses = 192.168.253.189:[email protected],192.168.253.190:[email protected]
pwds = aadminproxy:XXfVpJOzMdITLHv26rAgv
daemon = true
keepalive = true
event-threads = 8
log-level = message
log-path = /usr/local/mysql-proxy/log
sql-log=ON
proxy-address = 0.0.0.0:8066
admin-address = 0.0.0.0:2345
charset=utf8

启动

/usr/local/mysql-proxy/bin/mysql-proxyd test start 


更新有趣IT资讯,干货,关注下面公众号吧,互联网爱好者必备公众号

原文地址:http://blog.51cto.com/11243465/2178786

时间: 2024-11-08 21:28:24

生产环境实践Mysql5.7主从+Atlas实现读写分离的相关文章

MySQL主从同步、读写分离配置步骤

现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master) 192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i686) using readline 5.1 1.登录Master服务器,修改my.cnf,添加如下内容: server-id = 1 //数据库ID号,

MySQL主从同步、读写分离配置步骤、问题解决笔记

根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记:       现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用.       为了避免不必要的麻烦,主从服务器MySQL版本尽量保持一致; 环境:192.168.0.1 (Master)           192.168.0.2 (Slave) MySQL Version:Ver 14.14 Distrib 5.1.48, for pc-linux-gnu (i6

使用Atlas实现读写分离

 Atlas实现读写分离 环境介绍: 试验机器4台: 192.168.0.41   Atlas 192.168.0.42   Master 192.168.0.43   Slave-1 192.168.0.44   Slave-2 4台机器均为RHEL 6.4 x86_64系统 [[email protected] ~]# uname -a Linux Nginx 2.6.32-358.el6.x86_64 #1 SMP Tue Jan 29 11:47:41 EST 2013 x86_64 x

【纯干货】Amoeba实现MySQL主从同步与读写分离

[纯干货]Amoeba实现MySQL主从同步与读写分离 一.简介 amoeba简介 Amoeba(变形虫)项目,该开源框架于2008年开始发布一款 Amoeba for Mysql软件.这个软件致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的 时候充当SQL路由功能,专注于分布式数据库代理层(Database Proxy)开发.座落与 Client.DB Server(s)之间,对客户端透明.具有负载均衡.高可用性.SQL 过滤.读写分离.可路由相关的到目标数据库.可并发

mysql主从同步、读写分离

一:所需服务器 服务器名称 运行服务 服务端口 Ip地址 T1 Nginx 80 192.168.11.158 负载均衡 T2 Nginx/httpd 80,873 192.168.11.157 实际网站服务 T3 Nginx/httpd 80,873 192.168.11.161 实际网站服务 T4 Nginx/httpd 80,873 192.168.11.162 实际网站服务 T5 Mysql 8066 192.168.11.163 Mysql读写分离 T6 mysql 3306 192.

Linux—Centos7.4之搭建Mysql数据库主从同步、读写分离

MySQL主从同步与读写分离 目录第一部分 实验环境第二部分 配置主从同步第三部分 配置读写分离第四部分 测试验证 第一部分 实验环境 实验拓扑图: 服务器五台:1)客户端服务器一台:IP地址:192.168.80.10(client)需安装软件:mysql-boost-5.7.20.tar.gz 2)Amoeba调度服务器一台:IP地址:192.168.80.20(amoeba)需安装软件:jdk-8u144-linux-x64.tar.gzamoeba-mysql-3.0.5-RC-dist

mariadb的主从同步和读写分离

前言 MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可 MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品.前提两台服务器,今天就要用它来搭建多服务器的主从同步和读写分离, 两台服务的环境是:192.168.47.128 centos7 mariadb10.3192.168.47.129 centos6.5 mariadb10.3下面开始搭建: mariadb安装 1.centos7下面的安装我以前有很多教程

配置 Mysql 读写分离+强制走写节点+根据主从延时的读写分离

配置 Mysql 读写分离+强制走写节点+根据主从延时的读写分离http://www.linuxmysql.com/14/2019/1008.htm 原文地址:https://blog.51cto.com/rscpass/2423421

生产环境可用mysql5.7.23安装全过程

生产环境安装5.7.23全过程 先执行initialization.sh优化脚本 详情见 Linux 在安装mysql前,需要先查看一下是否清空了系统自带了mysql我们这里需要删除系统自带了mysql lib 我们先把mysql放置在/opt下先上传与线上版本一致的 [[email protected] opt]# cat /etc/redhat-release CentOS release 6.5 (Final) [[email protected] opt]# ll total 62932