mysql自动安装脚本(快速部署mysql)

mysql_install - 适用于生产环境单实例快速部署

MySQL8.0 自动安装脚本

mysql8_install.sh(执行前修改一下脚本里的配置参数,改成你自己的)(博客末尾)

my_test.cnf(博客末尾)(这个是模板文件,基本上不用改,mysql8_install.sh脚本执行的时候会自动替换里面的port,server_id,innodb_buffer_pool_size等)

mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

shell> wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz

三个文件放在同一个目录下,例如/root/soft/


Centos8 注意事项

shell> yum install jemalloc -y

内存管理器jemalloc库文件名字已经变更,需要建立一个软连接

shell> ln -s /usr/lib64/libjemalloc.so.2 /usr/lib64/libjemalloc.so


1)安装并启动mysql进程(主和从库都执行)

shell> /bin/bash mysql8_install.sh

注:my.cnf配置文件默认在/etc/目录下,文件名是以你的数据库名命名,例my_test.cnf,mysql.sock在/tmp目录下。

数据存放在/data/mysql/目录下。

2)配置主从复制(从库执行)

shell> /bin/bash mysql8_install.sh repl

3)配置组复制(先在Primary节点上执行,再到Secondary节点上执行)

注:先把3个节点MySQL实例启动后再开始搭建mgr,同时修改脚本里的ip地址和端口和hosts对应的主机名和地址

shell> /bin/bash mysql8_install.sh mgr

image

注:配置成功后,会在data数据目录下生成mysqld-auto.cnf配置文件。
脚本:
mysql8_install.sh

#!/bin/bash
# 适用于生产环境单实例快速部署,操作系统CentOS 7。echo "正在安装MySQL软件......."useradd mysql
useradd nagios
useradd zabbixsleep 2######配置参数######
mysql8_version=mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
mysql8_version_dir=mysql-8.0.32-linux-glibc2.12-x86_64################################主从复制配置################################
######同步复制用户######
repl_user=repl
repl_passwd=sysrepl
#############################root密码######
root_passwd=123456
#############################DBA管理用户######
dba_user=admin
dba_passwd=hechunyang
#############################8.0克隆用户######
clone_user=clone_user
clone_passwd=123456
#######################################################mgr组复制配置######################################修改hosts文件######
cat << EOF >> /etc/hosts192.168.71.11   mgr1
192.168.71.12   mgr2
192.168.71.13   mgr3EOF
#######################mysql_port=3306                 # 数据库实例对外请求端口
primary_ip=192.168.71.11        # 每个mgr节点IP
secondary1_ip=192.168.71.12     # 每个mgr节点IP
secondary2_ip=192.168.71.13     # 每个mgr节点IPprimary_port=33061        # mgr primary节点通讯端口
secondary1_port=33062     # mgr secondary1节点通讯端口
secondary2_port=33063     # mgr secondary2节点通讯端口local_ip=192.168.71.11    # 每个mgr节点IP不相同
local_port=33061          # 每个mgr节点通讯端口port不相同################################以下代码不用修改################################
if [ "$1" = "repl" ]
then
while true
doread -t 30 -p "输入你的主库IP:  " master_ipread -t 30 -p "输入你的主库端口号:  " master_portif [[ -z $master_ip || -z $master_port ]]thencontinueelseecho ""echo "主库IP是: $master_ip"echo "主库端口号是: $master_port"break fi
done/usr/local/mysql/bin/mysql -h127.0.0.1 -u"$dba_user" -p"$dba_passwd" -P"$master_port" --connect-expired-password -e "CHANGE MASTER TO MASTER_HOST='$master_ip',MASTER_USER='$repl_user',MASTER_PASSWORD='$repl_passwd',MASTER_PORT=$master_port,MASTER_AUTO_POSITION = 1,MASTER_CONNECT_RETRY=10; START SLAVE;"echo "MySQL主从复制同步已经初始化完毕。"exit 0
fi################################################
if [ "$1" = "mgr" ]
thenwhile true
doread -t 30 -p "是Primary吗?是请输入yes,否输入no:  " is_primaryif [[ -z $is_primary ]]thencontinueelseif [ $is_primary == "yes" ] || [ $is_primary == "no" ]thenbreak elseecho "你输入一个错误的字符$is_primary,请重新输入..."continuefifi
doneif [ $is_primary == "yes" ]
then/usr/local/mysql/bin/mysql -h127.0.0.1 -u"$dba_user" -p"$dba_passwd" -P"$mysql_port" --connect-expired-password -e "INSTALL PLUGIN group_replication SONAME  'group_replication.so'; set persist group_replication_group_name = '3b12b5bd-f0c6-11e9-9778-000c2900afc6';set persist group_replication_local_address =  '${local_ip}:${local_port}'; set persist group_replication_group_seeds = '${primary_ip}:${primary_port},${secondary1_ip}:${secondary1_port},${secondary2_ip}:${secondary2_port}';SET GLOBAL group_replication_bootstrap_group=ON; CHANGE MASTER TO MASTER_USER='$repl_user',MASTER_PASSWORD='$repl_passwd' FOR CHANNEL 'group_replication_recovery';START GROUP_REPLICATION;select sleep(5);select * from performance_schema.replication_group_members;SET GLOBAL group_replication_bootstrap_group=OFF;"else/usr/local/mysql/bin/mysql -h127.0.0.1 -u"$dba_user" -p"$dba_passwd" -P"$mysql_port" --connect-expired-password -e "INSTALL PLUGIN group_replication SONAME  'group_replication.so'; set persist group_replication_group_name = '3b12b5bd-f0c6-11e9-9778-000c2900afc6';set persist group_replication_local_address =  '${local_ip}:${local_port}'; set persist group_replication_group_seeds = '${primary_ip}:${primary_port},${secondary1_ip}:${secondary1_port},${secondary2_ip}:${secondary2_port}'; SET GLOBAL group_replication_bootstrap_group=OFF; CHANGE MASTER TO MASTER_USER='$repl_user',MASTER_PASSWORD='$repl_passwd' FOR CHANNEL 'group_replication_recovery';START GROUP_REPLICATION;select sleep(5);select * from performance_schema.replication_group_members;"fiecho "MySQL Mgr组复制已经初始化完毕。"
exit 0fi################################################ps aux | grep 'mysql' | grep -v 'grep' | grep -v 'bash'
if [ $? -eq 0 ]
thenecho "MySQL进程已经启动,无需二次安装。"exit 0
fiif [ ! -d /usr/local/${mysql8_version_dir} ]
thenyum install xz numactl* jemalloc* libaio* net-tools nload iftop sysstat wget vim ntpdate lrzsz -ytar -Jxvf ${mysql8_version} -C /usr/local/ln -s /usr/local/${mysql8_version_dir} /usr/local/mysqlchown -R mysql.mysql /usr/local/mysql/chown -R mysql.mysql /usr/local/mysql
elseln -s /usr/local/${mysql8_version_dir} /usr/local/mysqlchown -R mysql.mysql /usr/local/mysql/chown -R mysql.mysql /usr/local/mysql
fi while true
doread -t 30 -p "输入你的数据库名:  " dbnameread -t 30 -p "输入你的数据库端口号:  " dbportread -t 30 -p "输入MySQL serverId:  " serverIdread -t 30 -p "输入innodb_buffer_pool_size大小,单位G:  " innodb_bp_sizeif [[ -z $dbname || -z $dbport || -z $serverId || -z $innodb_bp_size ]]thencontinueelseecho "数据库名字是: $dbname"echo "数据库端口是: $dbport"echo "MySQL serverId: $serverId"echo "BP大小是: $innodb_bp_size GB"break fi
donesed "s/test/$dbname/g;s/3306/$dbport/;s/413306/$serverId/;/innodb_buffer_pool_size/s/1/$innodb_bp_size/" my_test.cnf > /etc/my_$dbname.cnfDATA_DIR=/data/mysql/$dbname
[ ! -d $DATA_DIR ] && mkdir -p $DATA_DIR/{data,binlog,relaylog,tmp,slowlog,log}; touch $DATA_DIR/log/error.log; chown -R mysql.mysql /data/mysql/if [ `ls -A $DATA_DIR/data/ | wc -w` -eq 0 ]
thencd /usr/local/mysqlecho ""echo "初始化MySQL数据目录......"echo ""bin/mysqld --defaults-file=/etc/my_$dbname.cnf --initialize --lower-case-table-names=1 --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql/$dbname/datasleep 2bin/mysqld_safe --defaults-file=/etc/my_$dbname.cnf --user=mysql &
fiwhile true
donetstat -ntlp | grep $dbportif [ $? -eq 1 ]thenecho "MySQL启动中,稍等......"sleep 5continueelseif [ ! -e "/tmp/mysql_${dbname}.sock" ];thenecho "MySQL启动中,稍等......"sleep 5continuefibreakfi
doneps aux | grep 'mysql' | grep -v 'grep' | grep -v 'bash'
if [ $? -eq 0 ]
thenecho "MySQL安装完毕。"
elseecho "MySQL安装失败。"exit 1
fi###更改root账号随机密码
random_passwd=`grep 'temporary password' $DATA_DIR/log/error.log | awk -F 'root@localhost: ' '{print $2}'`/usr/local/mysql/bin/mysql -S /tmp/mysql_$dbname.sock -p"$random_passwd" --connect-expired-password -e "alter user root@'localhost' identified by '$root_passwd';" 
if [ $? -eq 0 ]
thenecho "root账号随机密码更改完毕。"
elseecho "密码更改失败,再次重试更改。"sleep 5/usr/local/mysql/bin/mysql -S /tmp/mysql_$dbname.sock -p"$random_passwd" --connect-expired-password -e "alter user root@'localhost'identified by '$root_passwd';"if [ $? -eq 0 ]thenecho "root账号随机密码更改完毕。"elseecho "密码更改失败"exit 1fi
fi###创建同步账号和管理员账号
/usr/local/mysql/bin/mysql -S /tmp/mysql_$dbname.sock -p"$root_passwd" -e "set sql_log_bin=0;create user '$repl_user'@'%' IDENTIFIED BY '$repl_passwd'; GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO '$repl_user'@'%'; create user '$dba_user'@'%' IDENTIFIED BY '$dba_passwd'; GRANT ALL on *.* to '$dba_user'@'%' WITH GRANT OPTION;"sed -i -r "s/(PATH=)/\1\/usr\/local\/mysql\/bin:/" /root/.bash_profile
source /root/.bash_profileecho "MySQL账号初始化完毕。"###安装clone插件
/usr/local/mysql/bin/mysql -S /tmp/mysql_$dbname.sock -p"$root_passwd" -e "set sql_log_bin=0;INSTALL PLUGIN CLONE SONAME 'mysql_clone.so'; CREATE USER '$clone_user'@'%' IDENTIFIED BY '$clone_passwd';GRANT BACKUP_ADMIN,CLONE_ADMIN ON *.* TO '$clone_user'@'%';"echo ""
echo "clone克隆插件安装完毕。"### 最后一步清空binlog文件,恢复起始文件mysql-bin.000001和Position位置号153 
/usr/local/mysql/bin/mysql -S /tmp/mysql_$dbname.sock  -p"$root_passwd" -e "reset master;"

my_test.cnf

[client]
port		=  3306
socket   =  /tmp/mysql_test.sock
#default-character-set = utf8 
prompt=(\\u@\\h) [\\d]>\\_# The MySQL server
#########Basic##################
[mysqld]
##bind_address = 127.0.0.1
server-id	=  413306
port	        =  3306
user            =  mysql
basedir		=  /usr/local/mysql
datadir		=  /data/mysql/test/data
tmpdir		=  /data/mysql/test/tmp
secure_file_priv =  /data/mysql/test/tmp
socket		=  /tmp/mysql_test.sock
skip-external-locking
skip-name-resolve
skip-ssl
##init_connect = 'SET global sql_mode = ""'
sql_mode = ''
default_authentication_plugin = mysql_native_password
activate_all_roles_on_login = 1### Percona ###
#extra_port = 13306
#extra_max_connections = 100###skip-networking
default-storage-engine = INNODB
character-set-server = utf8
wait_timeout  =  3600
connect_timeout  =  20
interactive_timeout  =  3600
back_log  =  500
#event_scheduler  =  ON
open_files_limit = 65535
#thread_handling  =  pool-of-threadslower_case_table_names  =  1###### binlog ######
log-bin  =  /data/mysql/test/binlog/mysql-bin
binlog_format = ROW
binlog_row_image = FULL
binlog_row_metadata = FULL
binlog_checksum = NONE
binlog_transaction_dependency_tracking = WRITESET
sync_binlog      =   1
gtid_mode = on
enforce_gtid_consistency = on
log_slave_updates   =  1
master_info_repository = TABLE
relay_log_info_repository = TABLE
max_binlog_size  =  128M
binlog_cache_size  =  1M
expire-logs-days  =  7#########replication#############
relay-log               =  /data/mysql/test/relaylog/relay-log
slave-net-timeout                   =  10
#rpl_semi_sync_master_enabled        =  1
#rpl_semi_sync_master_wait_no_slave   =  1
#rpl_semi_sync_master_timeout        =  1000
#rpl_semi_sync_slave_enabled         =  1
#skip-slave-start
log_slave_updates                  =  1
relay_log_recovery                 =  1
#slave_skip_errors = 1062
read_only                          =  0###### Mgr config ######
loose-transaction_write_set_extraction = XXHASH64
loose-group_replication_start_on_boot = ON ###是否随mysql启动Group Replication
loose-group_replication_bootstrap_group = OFF  ###是否是Group Replication的引导节点,初次搭建集群的时候需要有一个节点设置为ON来启动Group Replication###参数设置为ON,是为了标示以后加入集群的服务器都已这台服务器为基准。以后加入的就不需要进行设置loose-group_replication_group_name = aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa                             
#loose-group_replication_local_address = 192.168.148.41:33061					       
#loose-group_replication_group_seeds = 192.168.148.41:33061,192.168.148.42:33072,192.168.148.39:33083					       
loose-group_replication_single_primary_mode = ON	
loose-group_replication_exit_state_action = OFFLINE_MODE
loose-group_replication_transaction_size_limit = 150000000 ###默认143M事务大小,最大值2147483647(大约2G),当此系统变量设置为0时,该组接受的事务大小没有限制。
loose-group_replication_enforce_update_everywhere_checks = OFF ###在单主模式下设置为OFF,多主模式下设置为ON。###offline_mode = ON###MHA ############
relay_log_purge            =  1
######################parallel replication####
slave_parallel_type = LOGICAL_CLOCK
slave_parallel_workers = 24
slave_preserve_commit_order = ON
###########################################slow log#############
slow_query_log  =  1
slow_query_log_file  =  /data/mysql/test/log/mysql-slow.log
#log-slow-verbosity = query_plan,explain
long_query_time  =  5#########error log#############
log-error   =  /data/mysql/test/log/error.log
log_timestamps = system#######per_thread_buffers############
max_connections=4100
max_user_connections=4000
max_connect_errors=100000000
max_allowed_packet = 256M
table_open_cache = 6144
table_definition_cache = 4096
sort_buffer_size = 128K
read_buffer_size = 128K
read_rnd_buffer_size = 128k
join_buffer_size = 128K
tmp_table_size = 64M
max_heap_table_size = 64M
bulk_insert_buffer_size = 32M
thread_cache_size = 64
#thread_concurrency = 32
thread_stack = 256K##MyISAM## 
key_buffer_size = 64M
myisam_sort_buffer_size = 8M
concurrent_insert=2
low_priority_updates=1
myisam_recover_options=BACKUP,FORCE######### InnoDB #############
sql_generate_invisible_primary_key = 1
innodb_adaptive_hash_index = 0
innodb_autoinc_lock_mode = 2
innodb_numa_interleave = 1
innodb_sort_buffer_size = 32M
innodb_rollback_on_timeout = 1
innodb_data_file_path = ibdata1:10M:autoextend
innodb_buffer_pool_dump_at_shutdown = 1
innodb_buffer_pool_load_at_startup = 1
innodb_buffer_pool_size = 1G
innodb_buffer_pool_instances	= 16
innodb_lru_scan_depth = 2048
innodb_log_file_size = 512M
innodb_log_buffer_size = 64M
innodb_log_files_in_group = 3
innodb_flush_log_at_trx_commit = 2
innodb_lock_wait_timeout = 10
innodb_sync_spin_loops = 40
innodb_max_dirty_pages_pct = 95
innodb_ddl_threads = 16
innodb_ddl_buffer_size = 1G
innodb_online_alter_log_max_size = 1G
innodb_parallel_read_threads = 32
innodb_thread_concurrency = 0
innodb_thread_sleep_delay = 500
innodb_concurrency_tickets = 1000
log_bin_trust_function_creators = 1
innodb_flush_method = O_DIRECT
#innodb_flush_method = O_DIRECT_NO_FSYNC
innodb_file_per_table = 1
innodb_read_io_threads = 16
innodb_write_io_threads = 16
innodb_use_native_aio = 1
innodb_io_capacity = 800
innodb_purge_threads = 16
innodb_purge_batch_size = 32
innodb_old_blocks_time = 1000
innodb_change_buffering = all
transaction_isolation = READ-COMMITTED
innodb_stats_on_metadata = 0
performance-schema = 1
performance-schema-instrument = wait/lock/metadata/sql/mdl=ON####### NEW ########
##innodb_kill_idle_transaction = 5###### SSD #########
#innodb_flush_neighbors = 0
#innodb_log_block_size = 4096
####################[mysqldump]
quick
max_allowed_packet = 128M[mysql]
no-auto-rehash
#prompt=(\\u@\\h) [\\d]>\\_
prompt=(\\u@\\h) [\\d]>\\_[myisamchk]
key_buffer_size = 64M
sort_buffer_size = 256k
read_buffer = 2M
write_buffer = 2M[mysqlhotcopy]
interactive-timeout[mysqld_safe]
open-files-limit = 65535
#flush_caches = 1
#numa_interleave = 1
malloc-lib = /usr/lib64/libjemalloc.so

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/285425.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

工作:三菱PLC程序开发流程总结

工作&#xff1a;三菱PLC程序开发流程总结 一、程序流程图 程序流程图是逻辑思维与动作流程的检查图&#xff0c;是保证逻辑思维合理的前提&#xff0c;写代码丢失方向可从程序流程图重新整理&#xff0c;程序流程图非常重要。 二、组态配置 组态配置是将所用到的基板和模块…

基于Java SpringBoot和Vue的医院信息管理挂号系统

摘要 医院信息管理系统&#xff08;Hospital Information Management System&#xff0c;简称HIMS&#xff09;是一种应用于医疗机构的信息化管理系统&#xff0c;旨在提高医疗服务质量、降低运营成本、提高工作效率和满足患者需求。HIMS通过对医院内各种信息的集成、管理和共享…

C++11 【初识】

C11简介 1.在2003年C标准委员会曾经提交了一份技术勘误表(简称TC1)&#xff0c;使得C03这个名字已经取代了C98称为C11之前的最新C标准名称。 2.不过由于C03(TC1)主要是对C98标准中的漏洞进行修复&#xff0c;语言的核心部分则没有改动&#xff0c;因此人们习惯性的把两个标准合…

用户行为分析遇到的问题-ubantu16,hadoop3.1.3

用户行为分析传送门 我的版本 ubantu16 hadoop 3.1.3 habse 2.2.2 hive3.1.3 zookeeper3.8.3 sqoop 1.46/1.47 我sqoop把MySQL数据往hbase导数据时候有问题 重磅&#xff1a;大数据课程实验案例&#xff1a;网站用户行为分析&#xff08;免费共享&#xff09; 用户行为分析-小…

QT添加新的组件

qt安装完成后&#xff0c;想要添加新的组件时&#xff1a; 1、找到QT安装的tool 2、找到其所在的文件夹位置 将该tool软件拖到终端页面 将源改为国内清华的路径&#xff0c;下载速度较快 installer.exe --mirror https://mirrors.tuna.tsinghua.edu.cn/qt

1951 年以来的美国ACIS 气候地图数据集(5 公里空间分辨率)

应用气候信息系统 (ACIS) NRCC NN ACIS是Applied Climate Information System的缩写&#xff0c;是由美国国家气象局&#xff08;NOAA&#xff09;开发的一种气候信息系统。ACIS气候地图是通过收集和整理全球的气象数据&#xff0c;利用计算机技术和数据分析方法生成的气候图表…

FPGA时序分析与时序约束(二)——时钟约束

目录 一、时序约束的步骤 二、时序网表和路径 2.1 时序网表 2.2 时序路径 三、时序约束的方式 三、时钟约束 3.1 主时钟约束 3.2 虚拟时钟约束 3.3 衍生时钟约束 3.4 时钟组约束 3.5 时钟特性约束 3.6 时钟延时约束 一、时序约束的步骤 上一章了解了时序分析和约束…

首次使用 git 配置 github,gitee 密钥

gitee 和 github 密钥配置 1. 检查配置信息 使用命令 git config --global --list 检查邮箱是否一致 不一致可以使用如下命令进行设置 git config --global user.name "name" git config --global user.email "emailqq.com" 2. 生成 SSH 密钥 # 为 G…

信号与系统分析导论——“信号与系统”

小雅兰马上就要期末考试啦&#xff0c;最近也要开始准备期末复习了&#xff0c;下面&#xff0c;就让我们进入信号与系统分析导论的世界吧&#xff01;&#xff01;&#xff01;&#xff01; 信号&#xff08;signal&#xff09; 系统&#xff08;system&#xff09; 信号的描…

ElasticSearch单机或集群未授权访问漏洞

漏洞处理方法&#xff1a; 1、可以使用系统防火墙 来做限制只允许ES集群和Server节点的IP来访问漏洞节点的9200端口&#xff0c;其他的全部拒绝。 2、在ES节点上设置用户密码 漏洞现象&#xff1a;直接访问9200端口不需要密码验证 修复过程 2.1 生成认证文件 必须要生成…

oracle sqldeveloper连接mysql中遇到的问题

问题1&#xff1a; Unknown initial character set index ‘255’ received from server.Initial client character 解决&#xff1a; 因为mysql-connector-java的版本与服务器数据库版本(8.0.11)不符合&#xff0c;所以导致连接出错&#xff0c; java架包的版本要与数据库版本…

如何实现TensorFlow自定义算子?

在上一篇文章中 Embedding压缩之基于二进制码的Hash Embedding&#xff0c;提供了二进制码的tensorflow算子源码&#xff0c;那就顺便来讲下tensorflow自定义算子的完整实现过程。 前言 制作过程基于tensorflow官方的custom-op仓库以及官网教程&#xff0c;并且在Ubuntu和Mac…