mysql二進制安裝腳本部署
目錄
- mysql二進制安裝腳本部署
- 單實體
- 使用函式的單實體
- 使用函式的單實體或者多實體
單實體
[root@localhost ~]# mkdir mysql //創建存放腳本目錄
[root@localhost ~]# ls
anaconda-ks.cfg mysql
[root@localhost ~]# cd mysql/
[root@localhost mysql]# mkdir files //創建安裝包目錄
[root@localhost mysql]# ls
files
[root@localhost mysql]# ls files/
mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
[root@localhost mysql]# touch mysql_install.sh //創建腳本檔案
[root@localhost mysql]# chmod +x mysql_install.sh //賦予執行權限
[root@localhost mysql]# ll
total 0
drwxr-xr-x. 2 root root 56 Sep 19 21:16 files
-rwxr-xr-x. 1 root root 0 Sep 19 21:18 mysql_install.sh
[root@localhost mysql]# vim mysql_install.sh
[root@localhost mysql]# cat mysql_install.sh
#!/bin/bash
#設定執行權限
if [ $UID -ne 0 ];then
echo "請以管理員用戶進行執行"
exit
fi
read -p "請輸入要創建的實體個數: " count
read -p "請輸入資料存放目錄(默認路徑: /opt/xbz): " datadir
read -p "請輸入要為資料庫設定的密碼: " passwd
read -p "請輸入安裝目錄,(默認路徑:/usr/local/mysql):" mysql_install_dir
#判斷安裝目錄合法性
echo $mysql_install_dir | grep -E '^/[a-z][a-z]*(/[a-z][a-z]*)*$' &> /dev/null
if [ $? -eq 0 ];then
if [ ! -d $mysql_install_dir ];then
mkdir -p $mysql_install_dir
fi
else
mysql_install_dir=/usr/local/mysql
fi
#判斷安裝目錄是否為空
if [ -z $mysql_install_dir ];then
mysql_install_dir=/usr/local/mysql
fi
#創建用戶
id mysql &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
else
echo "用戶已存在"
fi
#安裝依賴包
dnf -y install ncurses-compat-libs &> /dev/null
#解壓軟體包,修改目錄和所屬組
if [ ! -d $mysql_install_dir ];then
echo "解壓軟體包"
tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
fi
chown -R mysql.mysql ${mysql_install_dir}
#設定環境變數
echo 'export PATH=${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
#做頭檔案
ln -s ${mysql_install_dir}/include /usr/include/mysql
#配置lib
echo '${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
#設定man檔案
grep '${mysql_install_dir}/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i "22a MANDATORY_MANPATH ${mysql_install_dir}/man" /etc/man_db.conf
fi
#建立資料存放目錄
for i in $(seq $count);do
if [ $count -eq 1 ];then
if [ -z $datadir ];then
datadir=/opt/xbz
fi
if [ ! -d $datadir ];then
mkdir -p $datadir
fi
chown -R mysql.mysql $datadir
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/passwd
#生成資料組態檔
cat > /etc/my.cnf << EOF
[mysqld]
basedir = ${mysql_install_dir}
datadir = $datadir
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
EOF
#配置服務啟動腳本
if [ ! -f /etc/init.d/mysqld ];then
cp -a ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
sed -ri '/^basedir=/c basedir=${mysql_install_dir}' /etc/init.d/mysqld
sed -ri "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
fi
chmod +x /etc/init.d/mysqld
#啟動mysql并設定開機自啟
service mysqld start
sleep 6
chkconfig --add mysqld
ln -s ${mysql_install_dir}/bin/mysql /usr/bin
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
echo "資料庫的密碼是: $passwd"
fi
done
[root@localhost mysql]# ./mysql_install.sh
請輸入要創建的實體個數: 1
請輸入資料存放目錄(默認路徑: /opt/xbz):
請輸入要為資料庫設定的密碼: lnh@321
請輸入安裝目錄,(默認路徑:/usr/local/mysql):
解壓軟體包
Starting MySQL.Logging to '/opt/xbz/localhost.err'.
SUCCESS!
mysql: [Warning] Using a password on the command line interface can be insecure.
資料庫的密碼是: lnh@321
[root@localhost mysql]# mysql -uroot -p'lnh@321'
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> exit
Bye
使用函式的單實體
[root@localhost mysql]# cat mysql_install.sh
#!/bin/bash
#設定執行權限
if [ $UID -ne 0 ];then
echo "請以管理員用戶進行執行"
exit
fi
function init(){
id mysql &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
else
echo "用戶已存在"
fi
#安裝依賴包
dnf -y install ncurses-compat-libs &> /dev/null
#解壓軟體包,修改目錄和所屬組
if [ ! -d $mysql_install_dir ];then
echo "解壓軟體包"
tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
else
echo 'mysql已安裝,不需要重復安裝'
exit
fi
chown -R mysql.mysql ${mysql_install_dir}
#設定環境變數
echo 'export PATH=${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
#做頭檔案
ln -s ${mysql_install_dir}/include /usr/include/mysql
#配置lib
echo '${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
#設定man檔案
grep '${mysql_install_dir}/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i "22a MANDATORY_MANPATH ${mysql_install_dir}/man" /etc/man_db.conf
fi
}
function init2(){
if [ -z $datadir ];then
datadir=/opt/xbz
fi
if [ ! -d $datadir ];then
mkdir -p $datadir
fi
chown -R mysql.mysql $datadir
}
function single(){
init
#建立資料存放目錄
for i in $(seq $count);do
if [ $count -eq 1 ];then
init2
#判斷是否已格式化
content=$(ls -l $datadir | grep -v total | wc -l)
if [ $content -eq 0 ];then
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/passwd
else
echo '不需要重復初始化'
exit
fi
#生成資料組態檔
cat > /etc/my.cnf << EOF
[mysqld]
basedir = ${mysql_install_dir}
datadir = $datadir
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
EOF
#配置服務啟動腳本
if [ ! -f /etc/init.d/mysqld ];then
cp -a ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
sed -ri '/^basedir=/c basedir=${mysql_install_dir}' /etc/init.d/mysqld
sed -ri "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
fi
chmod +x /etc/init.d/mysqld
#啟動mysql并設定開機自啟
service mysqld start
sleep 6
chkconfig --add mysqld
ln -s ${mysql_install_dir}/bin/mysql /usr/bin
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
mysql -uroot -p$password --connect-expired-password -e "set password = password('$passwd');"
echo "mysql安裝成功,資料庫的密碼是: $passwd"
fi
done
}
read -p "請輸入要創建的實體個數: " count
read -p "請輸入安裝目錄,(默認路徑:/usr/local/mysql):" mysql_install_dir
read -p "請輸入資料存放目錄(默認路徑: /opt/xbz): " datadir
read -p "請輸入要為資料庫設定的密碼(默認密碼123456): " passwd
#判斷安裝目錄合法性
echo $mysql_install_dir | grep -E '^/[a-z][a-z]*(/[a-z][a-z]*)*$' &> /dev/null
if [ $? -eq 0 ];then
if [ ! -d $mysql_install_dir ];then
mkdir -p $mysql_install_dir
fi
else
mysql_install_dir=/usr/local/mysql
fi
#判斷安裝目錄是否為空
if [ -z $mysql_install_dir ];then
mysql_install_dir=/usr/local/mysql
fi
#判斷資料存放目錄是否為空
if [ -z $datadir ];then
datadir=/opt/xbz
fi
#判斷資料存放目錄是否合法
echo $datadir | grep "^/[a-z][a-z]*\(/[a-z][a-z]*\)*$" &> /dev/null
if [ $? -ne 0 ];then
datadir=/opt/xbz
fi
#設定資料庫密碼格式
if [ -z $passwd ];then
passwd=123456
else
echo $passwd | grep -E '[a-z]+' | grep -E '[A-Z]+' | grep -E '[0-9]+' | grep -E '_+' &> /dev/null
if [ $? -ne 0 ] || [ ${#passwd} -lt 8 ];then
passwd=123456
fi
fi
#創建用戶
if [ $count -eq 1 ];then
single
else
echo "多實體"
fi
[root@localhost mysql]# ./mysql_install.sh
請輸入要創建的實體個數: 3
請輸入安裝目錄,(默認路徑:/usr/local/mysql):
請輸入資料存放目錄(默認路徑: /opt/xbz):
請輸入要為資料庫設定的密碼(默認密碼123456):
多實體
[root@localhost mysql]# ./mysql_install.sh
請輸入要創建的實體個數: 1
請輸入安裝目錄,(默認路徑:/usr/local/mysql):
請輸入資料存放目錄(默認路徑: /opt/xbz):
請輸入要為資料庫設定的密碼(默認密碼123456):
用戶已存在
解壓軟體包
Starting MySQL.Logging to '/opt/xbz/localhost.err'.
SUCCESS!
ln: failed to create symbolic link '/usr/bin/mysql': File exists
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql安裝成功,資料庫的密碼是: 123456
[root@localhost mysql]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost mysql]# mysql -uroot -p'123456'
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> exit
Bye
使用函式的單實體或者多實體
[root@localhost mysql]# cat mysql_install.sh
#!/bin/bash
#設定執行權限
if [ $UID -ne 0 ];then
echo "請以管理員用戶進行執行"
exit
fi
#函式(單實體和多實體相同的部分)
function init(){
id mysql &> /dev/null
if [ $? -ne 0 ];then
useradd -r -M -s /sbin/nologin mysql
else
echo "用戶已存在"
fi
#安裝依賴包
dnf -y install ncurses-compat-libs perl &> /dev/null
#解壓軟體包,修改目錄和所屬組
if [ ! -d $mysql_install_dir ];then
echo "解壓軟體包"
tar xf files/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
fi
chown -R mysql.mysql ${mysql_install_dir}
#設定環境變數
echo 'export PATH=${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
#做頭檔案
ln -s ${mysql_install_dir}/include /usr/include/mysql
#配置lib
echo '${mysql_install_dir}/lib' > /etc/ld.so.conf.d/mysql.conf
ldconfig
#設定man檔案
grep '${mysql_install_dir}/man' /etc/man_db.conf &> /dev/null
if [ $? -ne 0 ];then
sed -i "22a MANDATORY_MANPATH ${mysql_install_dir}/man" /etc/man_db.conf
fi
}
#函式(單實體和多實體相同的部分)
function init2(){
if [ -z $datadir ];then
datadir=/opt/xbz
fi
if [ ! -d $datadir ];then
mkdir -p $datadir
fi
chown -R mysql.mysql $datadir
}
#函式(單實體)
function single(){
init
#建立資料存放目錄
for i in $(seq $count);do
if [ $count -eq 1 ];then
init2
#判斷是否已格式化
content=$(ls -l $datadir | grep -v total | wc -l)
if [ $content -eq 0 ];then
${mysql_install_dir}/bin/mysqld --initialize --user mysql --datadir $datadir &> /tmp/passwd
else
echo '不需要重復初始化'
exit
fi
#生成資料組態檔
cat > /etc/my.cnf << EOF
[mysqld]
basedir = ${mysql_install_dir}
datadir = $datadir
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
EOF
#配置服務啟動腳本
if [ ! -f /etc/init.d/mysqld ];then
cp -a ${mysql_install_dir}/support-files/mysql.server /etc/init.d/mysqld
sed -ri '/^basedir=/c basedir=${mysql_install_dir}' /etc/init.d/mysqld
sed -ri "/^datadir=/c datadir=$datadir" /etc/init.d/mysqld
fi
chmod +x /etc/init.d/mysqld
#啟動mysql并設定開機自啟
service mysqld start
sleep 6
chkconfig --add mysqld
ln -s ${mysql_install_dir}/bin/mysql /usr/bin
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
mysql -uroot -p"$password" --connect-expired-password -e "set password = password('$passwd');"
echo "mysql安裝成功,資料庫的密碼是: $passwd"
fi
done
}
#函式(多實體)
function multi(){
init
init2
port=3306
#配置組態檔/etc/my.cnf
cat > /etc/my.cnf <<EOF
[mysqld_multi]
mysqld = ${mysql_install_dir}/bin/mysqld_safe
mysqladmin = ${mysql_install_dir}/bin/mysqladmin
EOF
#創建各實體資料存放的目錄并初始化各實體
for i in $(seq $count);do
mkdir -p ${datadir}/$port
chown -R mysql.mysql ${datadir}
content=$(ls -l ${datadir}/$port | grep -v total | wc -l)
if [ $content -eq 0 ];then
${mysql_install_dir}/bin/mysqld --initialize --user=mysql --datadir=${datadir}/$port &> /tmp/passwd
#取出臨時資料庫密碼
password=$(grep 'password' /tmp/passwd |awk '{print $NF}')
else
let port++
continue
fi
#配置組態檔/etc/my.cnf
cat >> /etc/my.cnf << EOF
[mysqld$port]
datadir = ${datadir}/$port
port = $port
socket = /tmp/mysql{$port}.sock
pid-file = ${datadir}/${port}/mysql_${port}.pid
log-error=/var/log/${port}.log
EOF
#啟動各實體并修改資料庫密碼
ln -s ${mysql_install_dir}/bin/my_print_defaults /usr/bin/
${mysql_install_dir}/bin/mysqld_multi start ${port}
sleep 6
${mysql_install_dir}/bin/mysql -uroot -p"$password" -h127.0.0.1 -P${port} --connect-expired-password -e "set password = password('$passwd');"
let port++
done
echo "mysql安裝成功,資料庫的密碼是: $passwd"
}
read -p "請輸入要創建的實體個數: " count
read -p "請輸入安裝目錄,(默認路徑:/usr/local/mysql):" mysql_install_dir
read -p "請輸入資料存放目錄(默認路徑: /opt/xbz): " datadir
read -p "請輸入要為資料庫設定的密碼(默認密碼123456): " passwd
#判斷安裝目錄合法性
echo $mysql_install_dir | grep -E '^/[a-z][a-z]*(/[a-z][a-z]*)*$' &> /dev/null
if [ $? -eq 0 ];then
if [ ! -d $mysql_install_dir ];then
mkdir -p $mysql_install_dir
fi
else
mysql_install_dir=/usr/local/mysql
fi
#判斷安裝目錄是否為空
if [ -z $mysql_install_dir ];then
mysql_install_dir=/usr/local/mysql
fi
#判斷資料存放目錄是否為空
if [ -z $datadir ];then
datadir=/opt/xbz
fi
#判斷資料存放目錄是否合法
echo $datadir | grep "^/[a-z][a-z]*\(/[a-z][a-z]*\)*$" &> /dev/null
if [ $? -ne 0 ];then
datadir=/opt/xbz
fi
#設定資料庫密碼格式
if [ -z $passwd ];then
passwd=123456
else
echo $passwd | grep -E '[a-z]+' | grep -E '[A-Z]+' | grep -E '[0-9]+' | grep -E '_+' &> /dev/null
if [ $? -ne 0 ] || [ ${#passwd} -lt 8 ];then
passwd=123456
fi
fi
#創建用戶
if [ $count -eq 1 ];then
single
ss -antl
else
multi
ss -antl
fi
測驗單實體:
[root@localhost mysql]# ./mysql_install.sh
請輸入要創建的實體個數: 1
請輸入安裝目錄,(默認路徑:/usr/local/mysql):
請輸入資料存放目錄(默認路徑: /opt/xbz):
請輸入要為資料庫設定的密碼(默認密碼123456):
解壓軟體包
Starting MySQL.Logging to '/opt/xbz/localhost.err'.
SUCCESS!
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql安裝成功,資料庫的密碼是: 123456
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost mysql]# mysql -uroot -p'123456'
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> exit
Bye
測驗多實體:
//洗掉剛剛的資料
[root@localhost mysql]# pkill mysqld
[root@localhost mysql]# rm -rf /usr/local/mysql/
[root@localhost mysql]# rm -rf /opt/xbz/
[root@localhost mysql]# rm -rf /etc/my.cnf
[root@localhost mysql]# ./mysql_install.sh
請輸入要創建的實體個數: 3
請輸入安裝目錄,(默認路徑:/usr/local/mysql):
請輸入資料存放目錄(默認路徑: /opt/xbz):
請輸入要為資料庫設定的密碼(默認密碼123456):
用戶已存在
解壓軟體包
....
mysql安裝成功,資料庫的密碼是: 123456
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 80 *:3307 *:*
LISTEN 0 80 *:3308 *:*
LISTEN 0 128 [::]:22 [::]:*
[root@localhost mysql]# mysql -uroot -p'123456' -h127.0.0.1 -P3306
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> exit
Bye
[root@localhost mysql]# mysql -uroot -p'123456' -h127.0.0.1 -P3307
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> exit
Bye
[root@localhost mysql]# mysql -uroot -p'123456' -h127.0.0.1 -P3308
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 3
Server version: 5.7.38 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> exit
Bye
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/509228.html
標籤:Linux
上一篇:環境變數
下一篇:個人超級計算機
