centos7 安装mysql

查看mysql官网

https://dev.mysql.com/doc/refman/5.7/en/linux-installation-yum-repo.html

image-20201117190237858

找到 linux 上使用 yum 仓库的方式安装

添加 mysql yum 仓库

根据提示 下载 rpm 文件

image-20201117190732740

因为我的系统是 centos7 所以我选择了这个image-20201117190843318

这里可以把文件下下来后上传到 linux 上。如果机器联网也可以复制下载连接后直接在 linux 上下载

1
2
wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
# el7 表示 对 linux7 的支持

这里不用担心 mysql80 他是包含了以前的版本的(官网有说明)

image-20201117191605123

安装 sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

1
2
3
4
[root@iZx068ismxxjaoZ src]# ll
total 440764
-rw-r--r-- 1 root root 26024 Apr 25 2019 mysql80-community-release-el7-3.noarch.rpm
[root@iZx068ismxxjaoZ src]# sudo rpm -Uvh mysql80-community-release-el7-3.noarch.rpm

查看版本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@iZx068ismxxjaoZ src]#  yum repolist all | grep 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: 175
mysql-connectors-community-source MySQL Connectors Community - disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 120
mysql-tools-community-source MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64 MySQL Tools Preview disabled
mysql-tools-preview-source MySQL Tools Preview - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - disabled
mysql56-community/x86_64 MySQL 5.6 Community Server disabled
mysql56-community-source MySQL 5.6 Community Server - 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: 211
mysql80-community-source MySQL 8.0 Community Server - disabled

修改 mysql yum 仓库 默认配置

我选择安装 5.7 的版本 所以这里根据官方说明 禁掉 8.0 开启 5.7

1
2
[root@iZx068ismxxjaoZ src]# sudo yum-config-manager --disable mysql80-community
sudo: yum-config-manager: command not found

我的系统不支持 yum-config-manager 命令(官网也说了,如果不支持该命令可以直接去修改文件。注意:红框里说的很明白了,开启了多个版本安装的时候会安装最新的版本)

编辑:vim /etc/yum.repos.d/mysql-community.repo

image-20201117192920331

修改完后再匹配查询下:

1
2
3
4
[root@iZx068ismxxjaoZ src]#  yum repolist enabled | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 175
mysql-tools-community/x86_64 MySQL Tools Community 120
mysql57-community/x86_64 MySQL 5.7 Community Server 464

安装mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
sudo yum install mysql-community-server
#过程中提示 是否下载 按 y 确定

## 出现错误:
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql80-community-release-el7-3.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
是否继续?[y/N]:y


mysql-community-libs-compat-5.7.41-1.el7.x86_64.rpm 的公钥尚未安装


失败的软件包是:mysql-community-libs-compat-5.7.41-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
#执行:
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
#继续执行安装命令

安装完成!

1
2
3
4
5
6
7
8
9
10
11
12
Installed:
mysql-community-libs.x86_64 0:5.7.32-1.el7 mysql-community-libs-compat.x86_64 0:5.7.32-1.el7 mysql-community-server.x86_64 0:5.7.32-1.el7

Dependency Installed:
libaio.x86_64 0:0.3.109-13.el7 mysql-community-client.x86_64 0:5.7.32-1.el7 mysql-community-common.x86_64 0:5.7.32-1.el7

Replaced:
mariadb-libs.x86_64 1:5.5.65-1.el7

Complete!
[root@iZx068ismxxjaoZ src]#

启动mysql

1
systemctl start mysqld.service

官网说明

查看密码

1
2
[root@iZx068ismxxjaoZ src]# grep 'temporary password' /var/log/mysqld.log
2020-11-17T11:45:46.549571Z 1 [Note] A temporary password is generated for root@localhost: ;LdxZgpZe76H

登录并修改密码

1
2
mysql -uroot -p
# 输入密码的时候 大写的字母我用 大写开关 失败。 然后 用 shift + 字母 的方式输入大写才成功
1
2
3
4
5
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'new password'
-> ;
Query OK, 0 rows affected (0.00 sec)

mysql>

开启远程访问

1
2
mysql> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)

刷新

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

修改字符集

查看默认字符集

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
mysql> status;
--------------
mysql Ver 14.14 Distrib 5.7.32, for Linux (x86_64) using EditLine wrapper

Connection id: 15
Current database:
Current user: root@localhost
SSL: Not in use
Current pager: stdout
Using outfile: ''
Using delimiter: ;
Server version: 5.7.32 MySQL Community Server (GPL)
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 38 min 44 sec

Threads: 2 Questions: 18 Slow queries: 0 Opens: 114 Flush tables: 1 Open tables: 107 Queries per second avg: 0.007
--------------

mysql>

修改字符集

停止 mysql :systemctl stop mysqld.service

编辑文件:vim /etc/my.cnf 加入四行

image-20201117203429665

保存 my.cnf 后重启 MySQL :systemctl start mysqld.service

再次登录 mysql 查看状态

image-20201117203842429