CentOS7源码mysql-5.7.30自动安装脚本(亲测)

mysql-5.7.30源码自动安装部署脚本

脚本部署环境

[root@shell ~]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
[root@shell ~]# uname -a
Linux shell 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

mysql安装目录:/usr/local/mysql5.7
mysql数据目录:/data/mysql5.7
mysql日志、pid、socket文件:/data/mysql5.7
mysql采用默认端口:port 3306
自动安装脚本

#!/bin/bash
#written by rookie
# 2020-07-03-17:06:00
################################

echo "安装基本工具"
yum install -y vim wget curl tar

echo "国内ali源、epel源"
wget -O /etc/yum.repos.d/CentOS-aliyun.repo https://mirrors.aliyun.com/repo/Centos-7.repo
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum clean all
yum makecache

echo "解决mysql依赖"
yum install gcc gcc-c++ cmake bison ncurses ncurses-devel libaio git openssl openssl-devel  -y

echo "安装源码boost"
cd /usr/src/
wget -c http://nchc.dl.sourceforge.net/project/boost/boost/1.59.0/boost_1_59_0.tar.gz
tar -zxf boost_1_59_0.tar.gz
mv boost_1_59_0 /usr/local/boost

echo "下载源码mysql"
cd /usr/src/
wget -c http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.30.tar.gz
tar -zxf mysql-5.7.30.tar.gz

echo "预编译"
cd /usr/src/mysql-5.7.30
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql5.7   \
        -DMYSQL_UNIX_ADDR=/data/mysql5.7/mysql.sock  \
        -DMYSQL_DATADIR=/data/mysql5.7               \
        -DSYSCONFDIR=/usr/local/mysql5.7             \
        -DMYSQL_USER=mysql                           \
        -DMYSQL_TCP_PORT=3306                        \
        -DWITH_XTRADB_STORAGE_ENGINE=1               \
        -DWITH_INNOBASE_STORAGE_ENGINE=1             \
        -DWITH_PARTITION_STORAGE_ENGINE=1            \
        -DWITH_BLACKHOLE_STORAGE_ENGINE=1            \
        -DWITH_MYISAM_STORAGE_ENGINE=1               \
        -DWITH_READLINE=1                            \
        -DENABLED_LOCAL_INFILE=1                     \
        -DWITH_EXTRA_CHARSETS=1                      \
        -DDEFAULT_CHARSET=utf8                       \
        -DDEFAULT_COLLATION=utf8_general_ci          \
        -DEXTRA_CHARSETS=all                         \
        -DWITH_BIG_TABLES=1                          \
        -DWITH_DEBUG=0                               \
        -DENABLE_DTRACE=0                            \
        -DDOWNLOAD_BOOST=1                           \
        -DWITH_BOOST=/usr/local/boost
echo $?
echo "编译"
make -j 2
echo $?
echo "安装"
make install
echo $?

echo "初始登录用户"
useradd -r -s /sbin/nologin mysql

echo "数据目录授权"
mkdir -p /data/mysql5.7/
chown -R mysql. /data/mysql5.7

echo "拷贝源码目录的脚本文件"
cd /usr/src/mysql-5.7.30
cp support-files/mysql.server /etc/init.d/mysqld5.7
chmod +x /etc/init.d/mysqld5.7

#mysql优先/etc/my.cnf>/etc/mysql/my.cnf>PREFIX/my.cnf
echo "创建my.cnf配置文件"
cat >/usr/local/mysql5.7/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql5.7/
datadir=/data/mysql5.7/
port=3306
pid-file=/data/mysql5.7/mysql.pid
socket=/data/mysql5.7/mysql.sock
[mysqld_safe]
log-error=/data/mysql5.7/mysql.log
EOF

echo "初始化数据库,mysql.log记录初始化信息"
#注意:拷贝时注意初始化命令转义符位置避免缺少参数,否则初始化后数据目录内容不全;
/usr/local/mysql5.7/bin/mysqld --initialize --user=mysql --datadir=/data/mysql5.7 \
                               --basedir=/usr/local/mysql5.7/ --log-error=/data/mysql5.7/mysql.log
echo "启动停止mysql数据库"
#也可以通过service启停服务;
/etc/init.d/mysqld5.7 start

echo "添加sysv风格mysql服务自启"
#注意添加服务与/etc/init.d/mysqld5.7对应;
#也可以通过拷贝修改/usr/lib/systemd/system/服务生成systemd风格服务;
chkconfig --add mysqld5.7
chkconfig --level 35 mysqld5.7 on

#查看mysqld服务;
netstat -antp | grep mysqld
if(( $?==0 ));then echo "mysqld启动成功";else echo "mysqld启动失败";fi
ps -ef | grep mysqld


MYSQL_INI_PWD=$(awk '/temporary password/{print $NF}' /data/mysql5.7/mysql.log)
/usr/local/mysql5.7/bin/mysql -uroot -p$MYSQL_INI_PWD

脚本测试:
通过sh -x scripts命令显示当前执行的命令和参数,可用于排错;
脚本测试成功会使用root及数据库初始化临时生成的密码登录mysql;

sh -x autosrc_install_mysql_v1.sh
+ netstat -antp
+ grep mysqld
tcp6       0      0 :::3306                 :::*                    LISTEN      31662/mysqld        
+ ((  0==0  ))
+ echo $'mysqld\345\220\257\345\212\250\346\210\220\345\212\237'
mysqld启动成功
+ ps -ef
+ grep mysqld
root      31432      1  0 14:57 pts/1    00:00:00 /bin/sh /usr/local/mysql5.7//bin/mysqld_safe --datadir=/data/mysql5.7/ --pid-file=/data/mysql5.7/mysql.pid
mysql     31662  31432  6 14:57 pts/1    00:00:00 /usr/local/mysql5.7/bin/mysqld --basedir=/usr/local/mysql5.7/ --datadir=/data/mysql5.7 --plugin-dir=/usr/local/mysql5.7//lib/plugin --user=mysql --log-error=/data/mysql5.7/mysql.log --pid-file=/data/mysql5.7/mysql.pid --socket=/data/mysql5.7/mysql.sock --port=3306
root      31733  10863  0 14:57 pts/1    00:00:00 grep mysqld
+ /usr/local/mysql5.7/bin/mysql -uroot -pRA9yULbP_lpq
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

你可能感兴趣的:(mysql,shell)