Linux环境使用Shell脚本安装Mysql5.7
1. 前言
之前写过使用shell脚本安装Mysql5.6版本,闲暇之余研究了下mysql5.7与5.6安装的区别,写下Mysql5.7的shell运维脚本,供大家参考。(Linux环境使用Shell脚本安装Mysql5.6:https://blog.csdn.net/u011294519/article/details/89004581)
2.Mysql5.6与5.7的安装区别
- Mysql5.7的安装是使用/bin/mysqld,Mysql5.6是使用/scripts/mysql_install_db
- Mysql5.7没有默认的配置文件my-default.cnf,Mysql5.6在/support-files目录下有my-default.cnf,可以作为my.cnf文件模板
- Mysql5.7安装完成后首次本机登录不能使用127.0.0.1,必须使用localhost,Mysql5.6则可以使用127.0.0.1
3. 准备工作
到Mysql官网或者到华为软件仓库去下载Mysql安装包
官网链接:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
有32位和64位的区分 ,根据需要下载
华为软件仓库:https://mirrors.huaweicloud.com/mysql/Downloads/MySQL-5.7/
我下载的版本是mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
下载好安装包后拷贝到Linux服务器的任意位置上
检查机器上是否安装autoconf,执行命令:rpm -qa|grep autoconf
若无如上输出,则执行命令安装:yum -y install autoconf
4. 配置脚本
脚本内容如下
#!/bin/bash
# Mysql安装包所在路径,需要带上包名,示例:PACKAGE_FULL_WAY=/root/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
readonly PACKAGE_FULL_WAY=
# Mysql安装主目录,示例:INSTALL_HOME=/usr/local/mysql
readonly INSTALL_HOME=
# Mysql数据库root用户密码,示例:USER_PASSWD=root
readonly USER_PASSWD=
#check user
if [[ "$UID" -ne 0 ]]; then
echo "ERROR: the script must run as root"
exit 3
fi
function log_info() {
echo "[$(date -d today +"%Y-%m-%d %H:%M:%S %:::z")] $1"
}
function log_error() {
echo -e "[$(date +"%Y-%m-%d %H:%M:%S %Z%:z")] [ERROR] $* \n"
exit 1
}
function check_result() {
local ret_code=$1
shift
local error_msg=$*
if [[ ${ret_code} -ne 0 ]]; then
log_error ${error_msg}
fi
}
# 校验参数
function check_param() {
if [[ ! -n ${PACKAGE_FULL_WAY} ]] || [[ ! -n ${INSTALL_HOME} ]] || [[ ! -n ${USER_PASSWD} ]]; then
log_error "Param: PACKAGE_FULL_WAY INSTALL_HOME USER_PASSWD can not be null"
fi
if [[ ! -f ${PACKAGE_FULL_WAY} ]]; then
log_error "Please check the config of PACKAGE_FULL_WAY dose config Mysql package name"
fi
}
function check_mysql_process() {
local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
if [[ ${mysql_process_count} -gt 0 ]]; then
log_error "please stop and uninstall the mysql first"
fi
}
# 新建mysql用户
function add_user() {
#create group mysql
grep "^mysql" /etc/group &> /dev/null
if [[ $? -ne 0 ]]; then
groupadd mysql
fi
#create user mysql
id mysql &> /dev/null
if [[ $? -ne 0 ]]; then
useradd -g mysql mysql
chage -M 99999 mysql
fi
}
# 初始化my.cnf,按需配置
function init_my_conf() {
local host_ip=$(/sbin/ip route get 1| sed 's/^.*src \(\S*\).*$/\1/;q')
local total_mem=`free -m |grep "Mem"|awk '{print $2}'`
mkdir -p ${INSTALL_HOME}/log
mkdir -p ${INSTALL_HOME}/mysqld
touch ${INSTALL_HOME}/mysqld/mysqld.pid
chown mysql.mysql ${INSTALL_HOME}/mysqld/mysqld.pid
touch ${INSTALL_HOME}/log/mysql-error.log
touch ${INSTALL_HOME}/log/mysql-slow.log
touch ${INSTALL_HOME}/log/mysqld.log
chown mysql.mysql -R ${INSTALL_HOME}/log
echo "[mysql]
default-character-set=UTF8
[mysqld]
socket=/opt/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
port = 3306
basedir = ${INSTALL_HOME}
datadir = ${INSTALL_HOME}/data/mysql
pid-file = ${INSTALL_HOME}/mysql.pid
user = mysql
server-id = 1
#rpl_semi_sync_master_enabled=1
#rpl_semi_sync_master_timeout=1000
#rpl_semi_sync_slave_enabled=1
relay_log_purge=0
read_only=0
slave-skip-errors=1396
lower_case_table_names = 1
character-set-server=utf8
skip-name-resolve
skip-external-locking
back_log = 500
max_connections = 500
max_connect_errors = 2000
open_files_limit = 65535
table_open_cache = 128
max_allowed_packet = 64M
key_buffer_size = 64M
read_buffer_size = 64M
read_rnd_buffer_size = 16M
sort_buffer_size = 16M
join_buffer_size = 16M
tmp_table_size = 96M
max_heap_table_size = 96M
query_cache_size = 8M
query_cache_limit = 8M
thread_cache_size = 64
log_bin = mysql-bin
binlog_format = mixed
binlog_cache_size = 8M
sync_binlog = 1
max_binlog_cache_size = 8M
max_binlog_size = 500M
expire_logs_days = 10
log_error = ${INSTALL_HOME}/log/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = ${INSTALL_HOME}/log/mysql-slow.log
default_storage_engine = InnoDB
innodb_buffer_pool_size = ${total_mem}
innodb_file_per_table = 1
innodb_data_home_dir = ${INSTALL_HOME}/data/mysql
innodb_log_group_home_dir = ${INSTALL_HOME}
innodb_log_file_size = 500M
innodb_log_buffer_size = 20M
innodb_flush_log_at_trx_commit = 1
innodb_print_all_deadlocks = 1
[mysqld_safe]
log-error=${INSTALL_HOME}/log/mysqld.log" >/etc/my.cnf
chown mysql.mysql /etc/my.cnf
}
# 安装Mysql
function install_mysql() {
# 创建安装主目录
mkdir -p ${INSTALL_HOME}
# 解压mysql到安装主目录
tar -zxvf ${PACKAGE_FULL_WAY} -C ${INSTALL_HOME} > /dev/null 2>&1
check_result $? "unzip Mysql package error"
local package_name=`ls ${INSTALL_HOME} |grep mysql`
mv ${INSTALL_HOME}/${package_name}/* ${INSTALL_HOME}
rm -rf ${INSTALL_HOME}/${package_name}
cd ${INSTALL_HOME}
# 新建数据库目录
mkdir -p ${INSTALL_HOME}/data/mysql
chown -R mysql:mysql ${INSTALL_HOME}
# 安装并指定用户和data文件夹位置
./bin/mysqld --initialize-insecure --user=mysql --basedir=${INSTALL_HOME} --datadir=${INSTALL_HOME}/data/mysql
# 初始化my.cnf,按需配置
init_my_conf
# 复制mysql到服务自动启动里面
cp -pf ${INSTALL_HOME}/support-files/mysql.server /etc/init.d/mysqld
chmod 755 /etc/init.d/mysqld
# 修改basedir和datadir
sed -i "s#^basedir=.*#basedir=${INSTALL_HOME}#" /etc/init.d/mysqld
sed -i "s#^datadir=.*#datadir=${INSTALL_HOME}\/data\/mysql#" /etc/init.d/mysqld
# 加入环境变量,方便使用mysql命令,但是需要source /etc/profile
echo "###MYSQL_PATH_ENV_S" >>/etc/profile
echo "export PATH=${INSTALL_HOME}/bin:\$PATH" >> /etc/profile
echo "###MYSQL_PATH_ENV_E" >> /etc/profile
# 启动Mysql
start
if [[ ! -L ${INSTALL_HOME}/mysql.sock ]]; then
ln -s ${INSTALL_HOME}/mysql.sock /tmp
fi
# 修改Mysql用户root密码
./bin/mysqladmin -u root password ${USER_PASSWD}
cd ${INSTALL_HOME}
# 开启远程登录权限
./bin/mysql -hlocalhost -uroot -p${USER_PASSWD} << EOF
grant all privileges on *.* to aaa@qq.com'%' identified by 'root'; flush privileges;
EOF
chown -R mysql:mysql ${INSTALL_HOME}
}
# 安装Mysql
function install() {
log_info "+++++++++++ step 1 ++++++++++++++++"
check_param
log_info "check_param finish"
log_info "+++++++++++ step 2 ++++++++++++++++"
check_mysql_process
log_info "check_mysql_process finish"
log_info "+++++++++++ step 3 ++++++++++++++++"
add_user
log_info "add_user finish"
log_info "+++++++++++ step 4 ++++++++++++++++"
install_mysql
log_info "install_mysql finish"
}
# 卸载Mysql
function uninstall() {
# 如果Mysql仍启动则停止Msql
local mysql_process_count=`ps -ef |grep ${INSTALL_HOME}|grep -vwE "grep|vi|vim|tail|cat"|wc -l`
if [[ ${mysql_process_count} -gt 0 ]]; then
stop
fi
# 删除创建的文件
rm -rf ${INSTALL_HOME}
rm -rf /etc/init.d/mysqld
rm -rf /etc/my.cnf
if [[ -L /tmp/mysql.sock ]]; then
rm -rf /tmp/mysql.sock
fi
# 删除配置的环境变量
sed -i '/###MYSQL_PATH_ENV_S/,/###MYSQL_PATH_ENV_E/d' /etc/profile
#删除用户和用户组
id mysql &> /dev/null
if [[ $? -eq 0 ]]; then
userdel mysql
fi
log_info "uninstall Mysql success"
}
# 停止Mysql
function stop() {
su - mysql -c "service mysqld stop"
}
# 启动Mysql
function start() {
su - mysql -c "service mysqld start"
}
# Mysql状态检查
function check_status() {
su - mysql -c "service mysqld status"
}
function usage() {
echo "Usage: $PROG_NAME {start|stop|install|uninstall|check_status}"
exit 2
}
PROG_NAME=$0
ACTION=$1
case "$ACTION" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
install)
install
;;
uninstall)
uninstall
;;
check_status)
check_status
;;
*)
usage
;;
esac
使用root用户登录Linxu服务器,在任意目录下执行命令
vim mysql_manager.sh
按一下insert键,复制以上的脚本内容,在机器上右键粘贴进去
复制完成后需要修改脚本最上方的三个配置项
PACKAGE_FULL_WAY |
Mysql安装包所在路径,需要带上包名 示例:PACKAGE_FULL_WAY=/root/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz |
INSTALL_HOME |
Mysql安装主目录 示例:INSTALL_HOME=/usr/local/mysql |
USER_PASSWD |
Mysql数据库root用户密码 示例:USER_PASSWD=root |
修改完成后按一下Esc键,键盘输入:wq!即可
脚本命令:
sh mysql_manager.sh install |
安装并启动Mysql |
sh mysql_manager.sh start |
启动Mysql |
sh mysql_manager.sh stop |
关闭Mysql |
sh mysql_manager.sh restart |
重启Mysql |
sh mysql_manager.sh check_status |
Mysql状态检查 |
sh mysql_manager.sh uninstall |
卸载Mysql |
如果有Shell脚本大神觉得写得不对的地方请留言指正
5.安装后校验
使用Navicat登录
若出现无法连接的情况需要检查是不是服务器防火墙没有放开,执行service iptables stop关闭或放开3306端口即可
上一篇: NPS内网穿透搭建与配置
下一篇: 一些基本命令
推荐阅读
-
Linux环境使用Shell脚本安装Mysql5.7
-
【学习笔记 - Linux】CentOS7下安装jdk1.8、tomcat8.5、mysql5.7——搭建java web开发环境
-
Linux自动化脚本安装Nginx使用说明
-
【mysql】Linux centos 7.5 + mysql5.7二进制自动安装脚本(包括自动修改root密码)
-
使用Linux的Shell脚本定时处理MySQL超时
-
使用Linux的Shell脚本定时处理MySQL超时
-
在linux上使用shell .sh脚本删除指定目录中指定某一时间段之前创建的文件及文件夹的脚本
-
Linux环境下shell脚本中调用另一个shell脚本
-
Linux 安装Oracle的shell脚本
-
Linux 使用脚本安装MySQL数据库