Centos源码安装mysql5.7.28(脚本)

注:
建议先在/usr/src目录下下载好boost_1_59_0.tar.gz及mysql-5.7.28.tar.gz,因为下载很慢,wget -c 时会检查,如果已经存在源码包就不会再下载了

#!/bin/bash
#2020712319#auto_SourceCode_install_mysql
#by author toyix
################################
yum install lrzsz vim wget -y
echo "安装国内base及epel源"
curl -o	/etc/yum.repos.d/CentOS-Base.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 "安装软件环境依赖"
yum install -y boost gcc ncurses-devel libaio bison gcc-c++ git cmake ncurses-devel openssl openssl-devel
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 -xvf boost_1_59_0.tar.gz
echo "移动并改目录名"
mv boost_1_59_0  /usr/local/boost
echo "下载mysql5.7源码包"
wget  -c  http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.28.tar.gz
echo "解压"
tar -xvf mysql-5.7.28.tar.gz
cd mysql-5.7.28
echo "预编译"
cmake .  \
 -DCMAKE_INSTALL_PREFIX=/usr/local/mysql57 \
 -DMYSQL_UNIX_ADDR=/data/mysql57/mysql.sock \
 -DMYSQL_DATADIR=/data/mysql57 \
 -DSYSCONFDIR=/usr/local/mysql57 \
 -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 "编译及安装"
make -j4 
make -j4 install
echo "创建数据目录"
mkdir -p /data/mysql57
echo "新建mysql用户且不能登录操作系统"
useradd -s /sbin/nologin -r mysql
echo "将数据目录权限给mysql用户"
chown -R mysql. /data/mysql57
echo "新建启动文件(二进制文件)"
cp support-files/mysql.server /etc/init.d/mysqld57
echo "给二进制文件加可执行权限"
chmod +x /etc/init.d/mysqld57
echo "配置mysql配置文件"
cat >/usr/local/mysql57/my.cnf<<EOF
[mysqld]
basedir=/usr/local/mysql57/
datadir=/data/mysql57/
port=3306
pid-file=/data/mysql57/mysql.pid
socket=/data/mysql57/mysql.sock

[mysqld_safe]
log-error=/data/mysql57/mysql.log
EOF
echo "初始化数据库"
/usr/local/mysql57/bin/mysqld --initialize --user=mysql --datadir=/data/mysql57 --basedir=/usr/local/mysql57
echo "将mysql设置为系统服务,并开机自动启动"
chkconfig --add mysqld57
chkconfig --level 35 mysqld57 on
echo "关闭、启动数据库"
/etc/init.d/mysqld57 stop
/etc/init.d/mysqld57 start
echo "查看进程"
ps -ef|grep mysql
echo "查看端口"
netstat -tnlp |grep mysql

执行后结果

-- Installing: /usr/local/mysql57/share/aclocal/mysql.m4
-- Installing: /usr/local/mysql57/support-files/mysql.server
创建数据目录
新建mysql用户且不能登录操作系统
将数据目录权限给mysql用户
新建启动文件(二进制文件)
给二进制文件加可执行权限
配置mysql配置文件
初始化数据库
2020-07-02T03:26:18.175353Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-02T03:26:19.809160Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-02T03:26:20.267685Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-02T03:26:20.286493Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cc19c386-bc13-11ea-930a-000c295d3f32.
2020-07-02T03:26:20.288864Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-02T03:26:20.680530Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-02T03:26:20.695944Z 1 [Note] A temporary password is generated for root@localhost: kkOkiiZHq4(a     数据库root密码
将mysql设置为系统服务,并开机自动启动
关闭、启动数据库
 ERROR! MySQL server PID file could not be found!
Starting MySQL.Logging to '/data/mysql57/mysql.log'.
. SUCCESS! 
查看进程
root     37828  2103  0 10:55 pts/0    00:00:00 /bin/bash ./auto_install_mysql_v1.sh
root     58257     1  1 11:26 pts/0    00:00:00 /bin/sh /usr/local/mysql57//bin/mysqld_safe --datadir=/data/mysql57/ --pid-file=/data/mysql57/mysql.pid
mysql    58485 58257 18 11:26 pts/0    00:00:00 /usr/local/mysql57/bin/mysqld --basedir=/usr/local/mysql57/ --datadir=/data/mysql57 --plugin-dir=/usr/local/mysql57//lib/plugin --user=mysql --log-error=/data/mysql57/mysql.log --pid-file=/data/mysql57/mysql.pid --socket=/data/mysql57/mysql.sock --port=3306
root     58517 37828  0 11:26 pts/0    00:00:00 grep mysql
查看端口
tcp6       0      0 :::3306                 :::*                    LISTEN      58485/mysqld        
[root@localhost src]# 

登录测试

[root@localhost src]# /usr/local/mysql57/bin/mysql -u root -p"kkOkiiZHq4(a"
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 8
Server version: 5.7.28

Copyright (c) 2000, 2019, 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

Last login: Thu Jul  2 08:51:08 2020 from 192.168.1.103
[root@localhost ~]# ps -ef|grep mysql
root      1657     1  0 11:35 ?        00:00:00 /bin/sh /usr/local/mysql57//bin/mysqld_safe --datadir=/data/mysql57/ --pid-file=/data/mysql57/mysql.pid
mysql     1950  1657  3 11:35 ?        00:00:00 /usr/local/mysql57/bin/mysqld --basedir=/usr/local/mysql57/ --datadir=/data/mysql57 --plugin-dir=/usr/local/mysql57//lib/plugin --user=mysql --log-error=/data/mysql57/mysql.log --pid-file=/data/mysql57/mysql.pid --socket=/data/mysql57/mysql.sock --port=3306
root      2358  2340  0 11:36 pts/0    00:00:00 grep --color=auto mysql
[root@localhost ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2028/master         
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1526/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      2028/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1950/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1526/sshd           
[root@localhost ~]# 

OK
-------------------------end

你可能感兴趣的:(SHELL(脚本),mysql,LNMP)