首页 > 数据库 >mysql集群高可用搭建

mysql集群高可用搭建

时间:2023-08-04 09:33:06浏览次数:32  
标签:log wsrep mysqld mysql 集群 size rpm 搭建

mysql galera 集群模式

        mysql节点多主模式

        对任意一个节点的mysql操作都会实时同步到集群中的所有节点mysql上  使mysql集群像是无状态对外提供服务

首节点安装

rpm -e postfix-2:2.10.1-9.el7.x86_64
rpm -e mariadb-libs-5.5.68-1.el7.x86_64


rpm -ivh mysql-wsrep-common-5.7-5.7.40-25.32.el7.x86_64.rpm
rpm -ivh mysql-wsrep-libs-5.7-5.7.40-25.32.el7.x86_64.rpm
rpm -ivh mysql-wsrep-client-5.7-5.7.40-25.32.el7.x86_64.rpm
rpm -ivh mysql-wsrep-libs-compat-5.7-5.7.40-25.32.el7.x86_64.rpm
rpm -ivh mysql-wsrep-server-5.7-5.7.40-25.32.el7.x86_64.rpm
rpm -ivh mysql-wsrep-5.7-5.7.40-25.32.el7.x86_64.rpm
rpm -ivh mysql-wsrep-devel-5.7-5.7.40-25.32.el7.x86_64.rpm --force --nodeps
rpm -ivh mysql-wsrep-test-5.7-5.7.40-25.32.el7.x86_64.rpm --force --nodeps
rpm -ivh galera-3-25.3.37-1.el7.x86_64.rpm

 

1.安装rsync

  rpm -ivh   rsync-2.8.el7.x86_64.rpm

 

创建用户和日志目录

       useradd mysql

       mkdir -p /data/logs/mysql/
       chown -R mysql:mysql /data/mysql/
       chown -R mysql:mysql /data/logs/mysql/

      2.初始化mysql

         部署完mysql后必须先初始化mysql系统数据库,否则mysqld服务无法正常启动

         mysql_install_db --no-defaults  --datadir=/data/mysql --user=mysql

      3.设置远程登录用户名和密码

       vi /etc/my.cnf

        

       systemctl start mysqld

       mysql –uroot –p

       >flush privileges;

       >GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '11111' WITH GRANT OPTION;

       >flush privileges;

     4.修改my.cnf

       首节点的my.cnf和其它扩展节点的my.cnf有区别 不能直接拷贝覆盖

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/data/logs/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid



server_id=5
binlog_format=row
default_storage_engine=InnoDB
innodb_file_per_table=1
innodb_autoinc_lock_mode=2

wsrep_on=ON
wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://'
wsrep_node_name='node5'
wsrep_node_address='10.32.3.5'
wsrep_sst_auth=root:aabbcc
wsrep_sst_method=rsync

#validate_password=off

#skip-grant-tables

# start
#max_connections 应设置为 1000,默认151
max_connections = 1000

#local_infile应设置为:OFF
local-infile=0

#log_slave_updates 应设置为 ON
log_slave_updates=1

#log_bin 应设置为 ON
log_bin = mysql-bin

#slow_query_log 应设置为 ON
slow_query_log = 1

#应设置 log_error 错误日志
#log_error = /home/mysql.err

#general_log 应设置为 ON
general_log = 1

#应以非管理员帐号权限运行 MySQL
#user=mysql

lower_case_table_names = 1

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

default-time_zone = '+8:00'
query_cache_type=1
query_cache_size=128M
max_allowed_packet=32M
tmp_table_size=96M
max_heap_table_size=96M
innodb_buffer_pool_size=64G
innodb_log_buffer_size=512M
innodb_thread_concurrency=48
my.cnf

    5.重启mysql

       systemctl restart mysqld

   6.查看启动日志

       

扩展节点安装

   1.添加第二个节点

      和首节点的安装部署步骤一致:

      1.安装所有的mysql rpm包

      2.安装rsync服务

      3.初始化mysql 系统数据库

      4.配置mysql的远程连接用户名和密码

      5.配置my.cnf   这个和首节点的my.cnf不同

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/data/logs/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid



server_id=71
binlog_format=row
default_storage_engine=InnoDB
innodb_file_per_table=1
innodb_autoinc_lock_mode=2

wsrep_on=ON
wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://10.32.3.5' //这里配置首节点的ip地址
wsrep_node_name='node71'
wsrep_node_address='10.30.92.71'
wsrep_sst_auth=root:aabbb
wsrep_sst_method=rsync

#validate_password=off


# 安全加固
#binlog-format=ROW
#log-bin=mysqlbinlog
#plugin-load=validate_password.so
#log_slave_updates=ON
#log_error=/data/logs/mysql/mysqld.log

# start
#max_connections 应设置为 1000,默认151
max_connections = 1000

#local_infile应设置为:OFF
local-infile=0

#log_slave_updates 应设置为 ON
log_slave_updates=1

#log_bin 应设置为 ON
log_bin = mysql-bin

#slow_query_log 应设置为 ON
slow_query_log = 1

#应设置 log_error 错误日志
#log_error = /home/mysql.err

#general_log 应设置为 ON
general_log = 1

#应以非管理员帐号权限运行 MySQL
#user=mysql

lower_case_table_names = 1

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

default-time_zone = '+8:00'
query_cache_type=1
query_cache_size=128M
max_allowed_packet=32M
tmp_table_size=96M
max_heap_table_size=96M
innodb_buffer_pool_size=64G
innodb_log_buffer_size=512M
innodb_thread_concurrency=48
View Code

      6.启动mysqld

        systemctl start mysqld

        启动扩展节点的mysqld服务后自动会连接首节点mysqld服务 自动组成mysql集群

  2.添加第三个节点

     和第二个节点的安装部署步骤一致:

      1.安装所有的mysql rpm包

      2.安装rsync服务

      3.初始化mysql 系统数据库

      4.配置mysql的远程连接用户名和密码

      5.配置my.cnf   这个和首节点的my.cnf不同

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/data/logs/mysql/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid



server_id=70
binlog_format=row
default_storage_engine=InnoDB
innodb_file_per_table=1
innodb_autoinc_lock_mode=2

wsrep_on=ON
wsrep_provider=/usr/lib64/galera-3/libgalera_smm.so
wsrep_cluster_name='galera'
wsrep_cluster_address='gcomm://10.32.3.5,10.30.92.71'
wsrep_node_name='node70'
wsrep_node_address='10.30.92.70'
wsrep_sst_auth=root:aabb
wsrep_sst_method=rsync

#validate_password=off


# 安全加固
#binlog-format=ROW
#log-bin=mysqlbinlog
#plugin-load=validate_password.so
#log_slave_updates=ON
#log_error=/data/logs/mysql/mysqld.log

# start
#max_connections 应设置为 1000,默认151
max_connections = 1000

#local_infile应设置为:OFF
local-infile=0

#log_slave_updates 应设置为 ON
log_slave_updates=1

#log_bin 应设置为 ON
log_bin = mysql-bin

#slow_query_log 应设置为 ON
slow_query_log = 1

#应设置 log_error 错误日志
#log_error = /home/mysql.err

#general_log 应设置为 ON
general_log = 1

#应以非管理员帐号权限运行 MySQL
#user=mysql

lower_case_table_names = 1

sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

default-time_zone = '+8:00'
query_cache_type=1
query_cache_size=128M
max_allowed_packet=32M
tmp_table_size=96M
max_heap_table_size=96M
innodb_buffer_pool_size=32G
innodb_log_buffer_size=512M
my.cnf

      6.启动mysqld服务

         systemctl start mysqld

    3.测试集群数据库同步

      1.登录三台的mysql服务

         mysql -uroot -h10.32.3.3 -P3306 -p

         mysql -uroot -h10.32.3.4 -P3306 -p

         mysql -uroot -h10.32.3.5 -P3306 -p

       2.测试数据

         在任何一台服务上创建一个新的数据库 然后到其它节点查看是否有新建的数据库

         > create database test;

         > show databases;

高可用反向代理

        如果不用反向代理 整个集群对外暴露出的服务地址为三个IP和端口  程序调用的时候需要填写三个地址 只配置一个的话就会造成集群主机负载不均衡,其它集群节点只起到一个数据备份的作用

       这种情况就适合通过haproxy服务对整个集群进行代理,整个集群通过haproxy的ip和端口对外进行暴露  程序调用的时候只需要配置haproxy的ip的端口 由haproxy对客户端连接进行转发

#---------------------------------------------------------------------
# 全局设定部分
#---------------------------------------------------------------------
global

defaults
        log     global
        mode    tcp
        option  dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000

#---------------------------------------------------------------------
# HAPROXY状态页面
#---------------------------------------------------------------------
listen admin_stats
   stats       enable
   # 监听端口
   bind        *:12345
   mode        http
   option      httplog
   log         global
   maxconn     10
   # 刷新间隔
   stats       refresh          30s
   # 页面路径
   stats       uri              /
   stats       realm            haproxy
   # 访问认证
   stats       auth             admin:password
   stats       hide-version

#---------------------------------------------------------------------
# TCP转发及负载均衡及简单TCP可用性校验
# weight权重;check inter检测频率;rise 1一次可用恢复;fall 2两次失败降级
#---------------------------------------------------------------------

frontend mysql-in
    bind     *:3307
    maxconn  80000
    default_backend mysqldb

backend mysqldb
    #使用HTTP对URI路径可用性进行检测
    #option httpchk
    #http-check send meth GET uri /check.html
    #http-check expect status 200

    server mysql-1    10.30.92.70:3306    maxconn 40480  weight 10  check inter 10s  rise 1 fall 2
    server mysql-2    10.30.92.71:3306    maxconn 40480  weight 10  check inter 10s  rise 1 fall 2
    server mysql-3    10.32.3.5:3306    maxconn 44440  weight 10  check inter 10s  rise 1 fall 2
haproxy.cfg

      数据库集群的访问地址配置为 haproxyIP:Port 192.168.30.90:3307

启动节点和重启节点注意事项

      重启扩展节点的mysqld服务的时候可能需要先重启首节点的mysqld服务才行 整个集群的mysqld服务启动顺序有依赖关系

      否则扩展节点重启的时候由于连接主节点超时导致扩展节点上mysqld的服务启动异常

 

标签:log,wsrep,mysqld,mysql,集群,size,rpm,搭建
From: https://www.cnblogs.com/yxh168/p/17605031.html

相关文章

  • MySQL配置通过systemctl管理
     MySQL配置通过systemctl管理 参考官方文档资料,步骤不多。创建mysqld.service文件。touch/usr/lib/systemd/system/mysqld.servicechmod644/usr/lib/systemd/system/mysqld.service  官方对MySQL5和MySQL8提供的mysqld.service内容并不同。/etc/sysconfig/mysq......
  • python + mysql
    1.连接mysql数据库,基本数据查询流程#1.连接conn=pymysql.connect(host='127.0.0.1',port=3306,user='root',password='',db='db8',charset='utf8')#2.创建游标cursor=conn.cursor()#注意%s需要加引号sql="select*fromu......
  • pyspark 环境搭建和相关操作redis ,es
    一.环境搭建1.创建虚拟环境,指定python包2.切换到虚拟环境,安装你所需要的python相关模块包3.把整个虚拟环境打成.zip4.将zip上传的hadfs5.spark-submit指定python包的路径可以参考 https://dandelioncloud.cn/article/details/1589470996832964609二.pyspark数据r......
  • MySQL查询语句(1)
    连接数据库mysql-hlocalhost-uroot-prootDQL-介绍DQL英文全称是DataQueryLanguage(数据查询语言),数据查询语言,用来查询数据库中表的记录查询SELECT字段列表FROM表名列表WHERE条件列表GROUPBY分组字段列表HAVING分组后条件列表ORDERBY排序字......
  • 【Azure K8S | AKS】在AKS集群中创建 PVC(PersistentVolumeClaim)和 PV(PersistentVol
    问题描述在AKS集群中创建PVC(PersistentVolumeClaim)和PV(PersistentVolume)示例 问题解答在AzureKubernetesService(AKS)的官方网站中,关于存储的选项介绍中,并没有具体的yaml实例来创建PV,PVC。特别是使用自定义的Disk的情况。本文将根据以上图片中的AzureManagedDisk+......
  • mysql插入报错java.sql.SQLException: Incorrect string value: '\xF0\x9F\x87\xA
    背景环境java8,centos7.9,mysql8.0.34新装的环境,默认给装了mysql8,想着与时俱进用下新版,结果插入就报错java.sql.SQLException:Incorrectstringvalue:'\xF0\x9F\x87\xA8\xF0\x9F...'forcolumn解决方法这个错误通常是由于MySQL数据库中的字符集不支持存储特定的字符或表情符......
  • InnoDB – the best storage engine for MySQL?
    https://dev.mysql.com/doc/refman/5.7/en/innodb-introduction.html InnoDBisageneral-purposestorageenginethatbalanceshighreliabilityandhighperformance.InnoDB是一个通用的存储引擎,平衡了高可靠性和高性能。InMySQL5.7,InnoDBisthedefaultMySQLsto......
  • 基于GPT搭建私有知识库聊天机器人(四)问答实现
    前文链接:基于GPT搭建私有知识库聊天机器人(一)实现原理基于GPT搭建私有知识库聊天机器人(二)环境安装基于GPT搭建私有知识库聊天机器人(三)向量数据训练在前面的文章中,我们介绍了如何使用GPT模型搭建私有知识库聊天机器人的基本原理、环境安装、数据向量化。本文将进一步介绍如何使用lang......
  • 基于GPT搭建私有知识库聊天机器人(五)函数调用
    文章链接:基于GPT搭建私有知识库聊天机器人(一)实现原理基于GPT搭建私有知识库聊天机器人(二)环境安装基于GPT搭建私有知识库聊天机器人(三)向量数据训练基于GPT搭建私有知识库聊天机器人(四)问答实现OpenAI在6月13日发布了几个重磅更新,其中包括:开放了16k上下文的GPT-3.5-Turbo模型gpt-3.5-t......
  • 基于GPT搭建私有知识库聊天机器人(六)仿chatGPT打字机效果
    文章链接:基于GPT搭建私有知识库聊天机器人(一)实现原理基于GPT搭建私有知识库聊天机器人(二)环境安装基于GPT搭建私有知识库聊天机器人(三)向量数据训练基于GPT搭建私有知识库聊天机器人(四)问答实现基于GPT搭建私有知识库聊天机器人(五)函数调用在前几篇文章中,我们已经了解了如何使用GPT模......