MySql教程

重新编写MySQL-5.7.19版本自动编译脚本

本文主要是介绍重新编写MySQL-5.7.19版本自动编译脚本,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

重新编写MySQL-5.7.19版本自动编译脚本

以往写过一个自动编译安装MySQL-5.7.19的脚本,总觉得不太满意,因此想把这个脚本重新优化一下,逻辑更清晰,更容易使用。

一,该脚本使用的唯二先决条件

不管是虚拟机使用,还是真实物理机使用,请先将系统安装ISO文件挂载为本地仓库,示例如下:

[root@master local]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
[root@master local]# pwd
/var/www/html/local
[root@master local]# ls -al
total 312
drwxr-xr-x. 8 root root    220 Jun 27 11:11 .
drwxr-xr-x. 3 root root     19 Jun 27 11:10 ..
-rw-r--r--. 1 root root     14 Jun 27 11:10 CentOS_BuildTag
drwxr-xr-x. 3 root root     35 Jun 27 11:10 EFI
-rw-r--r--. 1 root root    227 Jun 27 11:10 EULA
-rw-r--r--. 1 root root  18009 Jun 27 11:10 GPL
drwxr-xr-x. 3 root root     57 Jun 27 11:10 images
drwxr-xr-x. 2 root root    198 Jun 27 11:10 isolinux
drwxr-xr-x. 2 root root     43 Jun 27 11:10 LiveOS
drwxr-xr-x. 2 root root 217088 Jun 27 11:11 Packages
drwxr-xr-x. 2 root root   4096 Jun 27 11:11 repodata
-rw-r--r--. 1 root root   1690 Jun 27 11:11 RPM-GPG-KEY-CentOS-7
-rw-r--r--. 1 root root   1690 Jun 27 11:11 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--. 1 root root   2883 Jun 27 11:11 TRANS.TBL

 

以上表示,我安装系统时使用的是CentOS Linux release 7.4.1708ISO文件,将文件内容拷贝到 httpd服务的发布目录下,repo文件内容如下:

[root@master local]# cat /etc/yum.repos.d/local.repo 
[local-http]
name=local
baseurl=http://192.168.0.16/local/
enable=1
gpgcheck=0


 

 [root@master local]# yum repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                                   repo name                                                             status
local-http                                                                local                                                                 3,894
repolist: 3,894

执行命令yum repolist 有看到仓库表示本地仓库搭建完毕。

二,

 MySQL和它的依赖以及安装脚本的下载地址:

链接:https://pan.baidu.com/s/10QKQrr5VYdpNSkJvMngdVg 
提取码:mysq 

将所下载的文件上传到服务器任意位置。找到安装脚本,执行它就可以了,什么都不用做。

脚本所做的工作是直到MySQL数据库初始化完成,并启动一次MySQL服务为止。

三,

安装脚本的内容:

#!/bin/bash
#auther alwaysbefine
set -eu
USER=mysql
echo "添加mysql用户"
sleep 5
i=`cat /etc/passwd | cut -f1 -d':' | grep "$USER"|wc -l`
echo $i
if [ $i -le 0 ];
then
useradd -M -r -s  /sbin/nologin $USER
echo "$USER is success add!!!"
else
echo "user $USER is exits"
fi

sleep 5
echo "删除旧MySQL和mariadb"

#rpm -qa |grep -e "mysql" -e "mariadb" |xargs rpm -e --nodeps
echo "本脚本用于编译安装mysql5.7及以上版本,在使用前
请自行配置本地yum源,由于是源码编译安装,时间比较长,请耐心等待,并在脚本运行结束后运行命令进行初始化,命令为:
source /etc/profile && mysql_secure_installation ,首先输入脚本最后所生成的密码,然后按命令
提示选择y,重置密码后的选项为选择密码模式,分三种,低中高,分别用0.1,2代表,请根据自己的
需求输入数字,剩下的都为y除了再次提示修改密码选项为n
"
yum install autoconf automake bzip2-devel cpp fontconfig-devel freetype-devel gcc gcc-c++ ncurses-devel libaio libaio-devel -y
echo "编译所需安装包已安装完毕"
echo "新建mysql数据库存放目录以及配置文件目录"
echo "mysql的环境变量和cmake的环境变量添加"
echo "export PATH=$PATH:/usr/local/src/cmake/bin:/usr/local/mysql/bin" >> /etc/profile
sleep 5
mkdir -p /usr/local/mysql/var
mkdir -p /usr/local/mysql/etc
mkdir -p /var/lib/mysql
#wget https://cmake.org/files/v3.2/cmake-3.2.0-rc2-Linux-x86_64.tar.gz
#wget https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.19.tar.gz

echo "开始解压相关安装包,分别为cmake,所需c语音库boost.1.59,mysql5.7.19"
if [ `find / -name cmake-3.2.0-rc2-Linux-x86_64.tar.gz|wc -l` -eq 0 ]
then
echo "cmake 安装包不存在,请拷贝"
exit
else
find / -name cmake-3.2.0-rc2-Linux-x86_64.tar.gz|xargs tar -zxf
fi

 
if [ `find / -name boost_1_59_0.tar.gz|wc -l` -eq 0 ]
then 
echo "boost 安装包不存在,请拷贝"
exit
else
tar -zxf `find / -name boost_1_59_0.tar.gz` -C /usr/local/src/
fi


if [ `find / -name mysql-5.7.19.tar.gz|wc -l` -eq 0 ]
then 
echo "mysql-5.7.19 安装包不存在,请拷贝"
exit
else
tar -zxf `find / -name mysql-5.7.19.tar.gz` -C /usr/local/src/
fi
rm -rf /usr/local/src/cmake
mv -f cmake-3.2.0-rc2-Linux-x86_64 /usr/local/src/cmake
echo "开始预编译"
export PATH=/usr/local/bin:/usr/bin:/usr/sbin/:/usr/local/src/cmake/bin:/usr/local/mysql/bin
cd /usr/local/src/mysql-5.7.19/
cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ -DINSTALL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/usr/local/mysql/etc -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DDOWNLOAD_BOOST=0 -DWITH_BOOST=/usr/local/src/boost_1_59_0
echo "正式编译,并生成编译日志在root目录下的makelog.log"
make 2>&1 |tee /root/makelog.log && make install
echo "mysql启动脚本放入开机启动目录"
cp -f /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
rm -rf /etc/my.cnf 
rm -rf /usr/local/mysql/var/*
chown -Rf mysql. /usr/local/mysql
chown mysql:mysql /etc/init.d/mysql
chmod +x /etc/init.d/mysql
chown -Rf mysql. /var/lib/mysql/
echo "生成mysql最主要的配置文件 my.cnf"
echo "
[client]
default-character-set=utf8
port=3306
socket=/var/lib/mysql/mysql.sock
[mysqldump]
quick
max_allowed_packet = 16M
[mysqld]
bind-address = 0.0.0.0
basedir = /usr/local/mysql
datadir = /usr/local/mysql/var
port=3306
server-id = 1
socket=/var/lib/mysql/mysql.sock
character-set-server = utf8
collation-server = utf8_general_ci
init_connect='SET NAMES utf8mb4'
default-storage-engine=INNODB
default_authentication_plugin=mysql_native_password
skip-character-set-client-handshake = true
lower_case_table_names = 1
key_buffer_size=16M
max_allowed_packet=16M
sql_mode=TRADITIONAL
[mysql]
default-character-set = utf8mb4
">/etc/my.cnf
echo "赋予开机启动脚本执行权限"
echo "生成数据库初始文件,并产生mysql初始密码,密码也保存在root目录下的passwd.txt"
/usr/local/mysql/bin/mysqld --initialize --user=mysql \
--basedir=/usr/local/mysql --datadir=/usr/local/mysql/var --collation-server=utf8_general_ci \
 2>&1 |tee /root/passwd.txt
echo "mysql加入启动项,并首次启动mysql服务"
 chkconfig --add mysql && chkconfig mysql on && service mysql start

四,

安装脚本的执行成功截图:

五,

安装脚本的功能

MySQL数据库安装位置是固定在脚本内的,路径为 : /usr/local/mysql ,该路径是预编译阶段所定义,如有需要请将预编译以及其后的所有路径自定义设置。

该脚本可反复执行,每一次执行成功后,都会是一个新的数据库覆盖旧的数据库。

六,脚本执行完毕后的后续工作

(1)MySQL5.7在安装完毕后,会提供一个随机生成的密码,通常这个密码是要更改掉的,因此,我们需要先更改 /etc/my.cnf 文件,将第三行和第二十七行注释掉,然后重启服务,重启命令为:service mysql restart

(2)执行命令:mysql_secure_installation  如下所示例,先输入前面所生成的随机密码,本例是j1%jfxgw%Dij,然后输入自己要的密码并确认。:

[root@slave1 ~]# service mysql restart
Shutting down MySQL.. SUCCESS! 
Starting MySQL. SUCCESS! 
[root@slave1 ~]# mysql_secure_installation 

Securing the MySQL server deployment.

Enter password for user root: 

The existing password for the user account root has expired. Please set a new password.

New password: 

(3)剩下的选择。

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

我选择的是2,2代表密码等级最高,如果再次设置密码,会要求大小写以及数字字母组合的高复杂度密码,这里,可根据自己的需要。然后要求更改密码,当然拒绝,就是n了。后面的就是远程连接关闭啊这些的,按我的来就可以了。

Re-enter new password: 

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Using existing password for root.

Estimated strength of the password: 100 
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done! 

命令执行完毕后,在将/etc/my.cnf文件注释的两行恢复,再次重启服务就可以舒服的使用了!!!!!~~~~~~~~~~~~~~

这篇关于重新编写MySQL-5.7.19版本自动编译脚本的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!