在Centos8上用原来Centos7上安装MySQL5.7的方法会安装失败,显示mysql-community-server安装错误。我们用新的方法在Centos8上安装MySQL5.7
安装MySQL
- 添加MySQL存储库
禁用MySQL默认的AppStream存储库:
sudo dnf remove @mysql
sudo dnf module reset mysql && sudo dnf module disable mysql
centos8没有MySQL存储库,因此我们将使用centos 7存储库。创建一个新的存储库文件。
sudo vim /etc/yum.repos.d/mysql-community.repo
将以下数据插入上面的存储库中
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=0
[mysql-connectors-community]
name=MySQL Connectors Community
baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
enabled=1
gpgcheck=0
[mysql-tools-community]
name=MySQL Tools Community
baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
enabled=1
gpgcheck=0
- 安装MySQL(这里我选择MySQL5.7)
sudo dnf --enablerepo=mysql57-community install mysql-community-server
如果安装失败,使用以下方法进行安装
- 先下载rpm包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-common-5.7.27-1.el6.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-libs-5.7.27-1.el6.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-client-5.7.27-1.el6.x86_64.rpm wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-community-server-5.7.27-1.el6.x86_64.rpm
- 再安装
yum install -y mysql-community-common-5.7.27-1.el6.x86_64.rpm yum install -y mysql-community-libs-5.7.27-1.el6.x86_64.rpm yum install -y mysql-community-client-5.7.27-1.el6.x86_64.rpm yum install -y mysql-community-server-5.7.27-1.el6.x86_64.rpm
- 下载完成后检查版本 (出现以下信息说明安装成功 )
[root@test ~]# rpm -qi mysql-community-server
Name : mysql-community-server
Version : 5.7.29
Release : 1.el7
Architecture: x86_64
Install Date: Sat 22 Feb 2020 11:04:07 AM CST
Group : Applications/Databases
Size : 801919839
License : Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field.
Signature : DSA/SHA1, Thu 19 Dec 2019 04:12:40 PM CST, Key ID 8c718d3b5072e1f5
Source RPM : mysql-community-5.7.29-1.el7.src.rpm
Build Date : Wed 18 Dec 2019 09:31:48 PM CST
Build Host : loki02.no.oracle.com
Relocations : (not relocatable)
Packager : MySQL Release Engineering
Vendor : Oracle and/or its affiliates
URL : http://www.mysql.com/
Summary : A very fast and reliable SQL database server
- 检查 mysql 源是否安装成功
yum repolist enabled | grep "mysql.*-community.*"
出现以下信息说明安装成功:
mysql-connectors-community MySQL Connectors Community 141
mysql-tools-community MySQL Tools Community 105
mysql57-community MySQL 5.7 Community Server
5.. 启动MySQL
systemctl start mysqld
- 查看启动状态
systemctl status mysqld
出现以下信息,则启动成功
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2020-02-22 11:14:47 CST; 2h 19min ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 21345 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
Process: 21323 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
Main PID: 21349 (mysqld)
Tasks: 30 (limit: 11516)
Memory: 209.1M
CGroup: /system.slice/mysqld.service
?..21349 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
- 设置开机启动
systemctl enable mysqld
- 刷新所有修改过的配置文件
systemctl daemon-reload
- 获取安装mysql后生成的临时密码,用于登录
grep 'temporary password' /var/log/mysqld.log
出现如下面信息,密码为: azx(1u;Br
2020-02-22T03:05:17.741049Z 1 [Note] A temporary password is generated for root@localhost: BL=azx(1u;Br
- 登录MySQL
mysql -uroot -p
再输入上面查找得到的临时密码即可进入mysql
- 修改登录密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码(修改后的密码,注意必须包含大小写字母数字以及特殊字符并且长度不能少于8位,否则会报错)';
或者通过:mysql> set password for 'root'@'localhost'=password('新密码');
或者通过:mysql> use mysql;
mysql> update user set password=PASSWORD('新密码') where user='root';
mysql> flush privileges;
- 添加远程登录用户(即本机访问服务器上的MySQL)
mysql> GRANT ALL PRIVILEGES ON *.* TO '用户名'@'%' IDENTIFIED BY '密码' WITH GRANT OPTION;
# 或者直接将root权限修改为可以通过远程访问(但不推荐)
mysql> use mysql;
mysql> UPDATE user SET Host='%' WHERE User='root';
mysql> flush privileges;
- 设置默认编码为utf-8(mysql安装后默认不支持中文)
vim /etc/my.cnf
进入文件后添加下面的配置即可
[mysqld]
character-set-server=utf8
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
- 重启MySQL服务并进入MySQL
shell> systemctl restart mysqld
shell> mysql -uroot -p
mysql> show variables like 'character%';
出现如下则说明编码修改完成
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
- 退出MySQL
mysql> quit