下载MySQL
[root@localhost ~]# wget http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
URL transformed to HTTPS due to an HSTS policy
--2021-04-28 06:55:22-- https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
正在解析主机 dev.mysql.com (dev.mysql.com)... 137.254.60.11
正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm [跟随至新的 URL]
--2021-04-28 06:55:24-- https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 23.1.245.43
正在连接 repo.mysql.com (repo.mysql.com)|23.1.245.43|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:26024 (25K) [application/x-redhat-package-manager]
正在保存至: “mysql80-community-release-el7-3.noarch.rpm”
mysql80-community-relea 100%[===============================>] 25.41K --.-KB/s 用时 0s
2021-04-28 06:55:25 (202 MB/s) - 已保存 “mysql80-community-release-el7-3.noarch.rpm” [26024/26024])
安装MySQL的安装工具
[root@localhost ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
警告:mysql80-community-release-el7-3.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
Verifying... ################################# [100%]
准备中... ################################# [100%]
正在升级/安装...
1:mysql80-community-release-el7-3 ################################# [100%]
此时默认装的是MySQL8.0,我们需要的是MySQL5.7,需要对配置进行更改
[root@localhost ~]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# vim mysql-community.repo
//进入文件之后对配置进行更改。
# Enable to use MySQL 5.7
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1 //[mysql57 - community]下的enabled=0改为1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
enabled=0 //[mysql80 - community]下的enabled=1改为0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
安装MySQL
[root@localhost yum.repos.d]# yum module disable mysql
MySQL 5.7 Community Server 106 kB/s | 2.2 MB 00:21
MySQL Connectors Community 23 kB/s | 107 kB 00:04
MySQL Tools Community 35 kB/s | 630 kB 00:18
依赖关系解决。
================================================================================================
软件包 架构 版本 仓库 大小
================================================================================================
禁用模块:
mysql
事务概要
================================================================================================
确定吗?[y/N]: y
完毕!
[root@localhost yum.repos.d]# yum -y install mysql-community-server
上次元数据过期检查:0:00:24 前,执行于 2021年04月28日 星期三 06时57分32秒。
依赖关系解决。
================================================================================================
软件包 架构 版本 仓库 大小
================================================================================================
安装:
mysql-community-server x86_64 5.7.34-1.el7 mysql57-community 173 M
安装依赖关系:
mysql-community-client x86_64 5.7.34-1.el7 mysql57-community 25 M
mysql-community-common x86_64 5.7.34-1.el7 mysql57-community 310 k
mysql-community-libs x86_64 5.7.34-1.el7 mysql57-community 2.4 M
ncurses-compat-libs x86_64 6.1-7.20180224.el8 baseos 331 k
事务概要
================================================================================================
安装 5 软件包
总下载:202 M
安装大小:876 M
下载软件包:
(1/5): ncurses-compat-libs-6.1-7.20180224.el8.x86_64.rpm 865 kB/s | 331 kB 00:00
(2/5): mysql-community-client-5.7.34-1.el7.x86_64.rpm 1.9 MB/s | 25 MB 00:13
(3/5): mysql-community-common-5.7.34-1.el7.x86_64.rpm 16 kB/s | 310 kB 00:19
(4/5): mysql-community-server-5.7.34-1.el7.x86_64.rpm 2.3 MB/s | 173 MB 01:15
(5/5): mysql-community-libs-5.7.34-1.el7.x86_64.rpm 8.4 kB/s | 2.4 MB 04:49
------------------------------------------------------------------------------------------------
总计 712 kB/s | 202 MB 04:50
警告:/var/cache/dnf/mysql57-community-65b9ecb4cc6a2553/packages/mysql-community-client-5.7.34-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
MySQL 5.7 Community Server 27 MB/s | 27 kB 00:00
导入 GPG 公钥 0x5072E1F5:
Userid: "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹: A4A9 4068 76FC BD3C 4567 70C8 8C71 8D3B 5072 E1F5
来自: /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
导入公钥成功
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
准备中 : 1/1
安装 : mysql-community-common-5.7.34-1.el7.x86_64 1/5
安装 : mysql-community-libs-5.7.34-1.el7.x86_64 2/5
运行脚本: mysql-community-libs-5.7.34-1.el7.x86_64 2/5
安装 : ncurses-compat-libs-6.1-7.20180224.el8.x86_64 3/5
安装 : mysql-community-client-5.7.34-1.el7.x86_64 4/5
运行脚本: mysql-community-server-5.7.34-1.el7.x86_64 5/5
安装 : mysql-community-server-5.7.34-1.el7.x86_64 5/5
运行脚本: mysql-community-server-5.7.34-1.el7.x86_64 5/5
[/usr/lib/tmpfiles.d/mysql.conf:23] Line references path below legacy directory /var/run/, updating /var/run/mysqld → /run/mysqld; please update the tmpfiles.d/ drop-in file accordingly.
验证 : ncurses-compat-libs-6.1-7.20180224.el8.x86_64 1/5
验证 : mysql-community-client-5.7.34-1.el7.x86_64 2/5
验证 : mysql-community-common-5.7.34-1.el7.x86_64 3/5
验证 : mysql-community-libs-5.7.34-1.el7.x86_64 4/5
验证 : mysql-community-server-5.7.34-1.el7.x86_64 5/5
Installed products updated.
已安装:
mysql-community-client-5.7.34-1.el7.x86_64 mysql-community-common-5.7.34-1.el7.x86_64
mysql-community-libs-5.7.34-1.el7.x86_64 mysql-community-server-5.7.34-1.el7.x86_64
ncurses-compat-libs-6.1-7.20180224.el8.x86_64
完毕!
[root@localhost yum.repos.d]# yum -y install mysql-community-server
上次元数据过期检查:0:08:44 前,执行于 2021年04月28日 星期三 06时57分32秒。
软件包 mysql-community-server-5.7.34-1.el7.x86_64 已安装。
依赖关系解决。
无需任何处理。
完毕!
[root@localhost yum.repos.d]# systemctl start mysqld.service
[root@localhost yum.repos.d]# systemctl status mysqld.service
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-04-28 07:06:35 EDT; 6s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 132597 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid >
Process: 132377 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 132599 (mysqld)
Tasks: 27 (limit: 11070)
Memory: 280.0M
CGroup: /system.slice/mysqld.service
└─132599 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
4月 28 07:06:29 localhost.localdomain systemd[1]: Starting MySQL Server...
查看临时密码:
[root@localhost yum.repos.d]# grep 'temporary password' /var/log/mysqld.log
2021-04-28T11:06:32.738686Z 1 [Note] A temporary password is generated for root@localhost: eyL=lusdq2y7
配置
设置开机自启动
[root@localhost ~]# systemctl enable --now mysqld
[root@localhost ~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-04-28 08:11:32 EDT; 1min 35s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 2067 (mysqld)
Tasks: 27 (limit: 11070)
Memory: 167.6M
CGroup: /system.slice/mysqld.service
└─2067 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
4月 28 08:10:57 localhost.localdomain systemd[1]: Starting MySQL Server...
4月 28 08:11:32 localhost.localdomain systemd[1]: Started MySQL Server.
确保3306端口监听起来
[root@localhost ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 0.0.0.0:*
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 5 127.0.0.1:631 0.0.0.0:*
LISTEN 0 80 *:3306 *:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 5 [::1]:631 [::]:*
找出临时密码
[root@localhost ~]# grep "password" /var/log/mysqld.log
2021-04-28T11:06:32.738686Z 1 [Note] A temporary password is generated for root@localhost: eyL=lusdq2y7
用临时密码登录
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.34
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> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.01 sec)
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'huangfang123!';
Query OK, 0 rows affected (0.01 sec)
mysql工具使用
//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
-uUSERNAME //指定用户名,默认为root
-hHOST //指定服务器主机,默认为localhost,推荐使用ip地址
-pPASSWORD //指定用户的密码
-P# //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
-V //查看当前使用的mysql版本
-e //不登录mysql执行sql语句后退出,常用于脚本
[root@localhost ~]# mysql -V
mysql Ver 14.14 Distrib 5.7.34, for Linux (x86_64) using EditLine wrapper
[root@localhost ~]# mysql -uroot -phuangfang123! -e 'show databases;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
[root@localhost ~]# mysql -uroot -phuangfang123!
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 13
Server version: 5.7.34 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> quit
Bye
数据库操作
//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';
mysql> CREATE DATABASE IF NOT EXISTS huang; //创建数据库
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES; //查看当前实例有哪些数据库
+--------------------+
| Database |
+--------------------+
| information_schema |
| huang |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库
mysql> DROP DATABASE IF EXISTS huang;
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
表操作
//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
mysql> CREATE DATABASE huang; //创建数据库huang
Query OK, 1 row affected (0.00 sec)
mysql> use huang //进入huang数据库
Database changed
mysql> use huang;
Database changed
mysql> CREATE TABLE huang (id int NOT NULL,name VARCHAR(100) NOT NULL,age tinyint); //创建huang表
Query OK, 0 rows affected (0.02 sec)
mysql> CREATE TABLE huangf (id int NOT NULL,name VARCHAR(100) NOT NULL,age tinyint); //创建huangf表
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW TABLES; //查看当前数据库有哪些表
+-----------------+
| Tables_in_huang |
+-----------------+
| huang |
| huangf |
+-----------------+
2 rows in set (0.00 sec)
//删除表
//语法:DROP TABLE [ IF EXISTS ] 'table_name';
mysql> DROP TABLE huang; //删除huang表
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW TABLES; //查看当前数据库有哪些表
+-----------------+
| Tables_in_huang |
+-----------------+
| huangf |
+-----------------+
1 row in set (0.00 sec)