百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 热门文章 > 正文

CENTOS Mysql5.7数据库自动安装脚本

bigegpt 2025-05-24 12:23 10 浏览

对于多台服务器安装mysql数据库,采用手工安装,是非常繁琐的,如果采取脚本批量安装,就非常的方便了,具体操作方法如下:

创建目录

mkdir -p /apps

mkdir -p /data

上传安装包

上传执行脚本

授权脚本执行权限

Chmod +x ./installMysql5.7.32

执行脚本

./installMysql5.7.32.sh

登录mysql数据库

脚本信息

脚本内容

#!/bin/bash

port=`ss -tanl | grep 3306 |wc -l`

if [ $port -eq 1 ]; then

echo "3306 is exists please kill 3306!!"

exit

fi

if [ ! -d "/apps/" ];then

echo "The Directory:/apps/ no exist"

exit

fi

if [ ! -f "/apps/mysql-5.7.32-el7-x86_64.tar.gz" ];then

echo "The Directory:/apps/ No mysql-5.7.32-el7-x86_64.tar.gz please mysql-5.7.32-el7-x86_64.tar.gz to /apps/"

exit

fi

if [ ! -f "/apps/installMysql5.7.32.sh" ];then

echo "The Directory:/apps/ No installMysql5.7.32.sh please copy installMysql5.7.32.sh to /apps/"

exit

fi

if [ ! -d "/data/" ];then

echo "The Directory:/data/ no exist please mkdir /data"

exit

fi

echo "<---------------------begin--------------------->"

cd /apps/

tar -zxvf mysql-5.7.32-el7-x86_64.tar.gz

mv mysql-5.7.32-el7-x86_64 mysql

echo 'export PATH=/apps/mysql/bin:$PATH' >> /etc/profile

source /etc/profile

source /etc/profile

rm -rf /data/*

mkdir /data/mysql

mkdir /data/mysql/data

mkdir /data/mysql/tmp

mkdir /data/mysql/var

mkdir /data/mysql/log

touch /data/mysql/log/slow.log

touch /data/mysql/log/mysqld.log

i=`cat /etc/passwd | cut -f1 -d':' | grep -w mysql -c`

if [ $i -le 0 ]; then

groupadd mysql

useradd -r -s /sbin/nologin -g mysql mysql

fi

chown -R mysql:mysql /data/mysql

chown -R mysql:mysql /apps/mysql

chown mysql:mysql /apps/mysql

if [ -f "/etc/my.cnf" ];then

rm -f /etc/my.cnf

fi

server_id=`date +%Y%m%d%H%M%S`

cat > /etc/my.cnf << 'EOF'

[mysqld]

############# GENERAL #############

autocommit = ON

character_set_server = utf8

collation_server = utf8_general_ci

explicit_defaults_for_timestamp = ON

lower_case_table_names = 1

port = 3306

#read_only = ON

transaction_isolation = READ-COMMITTED

user =mysql

innodb_file_per_table = 1

#skip-grant-tables

############### PATH ##############

basedir = /apps/mysql

datadir = /data/mysql/data

tmpdir = /data/mysql/tmp

socket = /data/mysql/var/mysql.sock

pid_file = /data/mysql/var/mysql.pid

innodb_data_home_dir = /data/mysql/data

log_error = /data/mysql/log/error.log

general_log_file = /data/mysql/log/general.log

slow_query_log_file = /data/mysql/log/slow.log

#log_bin = /data/mysql/log/mysql-bin

#log_bin_index = /data/mysql/log/mysql-bin.index

relay_log = /data/mysql/log/relay-log

relay_log_index = /data/mysql/log/relay-log.index

############# INNODB #############

innodb_flush_method = O_DIRECT

innodb_buffer_pool_size = 30G

innodb_log_file_size = 1G

innodb_log_files_in_group = 4

innodb_flush_log_at_trx_commit = 2

innodb_support_xa = ON

innodb_strict_mode = ON

innodb_data_file_path = ibdata1:256M;ibdata2:256M:autoextend

innodb_temp_data_file_path = ibtmp1:512M:autoextend

innodb_lock_wait_timeout = 5

innodb_undo_tablespaces = 3

innodb_undo_log_truncate = 1

innodb_max_undo_log_size = 5120M

innodb_lock_wait_timeout = 50

innodb_read_io_threads = 16

innodb_write_io_threads = 16

innodb_io_capacity = 1200

innodb_io_capacity_max = 2400

innodb_lru_scan_depth = 512

####### CACHES AND LIMITS #########

interactive_timeout = 86400

wait_timeout = 86400

lock_wait_timeout = 50

max_allowed_packet = 256M

max_connect_errors = 2000

max_connections = 2000

max_user_connections = 2000

join_buffer_size = 4M

sort_buffer_size = 4M

#table_definition_cache = 5000

#table_open_cache = 8000

#table_open_cache_instances = 4

#thread_cache_size = 9

#thread_stack = 256K

tmp_table_size = 32M

max_heap_table_size = 32M

binlog_cache_size = 1M

############# SAFETY ##############

#local_infile = OFF

#{$validate_password_enable}plugin-load = validate_password.so

#skip_name_resolve = ON

#sql_mode = STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,ONLY_FULL_GROUP_BY

############# LOGGING #############

#general_log = 0

log_queries_not_using_indexes = OFF

log_throttle_queries_not_using_indexes = 0

log_slow_admin_statements = ON

log_error_verbosity = 3

long_query_time = 3

slow_query_log = ON

log_timestamps = SYSTE

############# REPLICATION #############

#binlog_checksum = CRC32

#master_verify_checksum = ON

#slave_sql_verify_checksum = ON

binlog_format = ROW

binlog_rows_query_log_events = ON

expire_logs_days = 3

max_binlog_size = 256M

enforce_gtid_consistency = ON

gtid_mode = ON

log_slave_updates = ON

master_info_repository = TABLE

relay_log_info_repository = TABLE

server_id=000001

#skip_slave_start = ON

#slave_net_timeout = 4

sync_binlog = 100

sync_master_info = 10000

sync_relay_log = 10000

sync_relay_log_info = 10000

slave_parallel_workers = 4

slave_parallel_type = LOGICAL_CLOCK

relay_log_recovery = ON

slave_skip_errors = all

[mysqld_safe]

log-error=/data/mysql/log/mysqld.log

pid-file=/data/mysql/var/mysqld.pid

[client]

#default-character-set=utf8

[mysql]

default-character-set=utf8

EOF

sed -i "s/server_id=000001/server_id=$server_id/g" /etc/my.cnf

cd /apps/mysql/bin

mysqld --initialize --user=mysql --basedir=/apps/mysql --datadir=/data/mysql/data

mysql_ssl_rsa_setup --datadir=/data/mysql/data

mysqld_safe > /dev/null 2>&1 &

sleep 15

ln -s /apps/mysql/bin/msql /usr/bin

passwd1=`cat /data/mysql/log/error.log | grep password | awk -F "root@localhost:" '{print $2}'| awk -F " " '{print $1}'`

echo "initial password:"$passwd1

passwd2="'test123'"

passwd3="test123"

echo "<---------------modify passwd------------------>"

mysql -uroot -h127.0.0.1 -p${passwd1} --connect-expired-password -e "alter user 'root'@'localhost' identified by ${passwd2}"

mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "grant all privileges on *.* to root@'%' identified by ${passwd2};"

echo "The root password has been changed:"$passwd3

mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "UPDATE mysql.user SET Grant_priv='Y', Super_priv='Y' WHERE User='root';"

mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "grant replication slave,replication client on *.* to replic_user identified by 'test123';"

echo "The cluster user name have been created:replic_user"

echo "The cluster password have been created:test123"

mysql -uroot -h127.0.0.1 -p${passwd3} --connect-expired-password -e "FLUSH PRIVILEGES;"

echo "<---------------end------------------>"

备注

脚本内容可以根据自己的数据库版本进行调整,此脚本已在安装生产环境使用过,数据库的参数,可以根据自己的服务器配置进行调整。

相关推荐

Go语言泛型-泛型约束与实践(go1.7泛型)

来源:械说在Go语言中,Go泛型-泛型约束与实践部分主要探讨如何定义和使用泛型约束(Constraints),以及如何在实际开发中利用泛型进行更灵活的编程。以下是详细内容:一、什么是泛型约束?**泛型...

golang总结(golang实战教程)

基础部分Go语言有哪些优势?1简单易学:语法简洁,减少了代码的冗余。高效并发:内置强大的goroutine和channel,使并发编程更加高效且易于管理。内存管理:拥有自动垃圾回收机制,减少内...

Go 官宣:新版 Protobuf API(go pro版本)

原文作者:JoeTsai,DamienNeil和HerbieOng原文链接:https://blog.golang.org/a-new-go-api-for-protocol-buffer...

Golang开发的一些注意事项(一)(golang入门项目)

1.channel关闭后读的问题当channel关闭之后再去读取它,虽然不会引发panic,但会直接得到零值,而且ok的值为false。packagemainimport"...

golang 托盘菜单应用及打开系统默认浏览器

之前看到一个应用,用go语言编写,说是某某程序的windows图形化客户端,体验一下发现只是一个托盘,然后托盘菜单的控制面板功能直接打开本地浏览器访问程序启动的webserver网页完成gui相关功...

golang标准库每日一库之 io/ioutil

一、核心函数概览函数作用描述替代方案(Go1.16+)ioutil.ReadFile(filename)一次性读取整个文件内容(返回[]byte)os.ReadFileioutil.WriteFi...

文件类型更改器——GoLang 中的 CLI 工具

我是如何为一项琐碎的工作任务创建一个简单的工具的,你也可以上周我开始玩GoLang,它是一种由Google制作的类C编译语言,非常轻量和快速,事实上它经常在Techempower的基准测...

Go (Golang) 中的 Channels 简介(golang channel长度和容量)

这篇文章重点介绍Channels(通道)在Go中的工作方式,以及如何在代码中使用它们。在Go中,Channels是一种编程结构,它允许我们在代码的不同部分之间移动数据,通常来自不同的goro...

Golang引入泛型:Go将Interface「」替换为“Any”

现在Go将拥有泛型:Go将Interface{}替换为“Any”,这是一个类型别名:typeany=interface{}这会引入了泛型作好准备,实际上,带有泛型的Go1.18Beta...

一文带你看懂Golang最新特性(golang2.0特性)

作者:腾讯PCG代码委员会经过十余年的迭代,Go语言逐渐成为云计算时代主流的编程语言。下到云计算基础设施,上到微服务,越来越多的流行产品使用Go语言编写。可见其影响力已经非常强大。一、Go语言发展历史...

Go 每日一库之 java 转 go 遇到 Apollo?让 agollo 来平滑迁移

以下文章来源于GoOfficialBlog,作者GoOfficialBlogIntroductionagollo是Apollo的Golang客户端Apollo(阿波罗)是携程框架部门研...

Golang使用grpc详解(golang gcc)

gRPC是Google开源的一种高性能、跨语言的远程过程调用(RPC)框架,它使用ProtocolBuffers作为序列化工具,支持多种编程语言,如C++,Java,Python,Go等。gR...

Etcd服务注册与发现封装实现--golang

服务注册register.gopackageregisterimport("fmt""time"etcd3"github.com/cor...

Golang:将日志以Json格式输出到Kafka

在上一篇文章中我实现了一个支持Debug、Info、Error等多个级别的日志库,并将日志写到了磁盘文件中,代码比较简单,适合练手。有兴趣的可以通过这个链接前往:https://github.com/...

如何从 PHP 过渡到 Golang?(php转golang)

我是PHP开发者,转Go两个月了吧,记录一下使用Golang怎么一步步开发新项目。本着有坑填坑,有错改错的宗旨,从零开始,开始学习。因为我司没有专门的Golang大牛,所以我也只能一步步自己去...