1. shell脚本
需要用source install_mysql_8026.sh
点击查看代码
#!/bin/bash
Dir=/usr/local/src
function AddGroup(){
group=mysql
egrep "^$group" /etc/group
if [ $? -ne 0 ]
then
groupadd -r mysql
fi
}
function AddUser(){
user=mysql
egrep "^${user}" /etc/passwd
if [ $? -ne 0 ]
then
useradd -r -g mysql -s /bin/false mysql
fi
}
function Unpackage(){
cd ${Dir} && tar xf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz \
&& mv mysql-8.0.26-linux-glibc2.12-x86_64 mysql8026 \
&& rm -fr ${Dir}/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz \
&& ln -s /usr/local/src/mysql8026 /usr/local/src/mysql
}
function ChangeEnvironment(){
echo 'export PATH=/usr/local/src/mysql/bin:$PATH' >> /etc/profile
source /etc/profile
mysql -V
if [ $? -eq 0 ]
then
echo "安装成功"
else
echo "安装失败" && exit 1
fi
}
function MakeDir(){
mkdir /data/mysql/data -p
chown -R mysql:mysql /data/*
}
function EditFile(){
cat >>/etc/my.cnf<< EOF
[mysqld]
user=mysql
basedir=/usr/local/src/mysql8026
datadir=/data/mysql/data
socket=/tmp/mysql.sock
server_id=20
port=3306
[mysql]
socket=/tmp/mysql.sock
EOF
}
function InitMysql(){
apt install libaio1 -y
mysqld --initialize-insecure
}
function EditService(){
cat >/lib/systemd/system/mysqld.service<<EOF
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/src/mysql8026/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE=5000
EOF
}
function StartMysql(){
systemctl start mysqld.service && systemctl enable mysqld.service && systemctl status mysqld.service
}
main(){
AddGroup
AddUser
Unpackage
ChangeEnvironment
MakeDir
EditFile
InitMysql
EditService
StartMysql
}
main
2. py脚本
点击查看代码
#!/bin/python
import os
from pathlib import Path
import grp
import pwd
import tarfile
import sys
def AddGroup():
group_name = "mysql"
all_groups_list = grp.getgrall()
list1 = []
for i in all_groups_list:
list1.append(i[0])
if group_name in list1:
print("{} 组已经存在".format(group_name))
sys.exit()
else:
print("{}组不存在,即将创建".format(group_name))
os.system("groupadd -r {}".format(group_name))
def AddUser():
user="mysql"
list1=[]
username_list=pwd.getpwall()
for i in username_list:
list1.append(i[0])
if user in list1:
print("{}用户已经存在,请使用别的用户".format(user))
sys.exit()
else:
print("{}用户不存在,即将创建".format(user))
os.system("useradd -r -g mysql -s /bin/false mysql")
def Unpackage():
p = Path("/usr/local/src/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz")
with tarfile.open(p, "r", encoding="utf-8") as f:
f.extractall(p.parent)
os.remove(p)
p=Path("/usr/local/src/mysql-8.0.26-linux-glibc2.12-x86_64")
p.rename("/usr/local/src/mysql8026")
def SetEnv():
p = Path("/etc/profile")
with open(p,mode="a",encoding="utf-8") as f:
f.write("export PATH=/usr/local/src/mysql8026/bin:$PATH" + "\n")
os.system('. /etc/profile')
def MakeDir():
Path("/data/mysql/data").mkdir(parents=True, exist_ok=True)
os.system("chown -R mysql:mysql /data/*")
def EditFile():
filename = Path("/etc/my.cnf")
with open(filename,"w",encoding="utf-8") as f:
f.write("[mysqld]\n"
"user=mysql\n"
"basedir=/usr/local/src/mysql8026\n"
"datadir=/data/mysql/data\n"
"socket=/tmp/mysql.sock\n"
"server_id=20\n"
"port=3306\n"
"[mysql]\n"
"socket=/tmp/mysql.sock\n"
)
def InitMysql():
os.system("apt install libaio1 -y")
os.system("/usr/local/src/mysql8026/bin/mysqld --initialize-insecure")
def EditService():
filename=Path("/lib/systemd/system/mysqld.service")
with open(filename,"w",encoding="utf-8") as f:
f.write("[Unit]\n"
"After=network.target\n"
"After=syslog.target\n"
"[Install]\n"
"WantedBy=multi-user.target\n"
"[Service]\n"
"User=mysql\n"
"Group=mysql\n"
"ExecStart=/usr/local/src/mysql8026/bin/mysqld --defaults-file=/etc/my.cnf\n"
"LimitNOFILE=5000\n"
)
def StartMysql():
os.system("systemctl start mysqld.service && systemctl enable mysqld.service && systemctl status mysqld.service")
if __name__ == '__main__':
AddGroup()
AddUser()
Unpackage()
SetEnv()
MakeDir()
EditFile()
InitMysql()
EditService()
StartMysql()