#!/bin/bash
#mysql部署
useradd -M -s /sbin/nologin mysql
cd /home/data/install-page
mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.33-linux-glibc2.12-x86_64/ 、/home/data/mysql/
#yum -y install libaio
mkdir /home/data/mysql/mysqldata
mkdir /home/data/mysql/logs
touch /home/data/mysql/logs/error.log
mkdir /home/data/mysql/run
cp /etc/my.cnf /etc/my.cnf.bak
chown -R mysql:root /etc/my.cnf
chmod 644 /etc/my.cnf
#创建mysql配置文件
echo '[mysqld]
datadir=/data/mysql/mysqldata
port=3306
log-error=/data/mysql/logs/error.log
long_query_time=2
symbolic-links=0
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
default-time_zone = '+8:00'
character-set-server=utf8
max_connections = 500
sort_buffer_size = 16M
query_cache_limit = 8M
query_cache_size = 64M
open_files_limit = 1024
innodb_buffer_pool_size = 128M
innodb_buffer_pool_instances = 4
innodb_buffer_pool_load_at_startup = 1
innodb_flush_log_at_trx_commit = 2
innodb_file_per_table = ON
innodb_log_buffer_size = 8M
innodb_log_file_size = 256M
innodb_flush_method=O_DIRECT
skip_name_resolve
max_allowed_packet = 500M
wait_timeout = 600
interactive_timeout = 300
[client]
default-character-set=utf8' > /etc/my.cnf
chown -R mysql:root /home/data/mysql
cd /home/data/mysql/
./bin/mysqld --initialize --user=mysql --basedir=/home/data/mysql --datadir=/home/data/mysql/mysqldata
touch /usr/lib/systemd/system/mysql.service
echo '[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=/data/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
#Restart=on-failure
#RestartPreventExitStatus=1
#PrivateTmp=false' > /usr/lib/systemd/system/mysql.service
chmod 755 /usr/lib/systemd/system/mysql.service
systemctl daemon-reload
echo '
#MYSQL
export ES_HOME=/data/mysql
export PATH=$ES_HOME/bin:$PATH' >> /etc/profile
source /etc/profile
systemctl restart mysql
systemctl enable mysql
systemctl status mysql
MY_SQL=/data/mysql/bin/mysql
MY_USER=root
MY_PASS=`cat /data/mysql/logs/error.log |grep root@localhost: | awk -F' ' '{print $NF}'`
echo $MY_PASS
$MY_SQL -u$MY_USER -p$MY_PASS << EOF
alter user 'root'@'localhost' identified by 'J2021an06gan!';
grant all privileges on *.* to 'root'@'%' identified by 'J2021an06gan!' with grant option;
flush privileges;
EOF