1.下载mysql二进制安装包和依赖包
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-devel-0.3.109-13.el7.x86_64.rpm
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/libaio-0.3.109-13.el7.x86_64.rpm
wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
2.安装依赖包并解压mysql二进制安装包
#yum -y install numactl ##error while loading shared libraries: libnuma.so.1
#yum -y install openssl ##[ERROR] Could not find OpenSSL on the system
rpm -ivh libaio-0.3.109-13.el7.x86_64.rpm
rpm -ivh libaio-devel-0.3.109-13.el7.x86_64.rpm
tar -xzvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mv ./mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysqlrun
3.创建mysql运行用户和用户组
groupadd mysql
useradd -M -g mysql -s /sbin/nologin mysql
4.创建数据目录和日志目录并授予权限
mkdir -p /usr/local/mysqldata/data
mkdir -p /usr/local/mysqldata/binlogs
mkdir -p /usr/local/mysqldata/relaylogs
touch /usr/local/mysqldata/mysql-error.log
chown -R mysql:mysql /usr/local/mysqldata
chmod -R 770 /usr/local/mysqldata
chown -R mysql:mysql /usr/local/mysqlrun
chmod -R 770 /usr/local/mysqlrun
5.创建并配置my.cnf
mv /etc/my.cnf /etc/my.cnf_bak`date "+%Y%m%d%H%M"`
cat << EOF > /etc/my.cnf
[mysqld]
user=mysql
port=3306
#server-id=1
#binlog_format = ROW
#max_binlog_size = 1G
#expire_logs_days = 7
#log-bin=/usr/local/mysqldata/binlogs/mysql-bin
#relay-log=/usr/local/mysqldata/relaylogs/slave-relay-bin
skip-grant-tables
log-error=/usr/local/mysqldata/mysql-error.log
datadir=/usr/local/mysqldata/data
basedir=/usr/local/mysqlrun
socket=/usr/local/mysqlrun/mysql.sock
pid-file=/usr/local/mysqlrun/mysql.pid
skip-name-resolve
symbolic-links = 0
lower_case_table_names = 1
character-set-server = utf8
default-storage-engine = InnoDB
innodb_file_per_table = 1
max_connections = 2000
max_allowed_packet = 1G
interactive_timeout = 120
wait_timeout = 864000
[mysql]
socket=/usr/local/mysqlrun/mysql.sock
EOF
6.初始化mysql数据库
/usr/local/mysqlrun/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysqlrun --datadir=/usr/local/mysqldata/data
/usr/local/mysqlrun/bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysqldata/data
7.设定启动脚本中basedir和datadir的路径
sed -i '/^basedir/c\basedir=\/usr\/local\/mysqlrun' /usr/local/mysqlrun/support-files/mysql.server
sed -i '/^datadir/c\datadir=\/usr\/local\/mysqldata\/data' /usr/local/mysqlrun/support-files/mysql.server
8.将mysql工具添加到环境变量中
echo 'export PATH=/usr/local/mysqlrun/bin:$PATH' >> /etc/profile
source /etc/profile
9.设置mysql服务开机自启并启动
ln -s /usr/local/mysqlrun/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list
systemctl enable mysqld
systemctl restart mysqld && systemctl status mysqld
#/usr/local/mysqlrun/bin/mysqld_safe --defaults-file=/etc/my.cnf &
#ps -ef | grep mysql | grep -v grep | awk '{print $2}' |xargs kill -9
10.重置mysql数据库root密码(无需重启)
ROOT_PASSWD='m4r!adbOP'
ROOT_PASSWD_T='m4r\!adbOP'
mysql -uroot -e "update mysql.user set authentication_string=password('${ROOT_PASSWD_T}') where user='root';"
mysql -uroot -e "flush privileges;"
sed -i 's/^skip-grant-tables/#&/' /etc/my.cnf
mysql --connect-expired-password -uroot -p"${ROOT_PASSWD}" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${ROOT_PASSWD_T}';"
mysql --connect-expired-password -uroot -p"${ROOT_PASSWD}" -e "flush privileges;"
mysql -uroot -p"${ROOT_PASSWD}" -e "show databases;"
11.设置mysql远程访问
mysql -uroot -p"${ROOT_PASSWD}" -e "update mysql.user set host='%' where user='root' and host='localhost';"
mysql -uroot -p"${ROOT_PASSWD}" -e "select user,host from mysql.user;"
12.放开防火墙(防火墙关闭也无需操作)
firewall-cmd --permanent --zone=public --add-port=3306/tcp
firewall-cmd --reload