How to install Mysql 8 on CentOS 8
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 nameddefault.cnf
,we shouldcp default.cnf my.dnf
and configuremy.cnf
in this file.If you forget mysql password, you can
vim /etc/my.cnf
and addskip-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