CentOS7 x86_64 环境 MySQL 5.7 安装教程


文章目录


0. 卸载系统自带 MySQL 相关依赖

[root@hadoop102 ~]# rpm -qa|grep mysql
[root@hadoop102 ~]#
[root@hadoop102 ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64
[root@hadoop102 ~]#
[root@hadoop102 ~]# yum remove mariadb-libs-5.5.56-2.el7.x86_64 -y
[root@hadoop102 ~]#
[root@hadoop102 ~]# rpm -qa|grep mariadb*
[root@hadoop102 ~]#

1. 下载 MySQL 安装包

(以mysql-5.7.35-el7-x86_64.tar.gz为例)

[root@hadoop102 ~]# mkdir /datafs/{software,module}
[root@hadoop102 ~]# cd software
[root@hadoop102 software]# wget http://decs.pcl.ac.cn:5189/mirrors/x86_64/db/MySQL/linux-version/x86/mysql-5.7.35-el7-x86_64.tar.gz
--2022-03-04 11:11:52-- http://decs.pcl.ac.cn:5189/mirrors/x86_64/db/MySQL/linux-version/x86/mysql-5.7.35-el7-x86_64.tar.gz
正在解析主机 decs.pcl.ac.cn (decs.pcl.ac.cn)... 210.22.22.150
正在连接 decs.pcl.ac.cn (decs.pcl.ac.cn)|210.22.22.150|:5189... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:732374542 (698M) [application/octet-stream]
正在保存至: “mysql-5.7.35-el7-x86_64.tar.gz”

100%[===============================================================================================================================================>] 732,374,542 4.39MB/s 用时 3m 48s

2022-03-04 11:15:40 (3.06 MB/s) - 已保存 “mysql-5.7.35-el7-x86_64.tar.gz” [732374542/732374542])

[root@hadoop102 software]#


也可以去官方网站下载


2. 解压 MySQL 安装包

[root@hadoop102 software]# tar -zxf mysql-5.7.35-el7-x86_64.tar.gz -C /datafs/module/
[root@hadoop102 module]# mv mysql-5.7.35-el7-x86_64 mysql-5.7.35

3. 配置 mysql 用户

[root@hadoop102 module]# groupadd mysql
[root@hadoop102 module]# useradd -r -g mysql mysql
[root@hadoop102 module]# chown -R mysql. mysql-5.7.35
[root@hadoop102 module]# cd mysql-5.7.35

4. 配置系统环境变量

[root@hadoop102 mysql-5.7.35]# echo "export MYSQL_HOME=/datafs/module/mysql-5.7.35" >> /etc/profile.d/my_env.sh
[root@hadoop102 mysql-5.7.35]# echo "export PATH=$PATH:$MYSQL_HOME/bin" >> /etc/profile.d/my_env.sh
[root@hadoop102 mysql-5.7.35]# cat /etc/profile.d/my_env.sh
export MYSQL_HOME=/datafs/module/mysql-5.7.35
export PATH=$PATH:$MYSQL_HOME/bin
[root@hadoop102 mysql-5.7.35]# source /etc/profile.d/my_env.sh

5. 配置数据存储目录

[root@hadoop102 mysql-5.7.35]# mkdir {data,logs};chown -R mysql. ./*
[root@hadoop102 mysql-5.7.35]#
[root@hadoop102 mysql-5.7.35]# cd data/

6. 配置 my.cnf 文件

[root@hadoop102 data]# pwd
/datafs/module/mysql-5.7.35/data
[root@hadoop102 data]# vim /etc/my.cnf
[root@hadoop102 data]#
[root@hadoop102 data]# cat /etc/my.cnf
[client]
port = 3306
default-character-set = utf8mb4

[mysqld]
port = 3306
basedir = /datafs/module/mysql-5.7.35
datadir = /datafs/module/mysql-5.7.35/data
pid-file = /datafs/module/mysql-5.7.35/data/mysql.pid
socket = /tmp/mysql.sock

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

max_connections=2000

default_storage_engine=INNODB
lower_case_table_names=1

user = mysql

# skip-grant-tables

server-id = 1
character-set-server = utf8mb4
log_error = /datafs/module/mysql-5.7.35/logs/mysql-error.log
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /datafs/module/mysql-5.7.35/logs/mysql-slow.log
performance_schema = 0
explicit_defaults_for_timestamp

# 加上下面这一行,保证日志跟系统时间保持一致
log_timestamps=SYSTEM

[mysqld_safe]
general_log = 1
log-output = 'FILE,TABLE'
gerneral_log_file = /datafs/module/mysql-5.7.35/logs/mysqld.log
slow_query_log = 1
slow_query_log_file = /datafs/module/mysql-5.7.35/logs/mysql-slow.log
long_query_time = 5
log-error = /datafs/module/mysql-5.7.35/logs/mysql-error.log

[mysqldump]
quick
default-character-set = utf8mb4
max_allowed_packet = 256M
[root@hadoop102 data]#

7. 初始化数据库

[root@hadoop102 data]# cd ..
[root@hadoop102 mysql-5.7.35]# bin/mysqld --initialize --user=mysql --basedir=/datafs/module/mysql-5.7.35 --datadir=/datafs/module/mysql-5.7.35/data

8. 查看生成的密码

[root@hadoop102 mysql-5.7.35]# grep "password" /datafs/module/mysql-5.7.35/logs/mysql-error.log
2022-03-07T16:12:59.040388+08:00 1 [Note] A temporary password is generated for root@localhost: YqfwlliC&5<p
[root@hadoop102 mysql-5.7.35]#

9. 启动 MySQL 数据库

[root@hadoop102 mysql-5.7.35]# /datafs/module/mysql-5.7.35/support-files/mysql.server start
Starting MySQL.. SUCCESS!
[root@hadoop102 mysql-5.7.35]#

10. 配置开机自启动 MySQL 数据库

[root@hadoop102 mysql-5.7.35]# cp -a /datafs/module/mysql-5.7.35/support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@hadoop102 mysql-5.7.35]# chkconfig --add mysqld
[root@hadoop102 mysql-5.7.35]# chkconfig --list mysqld

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。

要列出 systemd 服务,请执行 'systemctl list-unit-files'
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'

mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
[root@hadoop102 mysql-5.7.35]#

11. 启停 MySQL 数据库

[root@hadoop102 mysql-5.7.35]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@hadoop102 mysql-5.7.35]#

[root@hadoop102 mysql-5.7.35]# service mysqld status
SUCCESS! MySQL running (34030)
[root@hadoop102 mysql-5.7.35]#

[root@hadoop102 mysql-5.7.35]# service mysqld stop
Shutting down MySQL.
. SUCCESS!
[root@hadoop102 mysql-5.7.35]#

[root@hadoop102 mysql-5.7.35]# service mysqld status
ERROR! MySQL is not running
[root@hadoop102 mysql-5.7.35]#

[root@hadoop102 mysql-5.7.35]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: inactive (dead)
Docs: man:systemd-sysv-generator(8)
[root@hadoop102 mysql-5.7.35]#

[root@hadoop102 mysql-5.7.35]# systemctl start mysqld
[root@hadoop102 mysql-5.7.35]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since 一 2022-03-07 16:17:29 CST; 1s ago
Docs: man:systemd-sysv-generator(8)
Process: 35050 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
CGroup: /system.slice/mysqld.service
├─35069 /bin/sh /datafs/module/mysql-5.7.35/bin/mysqld_safe --datadir=/datafs/module/mysql-5.7.35/data --pid-file=/datafs/module/mysql-5.7.35/data/mysql.pid
└─35476 /datafs/module/mysql-5.7.35/bin/mysqld --basedir=/datafs/module/mysql-5.7.35 --datadir=/datafs/module/mysql-5.7.35/data --plugin-dir=/datafs/module/mysql-5.7.35/li...

307 16:17:27 hadoop102 systemd[1]: Starting LSB: start and stop MySQL...
307 16:17:29 hadoop102 mysqld[35050]: Starting MySQL.. SUCCESS!
307 16:17:29 hadoop102 systemd[1]: Started LSB: start and stop MySQL.
[root@hadoop102 mysql-5.7.35]#

[root@hadoop102 mysql-5.7.35]# service mysqld status
SUCCESS! MySQL running (35476)
[root@hadoop102 mysql-5.7.35]#

12. 登录 MySQL 数据库

[root@hadoop102 mysql-5.7.35]# mysql -uroot -p'YqfwlliC&5<p'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35-log

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

13. 修改 MySQL 数据库密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Yq_f7w!lli9C&5<p';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye
[root@hadoop102 mysql-5.7.35]#

14. 重新登录 MySQL 数据库,创建用户并授予权限

可以参考之前这篇文章

[root@hadoop102 mysql-5.7.35]# mysql -uroot -p'Yq_f7w!lli9C&5<p'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'demo'@'localhost' IDENTIFIED BY 'password' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql>
mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| demo | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql>
mysql> select * from mysql.user where `user`="demo";
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| Host | User | Select_priv | Insert_priv | Update_priv | Delete_priv | Create_priv | Drop_priv | Reload_priv | Shutdown_priv | Process_priv | File_priv | Grant_priv | References_priv | Index_priv | Alter_priv | Show_db_priv | Super_priv | Create_tmp_table_priv | Lock_tables_priv | Execute_priv | Repl_slave_priv | Repl_client_priv | Create_view_priv | Show_view_priv | Create_routine_priv | Alter_routine_priv | Create_user_priv | Event_priv | Trigger_priv | Create_tablespace_priv | ssl_type | ssl_cipher | x509_issuer | x509_subject | max_questions | max_updates | max_connections | max_user_connections | plugin | authentication_string | password_expired | password_last_changed | password_lifetime | account_locked |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
| localhost | demo | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | Y | | | | | 0 | 0 | 0 | 0 | mysql_native_password | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 | N | 2022-03-07 16:22:19 | NULL | N |
+-----------+------+-------------+-------------+-------------+-------------+-------------+-----------+-------------+---------------+--------------+-----------+------------+-----------------+------------+------------+--------------+------------+-----------------------+------------------+--------------+-----------------+------------------+------------------+----------------+---------------------+--------------------+------------------+------------+--------------+------------------------+----------+------------+-------------+--------------+---------------+-------------+-----------------+----------------------+-----------------------+-------------------------------------------+------------------+-----------------------+-------------------+----------------+
1 row in set (0.00 sec)

mysql>
mysql> ALTER USER 'demo'@'localhost' IDENTIFIED BY 'mU$N(O#4>wem_cu*t%4%p';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

mysql> set password for 'demo'@'localhost'=password("mU$N(O#4>w@em_c!u*t%4.p");
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> update mysql.user set `host`="%" where `user`="demo";
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> select `user`,`host` from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| demo | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)

mysql> exit;
Bye
[root@hadoop102 mysql-5.7.35]# hostname -I
192.168.20.102
[root@hadoop102 mysql-5.7.35]# mysql -udemo -p'mU$N(O#4>w@em_c!u*t%4.p'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.35-log MySQL Community Server (GPL)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)

mysql> create database test;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)

mysql> use test;
Database changed
mysql> show tables;
Empty set (0.00 sec)

mysql> exit;
Bye

15. 确保 防火墙 是关掉的,或者放行端口

[root@hadoop102 mysql-5.7.35]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Docs: man:firewalld(1)
[root@hadoop102 mysql-5.7.35]#
[root@hadoop102 mysql-5.7.35]# service iptables status
Redirecting to /bin/systemctl status iptables.service
Unit iptables.service could not be found.
[root@hadoop102 mysql-5.7.35]#

如果墙是开着的,那就需要放行端口



在线安装的文章数不胜数,离线安装的此文是相对来说比较详细的了

我们下期见,拜拜!