二进制安装MySQL5.7
环境准备
软件包:mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz
1. 安装相关包
[root@centos7 ~]#yum -y install libaio numactl-libs
2. 创建用户和组
[root@centos7 ~]#groupadd mysql
[root@centos7 ~]#useradd -r -g mysql -s /bin/false mysql
3. 准备程序文件
[root@centos7 ~]#tar xfv mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@centos7 ~]#cd /usr/local/
[root@centos7 local]#ln -s mysql-5.7.30-linux-glibc2.12-x86_64 mysql
[root@centos7 local]#chown -R mysql.mysql /usr/local/mysql/
[root@centos7 local]#chmod -R 755 /usr/local/mysql/
4. 准备环境变量
[root@centos7 local]#echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 local]#. /etc/profile.d/mysql.sh
5. 创建数据库目录,修改权限
[root@centos7 local]#mkdir /data/mysql -pv
mkdir: created directory ‘/data/mysql’
[root@centos7 local]#chown -R mysql:mysql /data/mysql/
6. 准备配置文件
[root@centos7 local]#cd /usr/local/mysql/
[root@centos7 mysql]#cp /etc/my.cnf{,.bak}
[root@centos7 mysql]#vim /etc/my.cnf
[root@centos7 mysql]#cat /etc/my.cnf
[mysqld]
#datadir=/var/lib/mysql
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
explicit_defaults_for_timestamp=true
port=3306
character_set_server=utf8
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
log-error=/data/mysql/mysql.log
pid-file=/var/run/mariadb/mariadb.pid
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
7. 创建配置文件中指定的路径并修改权限
[root@centos7 mysql]#mkdir /var/lib/mysql
[root@centos7 mysql]#chown -R mysql:mysql /var/lib/mysql/
[root@centos7 mysql]#touch /var/lib/mysql/mysql.sock
[root@centos7 mysql]#ll /var/lib/mysql
total 0
-rw-r--r-- 1 root root 0 Jul 18 23:42 mysql.sock
[root@centos7 mysql]#chmod guo+wr /var/lib/mysql/mysql.sock
[root@centos7 mysql]#ll /var/lib/mysql
total 0
-rw-rw-rw- 1 root root 0 Jul 18 23:42 mysql.sock
[root@centos7 mysql]#touch /data/mysql/mysql.log
[root@centos7 mysql]#ll /data/mysql/
total 0
-rw-r--r-- 1 root root 0 Jul 18 23:43 mysql.log
[root@centos7 mysql]#chmod guo+rw /data/mysql/mysql.log
[root@centos7 mysql]#ll /data/mysql/mysql.log
-rw-rw-rw- 1 root root 0 Jul 18 23:43 /data/mysql/mysql.log
[root@centos7 mysql]#mkdir /var/run/mariadb
[root@centos7 mysql]#chown -R mysql:mysql /var/run/mariadb/
[root@centos7 mysql]#touch /var/run/mariadb/mariadb.pid
[root@centos7 mysql]#ll /var/run/mariadb/
total 0
-rw-r--r-- 1 root root 0 Jul 18 23:44 mariadb.pid
[root@centos7 mysql]#chmod guo+rw /var/run/mariadb/mariadb.pid
[root@centos7 mysql]#ll /var/run/mariadb/
total 0
-rw-rw-rw- 1 root root 0 Jul 18 23:44 mariadb.pid
8. 初始化数据库文件并生成 root 空密码
[root@centos7 mysql]#rm -rf /data/mysql/*
[root@centos7 mysql]#./bin/mysqld --initialize-insecure --datadir=/data/mysql/ --user=mysql
9 . 启动mysqld守护程序
[root@centos7 mysql]#./bin/mysqld_safe --user=mysql --datadir=/data/mysql &
10. 准备启动脚本,并启动服务
[root@centos7 mysql]#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql]#chkconfig --add mysqld
[root@centos7 mysql]#systemctl start mysqld.service
[root@centos7 mysql]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
LISTEN 0 80 [::]:3306 [::]:*
11. 数据库的登录、查询,修改密码,Ctrl+D,退出数据库
[root@centos7 mysql]#./bin/mysql -uroot -p
Enter password: #root密码为空,直接回车
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
mysql> use mysql;
mysql> SELECT VERSION();
+-----------+
| VERSION() |
+-----------+
| 5.7.30 |
+-----------+
mysql> alter user root@'localhost' identified by 'MySQL@2022.';
mysql> GRANT ALL PRIVILEGES ON *.* TO root@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> exit
12. 登录测试
[root@centos7 mysql]#mysql -uroot -pMySQL@2022.
13. MySQL安全加固
在MySQL安装完成后,运行mysql_secure_installation命令,提高安全性
[root@centos7 mysql]#mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #输入root用户的当前密码
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n #已经设置好密码了,不用改,回答“n”
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y #删除匿名用户,回答“y”
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y #删除test数据库,回答“y”
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y #重新加载特权表,回答“y”
Success.
All done!