自动化安装mysql
目录结构
[root@20230703-instance mysql]# ll
总用量 20
-rw-r--r-- 1 mysql mysql 547 7月 14 13:22 config
-rwxr-x--- 1 mysql mysql 2625 7月 14 13:22 install.sh
-rw-r--r-- 1 mysql mysql 1777 7月 14 13:22 my5.7.cnf
drwxr-xr-x 2 mysql mysql 4096 7月 14 13:22 pkg
-rwxr-xr-x 1 mysql mysql 917 7月 14 13:22 uninstall.sh
[root@20230703-instance mysql]# ll pkg
总用量 218448
-rw-r--r-- 1 mysql mysql 24372 7月 14 13:22 libaio-0.3.109-12.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 12776 7月 14 13:22 libaio-devel-0.3.109-12.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 26597364 7月 14 13:22 mysql-community-client-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 317444 7月 14 13:22 mysql-community-common-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 2472844 7月 14 13:22 mysql-community-libs-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 181833876 7月 14 13:22 mysql-community-server-5.7.34-1.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 311020 7月 14 13:22 net-tools-2.0-0.17.20131004git.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 30840 7月 14 13:22 numactl-libs-2.0.12-5.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 8352488 7月 14 13:22 perl-5.16.3-286.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 19672 7月 14 13:22 perl-Carp-1.26-244.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 19244 7月 14 13:22 perl-constant-1.27-2.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 48296 7月 14 13:22 perl-Data-Dumper-2.145-3.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 1545440 7月 14 13:22 perl-Encode-2.51-7.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 29092 7月 14 13:22 perl-Exporter-5.68-3.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 27088 7月 14 13:22 perl-File-Path-2.09-2.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 57680 7月 14 13:22 perl-File-Temp-0.23.01-3.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 78236 7月 14 13:22 perl-Filter-1.49-3.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 57004 7月 14 13:22 perl-Getopt-Long-2.40-2.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 39292 7月 14 13:22 perl-HTTP-Tiny-0.033-3.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 703436 7月 14 13:22 perl-libs-5.16.3-286.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 43580 7月 14 13:22 perl-macros-5.16.3-286.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 12592 7月 14 13:22 perl-parent-0.225-244.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 84468 7月 14 13:22 perl-PathTools-3.40-5.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 51252 7月 14 13:22 perl-Pod-Escapes-1.04-286.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 114320 7月 14 13:22 perl-podlators-2.5.1-3.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 88756 7月 14 13:22 perl-Pod-Perldoc-3.20-4.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 221216 7月 14 13:22 perl-Pod-Simple-3.28-4.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 27436 7月 14 13:22 perl-Pod-Usage-1.63-3.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 36808 7月 14 13:22 perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 49692 7月 14 13:22 perl-Socket-2.010-3.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 78888 7月 14 13:22 perl-Storable-2.45-3.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 14056 7月 14 13:22 perl-Text-ParseWords-3.29-4.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 50392 7月 14 13:22 perl-threads-1.87-4.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 39868 7月 14 13:22 perl-threads-shared-1.43-6.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 52744 7月 14 13:22 perl-TimeDate-2.30-2.el7.noarch.rpm
-rw-r--r-- 1 mysql mysql 46304 7月 14 13:22 perl-Time-HiRes-1.9725-3.el7.x86_64.rpm
-rw-r--r-- 1 mysql mysql 24792 7月 14 13:22 perl-Time-Local-1.2300-2.el7.noarch.rpm
config
[root@20230703-instance mysql]# cat config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
#SELINUXTYPE=targeted
my5.7.cnf
[root@20230703-instance mysql]# cat my5.7.cnf
[client]
port = 3306
#socket =/tmp/mysql.sock
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
default-character_set=utf8mb4
[mysqld]
safe-user-create=1
old_passwords=0
secure-auth=1
allow-suspicious-udfs=0
max_allowed_packet = 67108864
sql_mode=NO_AUTO_CREATE_USER
local_infile=0
auto-increment-offset = 1
auto-increment-increment = 2
binlog-ignore-db = performance_schema
binlog-ignore-db = information_schema
binlog-ignore-db = mysql
binlog-do-db = dataassetmnt
character-set-server=utf8mb4
read-only = 0
#binlog_format = mixed
#log-bin = mysql-bin
server-id=1
explicit_defaults_for_timestamp=true
#TODO
replicate-ignore-table = replicate-do-db_place.t_login_op_log
replicate-ignore-table = replicate-do-db_place.t_login_network_card
replicate-ignore-table = replicate-do-db_place.t_login_network_card_usage
replicate-ignore-table = replicate-do-db_place.t_login_network_config
replicate-ignore-table = replicate-do-db_place.t_login_network_config_network_card
port = 3306
# only replicate this db
#replicate-do-db=scmp_x
replicate-do-db=replicate-do-db_place
gtid-mode = ON
enforce_gtid_consistency=on
skip_slave_start=0
auto_increment_increment = 1
auto_increment_offset = 1
expire_logs_days = 7
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[mysqldump]
quick
max_allowed_packet = 32M
[mysql]
default-character-set=utf8mb4
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
#safe-updates
[root@20230703-instance mysql]#
install.sh
[root@20230703-instance mysql]# cat install.sh
#!/bin/bash
###mysql数据库的安装配置
#重复安装时需要先停止服务
systemctl stop mysqld
/etc/init.d/mysql stop
killall -9 mysql
killall -9 mysqld_safe
killall -9 mysqld
#删除已安装的文件
if [ -d /etc/mysql ]; then
rm -r -f /etc/mysql
fi
if [ -d /usr/local/mysql ]; then
rm -r -f /usr/local/mysql
rm -r -f /opt/mysql/server-5.5
rm -r -f /opt/mysql/server-5.6
rm -r -f /opt/mysql/server-5.7
fi
if [ -d /var/lib/mysql ]; then
rm -rf /var/lib/mysql
fi
if [ -f /etc/init.d/mysql ]; then
rm -f /etc/init.d/mysql
fi
if [ -f /etc/init.d/mysqld ]; then
rm -f /etc/init.d/mysqld
fi
if [ -L /usr/local/mysql ]; then
rm -f /usr/local/mysql
fi
if [ -f /root/.mysql_secret ]; then
rm -f /root/.mysql_secret
fi
rm -rf /var/log/mysql*
#卸载自带的mysql
rpm -e --nodeps $(rpm -qa |grep mariadb)
rpm -e --nodeps $(rpm -qa |grep MySQL)
rpm -e --nodeps $(rpm -qa |grep mysql)
#创建mysql用户
sh -c 'groupadd mysql'
sh -c 'useradd -r -g mysql mysql'
#安装mysql程序
pushd pkg
rpm -ivh net-tools-2.0-0.17.20131004git.el7.x86_64.rpm
rpm -ivh libaio*.rpm
rpm -ivh perl*.rpm
rpm -ivh perl-Data-Dumper-2.145-3.el7.x86_64.rpm
rpm -ivh numactl-libs-2.0.12-5.el7.x86_64.rpm
rpm -ivh mysql-*.rpm
popd
#my.cnf 默认配置文件
cp -f my5.7.cnf /etc/my.cnf
chmod 0444 /etc/my.cnf
rm /var/lib/mysql/auto.conf
#启动mysql
mysqld --initialize --user=mysql --console
systemctl restart mysqld
#设置root用户密码
init_password=`sudo grep 'temporary password' /var/log/mysqld.log | awk '{print $NF}'`
mysql -uroot -p$init_password --connect-expired-password -e "alter user 'root'@'localhost' identified by 'Release@2023@DataSafeYear'";
#
username=root
password=Release@2023@DataSafeYear
mysql -u$username -p$password -e "drop database if exists test";
mysql -u$username -p$password -e "set global local_infile='OFF'";
mysql -u$username -p$password -e "source /usr/setup/smp/sql/dataassetmnt.sql"
mysql -u$username -p$password -e "source /usr/setup/smp/sql/t_assets_vuln.sql"
mysql -u$username -p$password -e "source /usr/setup/smp/sql/t_assets_cnnvd.sql"
mysql -u$username -p$password -e "create user smp@'localhost' identified by 'Release@2023@DataSafeYear'"
mysql -u$username -p$password -e "grant all privileges on dataassetmnt.* to smp@'localhost' identified by 'Release@2023@DataSafeYear'"
mysql -u$username -p$password -e "grant select on mysql.* to smp@'localhost' identified by 'Release@2023@DataSafeYear'"
mysql -u$username -p$password -e "delete from mysql.user where user = 'root'"
mysql -u$username -p$password -e "flush privileges"
systemctl restart mysqld
uninstall.sh
[root@20230703-instance mysql]# cat uninstall.sh
#!/bin/bash
###mysql数据库的安装配置
#重复安装时需要先停止服务
systemctl stop mysqld
/etc/init.d/mysql stop
killall -9 mysql
killall -9 mysqld_safe
killall -9 mysqld
#删除已安装的文件
if [ -d /etc/mysql ]; then
rm -r -f /etc/mysql
fi
if [ -d /usr/local/mysql ]; then
rm -r -f /usr/local/mysql
rm -r -f /opt/mysql/server-5.5
rm -r -f /opt/mysql/server-5.6
rm -r -f /opt/mysql/server-5.7
fi
if [ -d /var/lib/mysql ]; then
rm -rf /var/lib/mysql
fi
if [ -f /etc/init.d/mysql ]; then
rm -f /etc/init.d/mysql
fi
if [ -f /etc/init.d/mysqld ]; then
rm -f /etc/init.d/mysqld
fi
if [ -L /usr/local/mysql ]; then
rm -f /usr/local/mysql
fi
if [ -f /root/.mysql_secret ]; then
rm -f /root/.mysql_secret
fi
rm -rf /var/log/mysql*
#卸载自带的mysql
rpm -e --nodeps $(rpm -qa |grep mariadb)
rpm -e --nodeps $(rpm -qa |grep MySQL)
rpm -e --nodeps $(rpm -qa |grep mysql)
[root@20230703-instance mysql]#