MySQL5.7.25初始化安装脚本(shell)#!/bin/bash
echo "初始化安装脚本"
<<COMMENT
注:
AUTHOR: ws, 779269823@qq
CREATED: 2019.11.06
MYSQL版本:5.7.25
CentOS Linux release 6.6
COMMENT
>>>>##定义变量>>>>##
SOFT_DIR=/opt/mysql
RUN_DIR=/var/run/mysql
GROUP_NAME=mysql
USER_NAME=mysql
PORT=3306
MYSQLDB_HOME=/opt/apps/mysql
MYSQLDB_DATA_HOME=$MYSQLDB_HOME/data
BINLOG_DIR=$MYSQLDB_DATA_HOME
TMP_DIR=$MYSQLDB_HOME/tmp
MYSQL_VERSION="mysql-5.7.25-linux-glibc2.12-x86_64"
CPU_NUMBERS=$(cat /proc/cpuinfo |grep "processor"|wc -l)
MYSQL_ID=`ifconfig eth0 | grep "inet addr" | awk -F. '{print $4}' | awk '{print $1}'`
# 获取服务器内存
COMPUTER_MEM=`free -m |grep "Mem"|awk '{print $2}'`
>>>>>>>>>>#
#check if user is root
if [ $(id -u) != "0" ];then
echo "Error: You must be root to run this script!"
exit 1
fi
echo "========================================================================="
echo " install MySQL 5.7.25 on Redhat/CentOS Linux "
mysql下载后的初次使用echo "========================================================================="
#addGroup
if [ -z $(cat /etc/group|awk -F: '{print $1}'| grep -w "$GROUP_NAME") ]
then
groupadd $GROUP_NAME
if(( $? == 0 ))
then
echo "group $GROUP_NAME add sucessfully!"
fi
else
echo "$GROUP_NAME is exsits"
fi
#addUser
if [ -z $(cat /etc/passwd|awk -F: '{print $1}'| grep -w "$USE_NAME") ]
then
adduser -g $GROUP_NAME $USER_NAME
if (( $? == 0 ))
then
echo "user $USER_NAME add sucessfully!"
fi
else
echo "$USER_NAME is exsits"
fi
#downMySQL
# 判断安装介质⽬录是否创建
if [ -d $SOFT_DIR ];then
echo "⽬录已创建"
else
mkdir -p $SOFT_DIR
fi
#删除centos系统⾃带的mariadb数据库防⽌发⽣冲突
rpm -qa|grep mariadb
rpm -e mariadb-libs --nodeps
# 安装libaio
yum -y install libaio
if [ -s /etc/myf ]; then
mv /etc/myf /etc/myf.`date +%Y%m%d%H%M%S`.bak
fi
echo "====================安装mysql5.7.25=========================="
# 创建mysql配置⽂件:
cat >>/etc/myf<<EOF
[mysql]
# CLIENT #
port = $PORT
socket = $RUN_DIR/mysql_$PORT.sock
no-auto-rehash
prompt = "\\u@\\h [\\d]>"
[mysqld]
# GENERAL #
server-id = $MYSQL_ID
user = mysql
default-storage-engine = InnoDB
socket = $RUN_DIR/mysql_$PORT.sock
pid-file = $RUN_DIR/mysqld_$PORT.pid
port = $PORT
local-infile = on
bind-address = 0.0.0.0
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
performance_schema = OFF
# MyISAM #
key-buffer-size = 128M
# SAFETY #
max-allowed-packet = 16M
max-connect-errors = 1000000
skip-name-resolve
#sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION,STRICT_ALL_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO sql-mode = NO_ENGINE_SUBSTITUTION
sysdate-is-now = 1
#innodb = FORCE
symbolic-links = 0
explicit_defaults_for_timestamp
# DATA STORAGE #
datadir = $MYSQLDB_DATA_HOME
tmpdir = $TMP_DIR
lc-messages-dir = $MYSQLDB_HOME/share
# BINARY LOGGING #
expire-logs-days = 7
sync-binlog = 1
log_bin = $BINLOG_DIR/mysql-bin
max_binlog_size = 1G
binlog_format = ROW
binlog_row_image = full
# REPLICATION #
read-only = 0
skip-slave-start = 1
log-slave-updates = 1
relay-log = $BINLOG_DIR/relay-bin
slave-net-timeout = 60
sync-master-info = 1000
sync-relay-log = 1000
sync-relay-log-info = 1000
master-info-repository = TABLE
relay-log-info-repository = TABLE
relay_log_recovery = ON
# CACHES AND LIMITS #
tmp-table-size = 64M
max-heap-table-size = 128M
query-cache-type = 0
query-cache-size = 0
max-connections = 4096
thread-cache-size = 100
open-files-limit = 65535
table-definition-cache = 4096
table-open-cache = 4096
join_buffer_size = 16M
# INNODB #
innodb_data_file_path = ibdata1:100M:autoextend
innodb-flush-method = O_DIRECT
innodb-log-files-in-group = 2
innodb-log-file-size = 256M
innodb-flush-log-at-trx-commit = 1
innodb-file-per-table = 1
innodb-buffer-pool-size = 6G
innodb_buffer_pool_instances = 4
innodb_buffer_pool_dump_pct = 80
innodb_buffer_pool_dump_at_shutdown = 1 #该命令⽤于在关闭时把热数据dump到本地磁盘
innodb_buffer_pool_load_at_startup = 1 #在启动时把热数据加载到内存
innodb_flush_log_at_trx_commit = 1 #事务提交时把log buffer写⼊log file,并flush(刷到磁盘),系统默认。最安全也是最慢的。mysqld/主机crash,binlog可能丢最多⼀个语句或⼀个事务。innodb_flush_method = O_DIRECT #避免双写缓冲的参数
innodb_lock_wait_timeout = 60 # 锁等待超时时间单位秒
innodb_status_output = OFF #启⽤标准InnoDB监视器
query_cache_size = 0 # 关闭查询缓存
query_cache_type = 0 # 关闭查询缓存
# LOGGING #
log-error = $RUN_DIR/error_$PORT.log
log-queries-not-using-indexes = 1
slow-query-log = 1
slow-query-log-file = $RUN_DIR/slow-query_$PORT.log
long_query_time = 1
log_timestamps = system
# 半同步复制参数
#rpl_semi_sync_master_enabled = 1
#rpl_semi_sync_master_timeout = 1000 # 单位秒
## MGR START ##
gtid-mode = on #全局事务id
enforce-gtid-consistency = on #强制GTID的⼀致性
##master-info-repository = TABLE #将master.info元数据保存在系统表中
##relay-log-info-repository = TABLE #将relay.info元数据保存在系统表中
#binlog-checksum = none #禁⽤⼆进制⽇志事件校验
##log-slave-updates = on #级联复制
##log-bin=binlog#开启⼆进制⽇志记录
##binlog-format=ROW#以⾏的格式记录
#transaction-write-set-extraction=XXHASH64#使⽤哈希算法将其编码为散列
#loose-group_replication_group_name='ce9be252-2b71-11e6-b8f4-00212844f856'#加⼊的组名,可以修改,只要格式对#loose-group_replication_start_on_boot=off#不⾃动启⽤组复制集
#loose-group_replication_local_address='mgr1:33061'#以本机端⼝33061接受来⾃组中成员的传⼊连接
#loose-group_replication_group_seeds='mgr1:33061,mgr2:33062,mgr3:33063'#组中成员访问表
#loose-group_replication_bootstrap_group=off#不启⽤引导组
## MGR END ##
[mysqld_safe]
socket = $RUN_DIR/mysql_$PORT.sock
pid-file = $RUN_DIR/mysqld_$PORT.pid
nice = 0
[mysqldump]
quick
quote-names
max-allowed-packet = 16M
socket = $RUN_DIR/mysql_$PORT.sock
EOF
# 判断mysql软件包是否存在
if [ -f $SOFT_DIR/${MYSQL_VERSION}. ]
then
cd $SOFT_DIR
tar -zxvf $SOFT_DIR/$MYSQL_
rm -rf $MYSQLDB_HOME
mv -Tf $SOFT_DIR/$MYSQL_VERSION $MYSQLDB_HOME
else
echo "没有发现mysql⼆进制⽂件"
echo "下载mysql⼆进制⽂件放到和本脚本在同⼀⽬录中"
wget sql/archives/get/p/23/file/${MYSQL_VERSION}. -P $SOFT_DIR
if(( $? == 0 ))
then
echo "MySQL DownLoad sucessfully!"
cd $SOFT_DIR
tar -zxvf $SOFT_DIR/$MYSQL_
rm -rf $MYSQLDB_HOME
mv -Tf $SOFT_DIR/$MYSQL_VERSION $MYSQLDB_HOME
else
echo "MySQL DownLoad failed!"
exit $ERROR_EXIT
fi
fi
# 设置mysql⽬录权限
if [ -d $MYSQLDB_DATA_HOME ];then
echo "⽬录已存在"
else
mkdir -p $MYSQLDB_DATA_HOME
chown -R mysql:mysql $MYSQLDB_DATA_HOME
echo "⽬录和权限创建成功!"
fi
# 判断运⾏binlog⽬录是否创建
if [ -d $BINLOG_DIR ];then
echo $BINLOG_DIR"⽬录已创建"
else
mkdir -p $BINLOG_DIR
fi
# 判断运⾏⽇志⽬录是否创建
if [ -d $RUN_DIR ];then
echo "⽬录已创建"
else
mkdir -p $RUN_DIR
fi
# 设置tmp⽬录权限
if [ -d $TMP_DIR ];then
echo "⽬录已存在"
else
mkdir -p $TMP_DIR
chown -R mysql:mysql $TMP_DIR
echo "tmp⽬录和权限创建成功!"
fi
# ⽬录授权
chown -R mysql:mysql $MYSQLDB_HOME
echo $RUN_DIR && chown -R mysql:mysql $RUN_DIR
chown -R mysql:mysql $BINLOG_DIR
# 配置开机启动
#function add_auto_start
#{
# cp $MYSQLDB_HOME/support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod +x /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld
#}
# 初始化数据库
cat /dev/null > $RUN_DIR/error_$PORT.log
echo $RUN_DIR && chown -R mysql:mysql $RUN_DIR
$MYSQLDB_HOME/bin/mysqld --initialize --user=mysql --basedir=$MYSQLDB_HOME/ --datadir=$MYSQLDB_DATA_HOME/ >> $TMP_ # 设置环境变量
cat >> /etc/profile <<EOF
PATH=$MYSQLDB_HOME/bin:\$PATH
export PATH
EOF
source /etc/profile
#add_auto_start
echo "启动MySQL实例..."
#service mysqld start
$MYSQLDB_HOME/bin/mysqld_safe --defaults-file=/etc/myf &
sleep 5
>>>>>>>>>>>>>>>>>####
echo "mysql 5.7.25 安装完成,初始密码请查看$RUN_DIR/error_$PORT.log⽂件":
INIT_PASSWD=`cat $RUN_DIR/error_$PORT.log | grep password | awk '{print $NF}'`
echo "初始化root密码...$INIT_PASSWD"
mysql --connect-expired-password -e "alter user 'root'@'localhost' identified by 'mysql';flush privileges;" -p"$INIT_PASSWD"
echo "配置服务器本地免密登录..."
mysql_config_editor set -S $RUN_DIR/mysql_$PORT.sock -u root -p
mysql_config_editor print --all
echo "放开root⽤户客户端IP限制..."
mysql<<EOF
grant all privileges on *.* to 'root'@'%' identified by 'mysql' with grant option;
flush privileges;
quit
EOF
echo "DONE SUCCESSFULLY !"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
IDEA连接MySQL8.0.20
下一篇 »
推荐文章
热门文章
-
随机森林算法的改进方法
2024-10-02 -
基于随机森林算法的风险预警模型研究
2024-10-02 -
Python中的随机森林算法详解
2024-10-02 -
随机森林发展历史
2024-10-02 -
如何使用随机森林进行时间序列数据模式识别(八)
2024-10-02 -
随机森林回归模型原理
2024-10-02 -
如何使用随机森林进行时间序列数据模式识别(六)
2024-10-02 -
如何使用随机森林进行时间序列数据预测(四)
2024-10-02 -
如何使用随机森林进行异常检测(六)
2024-10-02 -
随机森林算法和grandientboosting算法 -回复
2024-10-02 -
随机森林方法总结全面
2024-10-02 -
随机森林算法原理和步骤
2024-10-02 -
随机森林的原理
2024-10-02 -
随机森林 重要性
2024-10-02 -
随机森林算法
2024-10-02 -
机器学习中随机森林的原理
2024-10-02 -
随机森林算法原理
2024-10-02 -
使用计算机视觉技术进行动物识别的技巧
2024-10-02 -
基于crf命名实体识别实验总结
2024-10-02 -
transformer预测模型训练方法
2024-10-02
最新文章
-
随机森林算法介绍及R语言实现
2024-10-02 -
基于随机森林优化的神经网络算法在冬小麦产量预测中的应用研究_百度文 ...
2024-10-02 -
基于正则化贪心森林算法的情感分析方法研究
2024-10-02 -
随机森林算法和grandientboosting算法
2024-10-02 -
基于随机森林的图像分类算法研究
2024-10-02 -
随机森林结合直接正交信号校正的模型传递方法
2024-10-02
发表评论