MySQL安装
官网:https://www.mysql.com/
DOCUMENTATION
MySQL Server >> MySQL 5.7
Install文档
Download LINK
Download
右键复制链接
操作
[root@localhost ~]# hostnamectl --static set-hostname mysql ### 修改主机名
### 重新登录后即可生效 [ !!不是重启!! ]
[root@mysql ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-4.noarch.rpm ### 注意 该RPM居然为8.0版本
[root@mysql ~]# yum install -y mysql80-community-release-el7-4.noarch.rpm ### 安装 该RPM
[root@mysql ~]# ls /etc/yum.repos.d/ | grep mysql ### yum.repo.d 路径下会生成mysql的在线源
mysql-community.repo
mysql-community-source.repo
[root@mysql ~]# yum repolist enabled ### 显示所有可用仓库
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.lzu.edu.cn
* extras: ftp.sjtu.edu.cn
* updates: mirrors.huaweicloud.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base 10,072
extras/7/x86_64 CentOS-7 - Extras 500
mysql-connectors-community/x86_64 MySQL Connectors Community 221
mysql-tools-community/x86_64 MySQL Tools Community 135
mysql80-community/x86_64 MySQL 8.0 Community Server 301
updates/7/x86_64 CentOS-7 - Updates 2,946
repolist: 14,175
[root@mysql ~]# yum repolist all | grep mysql ### 查看拥有的所有仓库 过滤出mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 221
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 135
mysql-tools-community-source MySQL Tools Community - Sour disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 301 ### 默认开启的为8.0
mysql80-community-source MySQL 8.0 Community Server - disabled
[root@mysql ~]# yum -y install yum-utils ### 安装 yum-utils
[root@mysql ~]# yum-config-manager --disable mysql80-community ### disable mysql8.0
[root@mysql ~]# yum repolist all | grep mysql ### 再次查看发现mysql8.0 已被disabled
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 221
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 135
mysql-tools-community-source MySQL Tools Community - Sour disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql57-community/x86_64 MySQL 5.7 Community Server disabled
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disabled
[root@mysql ~]# yum-config-manager --enable mysql57-community ### enable mysql5.7
[root@mysql ~]# yum repolist all | grep mysql ### 再次查看发现mysql5.7 已被 enable
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community disabled
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 221
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 135
mysql-tools-community-source MySQL Tools Community - Sour disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 544
mysql57-community-source MySQL 5.7 Community Server - disabled
mysql80-community/x86_64 MySQL 8.0 Community Server disabled
mysql80-community-source MySQL 8.0 Community Server - disable
[root@mysql ~]# yum repolist enabled | grep mysql ### mysql5.7 enabled
mysql-connectors-community/x86_64 MySQL Connectors Community 221
mysql-tools-community/x86_64 MySQL Tools Community 135
mysql57-community/x86_64 MySQL 5.7 Community Server 544
安装
[root@mysql ~]# yum -y install mysql-community-server mysql ### 安装服务端 客户端
[root@mysql ~]# systemctl start mysqld ### 开启 mysqld服务
[root@mysql ~]# systemctl enable mysqld ### 设置开机自启
[root@mysql ~]# cat /var/log/mysqld.log | grep password ### 获得首次登录密码
2021-11-12T07:06:12.429748Z 1 [Note] A temporary password is generated for root@localhost: NMse%uY=L6p(
[root@mysql ~]# mysql -uroot -p'NMse%uY=L6p(' ### 登录 mysql数据库
mysql> show databases; ### 首次登录MySQL 需要重置密码 否则无权访问
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
### 在执行此语句之前,必须使用ALTER USER语句重置密码
mysql> alter user 'root'@'localhost' identified by 'P@ssw0rd';
Query OK, 0 rows affected (0.00 sec)
### 数据库密码(密码必须符合复杂性要求,包含字母大小写,数字,特殊符号,长度不少于8位)
[root@mysql ~]# mysql -uroot -p'P@ssw0rd' ### 使用新密码登录数据库
mysql> show databases; ### 使用新密码登录后 可查看数据库信息了
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> grant select,drop,insert on *.* to 'wangjun'@'localhost' identified by 'P@ssw0rd'; ### 添加用户权限
Query OK, 0 rows affected, 1 warning (0.02 sec)
mysql> select * from mysql.user; ### 查看 MySQL用户表 所有信息
mysql> select user,host from mysql.user; ### 只查看 MySQL用户表 user,host字段的信息
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
| wangjun | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
mysql> flush privileges; ### 更新权限
Query OK, 0 rows affected (0.00 sec)
mysql> show grants for 'wangjun'@'localhost'; ### 查看用户权限
+------------------------------------------------------------+
| Grants for wangjun@localhost |
+------------------------------------------------------------+
| GRANT SELECT, INSERT, DROP ON *.* TO 'wangjun'@'localhost' |
+------------------------------------------------------------+
1 row in set (0.00 sec)