#!/bin/bash
#安装MySQL程序
install_mysql(){
#mv /etc/yum.repos.d/* /tmp/
#curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
#yum install -y nfs-utils net-tools vim iotop telnet sysstat
mount -t nfs 10.20.30.200:/DATA /mnt
tar -xvf /mnt/software/mysql-8.0.20-linux-glibc2.12-x86_64.tar.xz -C /usr/local/
ln -s /usr/local/mysql-8.0.20-linux-glibc2.12-x86_64 /usr/local/mysql
yum remove -y mariadb-libs
useradd mysql -M -s /sbin/nologin
mkdir -p /data/mysql/data
chown -R mysql.mysql /data/mysql/data
cp /mnt/software/my_8.0.x.cnf /etc/my.cnf
cat >>/etc/profile <<EOF
export PATH=/usr/local/mysql/bin:$PATH
EOF
source /etc/profile
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize-insecure
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
/etc/init.d/mysqld start
chkconfig --add mysqld
}
#设置server_id
#SELECT inet_aton('192.168.0.2') as ip10进制;
#SELECT inet_ntoa(@@server_id) as 真实ip;
set_server_id(){
ip_addr=`ifconfig |grep 'broadcast' | awk '{print $2}'`
a=`echo ${ip_addr} | cut -d '.' -f1`
b=`echo ${ip_addr} | cut -d '.' -f2`
c=`echo ${ip_addr} | cut -d '.' -f3`
d=`echo ${ip_addr} | cut -d '.' -f4`
ip_addr_num=`expr $a \* 256 \* 256 \* 256 + $b \* 256 \* 256 + $c \* 256 + $d`
sed -i "s/server_id=1/server_id=$ip_addr_num/g" /etc/my.cnf
/usr/local/mysql/bin/mysql -e "set global server_id=$ip_addr_num;"
}
#重置root密码、设置开发用户、设置备份
set_password(){
new_password=`openssl rand -base64 12`
dev_password=`openssl rand -base64 12`
install_date=`date +"%Y-%m-%d %H:%M:%S"`
ip=`/sbin/ip addr|grep '10\.'|awk -F'[ /]+' '{print $3}'`
/usr/local/mysql/bin/mysql -e "alter user root@'localhost' identified with mysql_native_password by '$new_password';"
sed -i "s/password=/password=$new_password/g" /etc/my.cnf
/usr/local/mysql/bin/mysql -e "create user dev_root@'%' identified by '$dev_password';"
/usr/local/mysql/bin/mysql -e "GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, SHOW DATABASES, CREATE TEMPORARY TABLES, CREATE VIEW,SHOW VIEW ON *.* TO dev_root@'%';"
#/usr/local/mysql/bin/mysql -e "create user repl@'%' identified by 'etocrm@2022';"
#/usr/local/mysql/bin/mysql -e "grant replication slave on *.* to repl@'%';"
#安装xtrabackup及备份脚本
cd /mnt/software && yum localinstall -y percona-xtrabackup-80-8.0.30-23.1.el7.x86_64.rpm percona-toolkit-3.4.0-3.el7.x86_64.rpm
cp /mnt/software/wux_nas_xtrabackup.sh /root/
sed -i "s/PWD=/PWD=$new_password/g" /root/wux_nas_xtrabackup.sh
echo "10 00 * * * /bin/sh /root/wux_nas_xtrabackup.sh > /dev/null" >>/var/spool/cron/root
#echo "#########" >> /mnt/checklist/mysql/mysql_install_info.txt
echo "install date:$install_date" >> /mnt/checklist/mysql/mysql_install_info.txt
echo "addr:$ip" >> /mnt/checklist/mysql/mysql_install_info.txt
echo "user:root password:$new_password" >> /mnt/checklist/mysql/mysql_install_info.txt
echo "user:dev_root password:$dev_password" >> /mnt/checklist/mysql/mysql_install_info.txt
echo "#########" >> /mnt/checklist/mysql/mysql_install_info.txt
}
#设置邮件
set_mail(){
yum install -y mailx #sendmail
cat >/etc/mail.rc <<EOF
set from=alerts@etocrm.com
set smtp=smtp.exmail.qq.com
set smtp-auth-user=alerts@etocrm.com
set smtp-auth-password=123456
set smtp-auth=login
EOF
mail -s "my.cnf" peng.wang@etocrm.com < /etc/my.cnf
}
#添加监控
set_monitor(){
cp /mnt/software/mysql_check.sh /tmp/
echo "*/1 * * * * /bin/sh /tmp/mysql_check.sh > /tmp/mysql_check.info" >>/var/spool/cron/root
umount /mnt
}
install_mysql
set_server_id
set_password
set_mail
set_monitor
标签:脚本,info,参考,MySQL,mnt,etc,install,mysql,txt
From: https://www.cnblogs.com/enzo-ocean/p/18180269