开始安装
- 下载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 09:20:00-- 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 09:20:02-- https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm 正在解析主机 repo.mysql.com (repo.mysql.com)... 23.57.113.239 正在连接 repo.mysql.com (repo.mysql.com)|23.57.113.239|:443... 已连接。 已发出 HTTP 请求,正在等待回应... 200 OK 长度:26024 (25K) [application/x-redhat-package-manager] 正在保存至: “mysql80-community-release-el7-3.noarch.rpm.2” mysql80-community-re 100%[====================>] 25.41K --.-KB/s 用时 0.1s 2021-04-28 09:20:03 (253 KB/s) - 已保存 “mysql80-community-release-el7-3.noarch.rpm.2” [26024/26024])
2. 安装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%] 软件包 mysql80-community-release-el7-3.noarch 已经安装
进入文件更改配置
[root@localhost ~]# cd /etc/yum.repos.d/ [root@localhost yum.repos.d]# cd /etc/yum.repos.d/ # Enable to use MySQL 5.6 [mysql56-community] name=MySQL 5.6 Community Server baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/ enabled=0 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql # 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=0 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=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql [mysql-connectors-community] name=MySQL Connectors Community "mysql-community.repo" 72L, 2076C 28,9 15%
安装MySQL 并启动查看
[root@localhost ~]# yum module disable mysql 上次元数据过期检查:0:03:54 前,执行于 2021年04月28日 星期三 09时26分37秒。 依赖关系解决。 无需任何处理。 完毕! [root@localhost ~]# yum -y install mysql-community-server 上次元数据过期检查:0:05:00 前,执行于 2021年04月28日 星期三 09时26分37秒。 依赖关系解决。 ================================================================================== 软件包 架构 版本 仓库 大小 ================================================================================== 安装: mysql-community-server x86_64 8.0.24-1.el7 mysql80-community 428 M 安装依赖关系: compat-openssl10 x86_64 1:1.0.2o-3.el8 appstream 1.1 M mysql-community-client x86_64 8.0.24-1.el7 mysql80-community 45 M mysql-community-client-plugins x86_64 8.0.24-1.el7 mysql80-community 189 k mysql-community-common x86_64 8.0.24-1.el7 mysql80-community 614 k mysql-community-libs x86_64 8.0.24-1.el7 mysql80-community 4.0 M ncurses-compat-libs x86_64 6.1-7.20180224.el8 baseos 331 k 事务概要 ================================================================================== 安装 7 软件包 总计:479 M 总下载:428 M 安装大小:2.2 G 下载软件包: [SKIPPED] compat-openssl10-1.0.2o-3.el8.x86_64.rpm: Already downloaded [SKIPPED] ncurses-compat-libs-6.1-7.20180224.el8.x86_64.rpm: Already downloaded [SKIPPED] mysql-community-client-8.0.24-1.el7.x86_64.rpm: Already downloaded [SKIPPED] mysql-community-client-plugins-8.0.24-1.el7.x86_64.rpm: Already downloaded [SKIPPED] mysql-community-common-8.0.24-1.el7.x86_64.rpm: Already downloaded [SKIPPED] mysql-community-libs-8.0.24-1.el7.x86_64.rpm: Already downloaded [MIRROR] mysql-community-server-8.0.24-1.el7.x86_64.rpm: Interrupted by header callback: Server reports Content-Length: 88250516 but expected size is: 448452756 (7/7): mysql-community-server-8.0.24-1.el7.x86_64 17 MB/s | 428 MB 00:25 ---------------------------------------------------------------------------------- 总计 18 MB/s | 479 MB 00:26 警告:/var/cache/dnf/mysql80-community-fa2cafb076194999/packages/mysql-community-client-8.0.24-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY MySQL 8.0 Community Server 1.9 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 安装 : compat-openssl10-1:1.0.2o-3.el8.x86_64 1/7 运行脚本: compat-openssl10-1:1.0.2o-3.el8.x86_64 1/7 安装 : mysql-community-client-plugins-8.0.24-1.el7.x86_64 2/7 安装 : mysql-community-common-8.0.24-1.el7.x86_64 3/7 安装 : mysql-community-libs-8.0.24-1.el7.x86_64 4/7 运行脚本: mysql-community-libs-8.0.24-1.el7.x86_64 4/7 安装 : ncurses-compat-libs-6.1-7.20180224.el8.x86_64 5/7 安装 : mysql-community-client-8.0.24-1.el7.x86_64 6/7 运行脚本: mysql-community-server-8.0.24-1.el7.x86_64 7/7 安装 : mysql-community-server-8.0.24-1.el7.x86_64 7/7 运行脚本: mysql-community-server-8.0.24-1.el7.x86_64 7/7 [/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. 验证 : compat-openssl10-1:1.0.2o-3.el8.x86_64 1/7 验证 : ncurses-compat-libs-6.1-7.20180224.el8.x86_64 2/7 验证 : mysql-community-client-8.0.24-1.el7.x86_64 3/7 验证 : mysql-community-client-plugins-8.0.24-1.el7.x86_64 4/7 验证 : mysql-community-common-8.0.24-1.el7.x86_64 5/7 验证 : mysql-community-libs-8.0.24-1.el7.x86_64 6/7 验证 : mysql-community-server-8.0.24-1.el7.x86_64 7/7 Installed products updated. 已安装: compat-openssl10-1:1.0.2o-3.el8.x86_64 mysql-community-client-8.0.24-1.el7.x86_64 mysql-community-client-plugins-8.0.24-1.el7.x86_64 mysql-community-common-8.0.24-1.el7.x86_64 mysql-community-libs-8.0.24-1.el7.x86_64 mysql-community-server-8.0.24-1.el7.x86_64 ncurses-compat-libs-6.1-7.20180224.el8.x86_64 完毕!
查看 更改临时密码
[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 09:30:57 localhost.localdomain systemd[1]: Starting MySQL Server... 4月 28 09:31: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 'tengjia123!'; 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 -ptengjia123! 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
数据库
-
创建数据库,先查看
mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.00 sec)
创建一个teng
mysql> create database teng; Query OK, 1 row affected (0.00 sec) mysql> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | teng | +--------------------+ 5 rows in set (0.00 sec)
表的操作
-
mysql> create table student(id int(11) not null auto_increment primary key,name varchar(100) not null,age tinyint(4)); //创建student表 Query OK, 0 rows affected (0.01 sec) mysql> desc student; //查看student表结构 +-------+--------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+--------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(100) | NO | | NULL | | | age | tinyint(4) | YES | | NULL | | +-------+--------------+------+-----+---------+----------------+ mysql> show tables; //查看表 +-----------------+ | Tables_in_teng | +-----------------+ | student | +-----------------+ 1 row in set (0.00 sec) mysql> drop table student; //删除表 Query OK, 0 rows affected (0.00 sec) mysql> show tables; Empty set (0.00 sec)