How to install Mysql 8 on CentOS 8

本文档详细介绍了如何在CentOS8上卸载MySQL8并安装MySQL5.7。首先使用dnf命令安装MySQL5.7的包,然后配置yum源以启用5.7版本,禁用8.0版本。接着启动mysqld服务,设置root用户和密码,并允许远程连接。最后,如果忘记密码,提供了重置的方法。整个过程涵盖了从安装到基本配置的所有步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

install Mysql 8 on CentOS 8

Install the Mysql 8.0 server by using the CentOS package manager as root or user with sudo privileges:

$ sudo dnf install @mysql

The @mysql module installs MySQL and all dependencies.

Once the installation in complete,start the MySQL and enable it to automatically start on boot by running the following command:

$ sudo systemctl enable --now mysqld

To check whether the MySQL server is running,type:

$ sudo systemctl status mysqld

After that we can use the command below with root password or No password:

$ mysql - u root -p

Finally,we can set password for MySQL:

ALTER USER USER() IDENTIFIED BY 'PASSWORD';

There is a option for opening remote:

In mysql 8 it never ever support grant privileges on *.* to 'root'@'%'identified by 'password' with grant option.

If we want to use remote to connect mysql,we must use commands below:

mysql> create user 'root'@'%' identified by 'password';
Query OK, 0 rows affected (0.04 sec)
mysql> grant all privileges on *.* to 'root'@'%';
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;

The above is the process installing mysql 8,but in actual production,most companies use mysql 5.7. So we need remove mysql 8 and reinstall mysql 5.7 with commands below.

We can find rpm download file in mysql official web site with wget downloading.

$ cd ~
$ mkdir downloads
$ wget https://repo.mysql.com//mysql80-community-release-el8-1.noarch.rpm

After downloaded,we can see a file named mysql80-community-release-el8-1.noarch.rpm in directory downloads. we use dnf command to install.

$ dnf install -y mysql80-community-release-el8-1.noarch.rpm

After installed, now in our repository we can see mysql-repository.

$ dnf repolist
AppStream
BaseOS
epel
extrals
mysql-connectors-community
mysql-tools-community
mysql80-community

Now we can install mysql-community-server , but if we install with a mysql 5.7 configure. The installed software still mysql 8. So we can configure file /etc/yum.repos.d/mysql-community.repo with code below and set enabled=0 in [mysql80-community]:

[mysql80-community]
name=MySQL 8.0 Community Server
baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/8/$basearch/
enabled=8
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

Run dnf install -y mysql-community-server to install mysql 5.7

If we met this error: No match for argument: mysql-community-server

we just use this command would be okay. dnf module disable mysql

That’s all how to install mysql 5.7 in CentOS 8.

Next we need to configure mysql 5.7

First step turn on mysqld.service

$ systemctl start mysqld
# or
$ systemctl enable --now mysqld

Second step set user and password:

mysql -uroot -p
mysql> alter user root@localhost identified by 'password';

Allow user to use remote:

mysql> grant all privileges on *.* to 'user'@'%' identified by 'password' with grant option;
mysql> flush priviliages;

If we for get password:

$ vim /etc/my.cnf
# add
[mysqld]
skip-grant-tables
$ systemctl restart mysqld
$ mysql -uroot
mysql> update mysql.user set authentication_string=password where user='root';

Sometimes in others Linux versions, the root user’s default password might be empty, but in CentOS 7, after installed mysql, it would generate a temporary password and we should use command to view it.

cat /var/log/mysqld.log | grep password

And sometimes we can’t see my.cnf file in /etc. But there is another file named default.cnf,we should cp default.cnf my.dnf and configure my.cnf in this file.

If you forget mysql password, you can vim /etc/my.cnf and add skip-grant-tables

sudo systemctl restart mysqld
# login mysql without password
mysql -uroot
# use command to update password
mysql> update mysql.user set authentication_string=password('password') where user='root';
# after that vim /etc/my.cnf with a # before the skip-grant-tables
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Evan.sun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值