一键以二进制方式安装数据库
压缩包解压在了/usr/local 里 数据库使用位置可以执行定义,需要自行准备数据库的安装文件
安装包下载目录:https://downloads.mariadb.org/
二进制名称:mariadb-10.4.10-linux-x86_64.tar.gz
带systemctl管理的名称:mariadb-10.4.10-linux-systemd-x86_64.tar.gz (for systems with systemd)
脚本效果
补上脚本效果,前几篇光顾着写,忘记了贴上效果,和怎么使用了 = =,嘿嘿
- 把包和脚本放在一个目录里,然后,脚本名,后面跟上包名。
- 看最后的两句提示,如果你没有输入密码,也就是需要自己手动执行mysql_secure_installation
- 最后需要运行一下提示,用来更改path变量,使用mysql的命令
- 如果是最小化安装可能会出现缺少libaio库,需要yum install libaio
[[email protected] ~]#ll
total 1207700
-rw-------. 1 root root 1377 Oct 26 11:10 anaconda-ks.cfg
-rw-r--r--. 1 root root 21702 Nov 13 21:44 init_env_191112.sh
-rw-r--r--. 1 root root 776077769 Nov 18 09:31 mariadb-10.2.29-linux-systemd-x86_64.tar.gz
-rw-r--r--. 1 root root 460570427 Nov 18 09:30 mariadb-10.2.29-linux-x86_64.tar.gz
-rw-r--r--. 1 root root 2169 Nov 19 09:44 onekeysql.sh
[[email protected] ~]#sh onekeysql.sh mariadb-10.2.29-linux-x86_64.tar.gz
input install dir:/data/mysql
input set mysql root passwd (Optional):123456
Thanks for using MariaDB!
mysql_secure_installation is complete.
please run 'source /etc/profile.d/mysql.sh'
[[email protected] ~]#
[[email protected] ~]#source /etc/profile.d/mysql.sh
[[email protected] ~]#mysql -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 18
Server version: 10.2.29-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.00 sec)
MariaDB [(none)]>
脚本代码
#!/bin/bash
#
#********************************************************************
#Author: liuhao
#QQ: 1921160095
#Date: 2019-11-18
#FileName: onekeysql.sh
#Description: The test script
#Copyright (C): 2019 All rights reserved
#********************************************************************
echo "二进制方式安装数据库,执行脚本时,后面需要跟上压缩包。"
yum install libaio
if ! $1 &> /dev/null ;then
package=$1
package1=`echo $package | sed -nr 's#^(.*)\..*\..*#\1#p'`
#设置安装的目录
read -p "input install dir:" destdir
# 用来初始化数据库时,使用设置数据库密码,可以不选,后面自己手动执行
read -p "input set mysql root passwd (Optional):" sqlpasswd
# 判断是否是绝对路径
if echo $destdir | grep "^/" &> /dev/null; then
if id mysql &> /dev/null ; then
echo "user is created"
else
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -d $destdir mysql
fi
mkdir -p $destdir
chown mysql:mysql $destdir
if [ -d /usr/local/mysql ];then
echo "/usr/local/mysql is exist!"
else
tar -xf $package -C /usr/local/
cd /usr/local/
ln -sv $package1 mysql
chown -R root:root /usr/local/mysql/
cp -f /usr/local/mysql/support-files/my-huge.cnf /etc/my.cnf
sed -i "/mysqld\]/a datadir\t\t= $destdir" /etc/my.cnf
/usr/local/mysql/scripts/mysql_install_db --datadir=$destdir --user=mysql
# 判断是否可以使用systemctl启动管理,否则就使用service
if [ -f /usr/local/mysql/support-files/systemd/mariadb.service ];then
cp /usr/local/mysql/support-files/systemd/mariadb.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl enable --now mariadb
else
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
fi
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
chmod +x /etc/profile.d/mysql.sh
# 如果输入密码了,就帮助初始化数据库。
if ! $sqlpasswd &> /dev/null ;then
/usr/local/mysql/bin/mysql_secure_installation << eof
y
$sqlpasswd
$sqlpasswd
y
y
y
y
eof
echo -e " \e[31m mysql_secure_installation is complete. \e[0m"
echo -e " \e[31m please run 'source /etc/profile.d/mysql.sh' \e[0m"
else
echo -e " \e[31m please manual exec mysql_secure_installation \e[0m"
echo -e " \e[31m please run 'source /etc/profile.d/mysql.sh' \e[0m"
fi
fi
else
echo "please input Absolute path !!"
fi
else
echo -e "$0 is need input xxx.tar.gz\neg:$0 mysql.tar.gz"
fi