检查系统中是否已安装 MySQL。
rpm -qa | grep mysql
返回空值的话,就说明没有安装 MySQL
查看已安装的 Mariadb 数据库版本
# rpm -qa|grep -i mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
卸载已安装的 Mariadb 数据库
rpm -qa|grep mariadb|xargs rpm -e --nodeps
再次查看已安装的 Mariadb 数据库版本,确认是否卸载完成
rpm -qa|grep -i mariadb
下载安装包文件
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
安装mysql-community-release-el7-5.noarch.rpm包
rpm -ivh mysql-community-release-el7-5.noarch.rpm
安装完成之后,会在 /etc/yum.repos.d/ 目录下新增 mysql-community.repo 、mysql-community-source.repo 两个 yum 源文件
[root@bogon yum.repos.d]# ls
CentOS-Base.repo CentOS-Sources.repo ius.repo
CentOS-CR.repo CentOS-Vault.repo ius-testing.repo
CentOS-Debuginfo.repo epel.repo mysql-community.repo
CentOS-fasttrack.repo epel-testing.repo mysql-community-source.repo
CentOS-Media.repo ius-archive.repo
执行 yum repolist all | grep mysql 命令查看可用的 mysql 安装文件
[root@bogon yum.repos.d]# yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 131
mysql-connectors-community-source MySQL Connectors Community - S disabled
mysql-tools-community/x86_64 MySQL Tools Community enabled: 100
mysql-tools-community-source MySQL Tools Community - Source disabled
mysql55-community/x86_64 MySQL 5.5 Community Server disabled
mysql55-community-source MySQL 5.5 Community Server - S disabled
mysql56-community/x86_64 MySQL 5.6 Community Server enabled: 496
mysql56-community-source MySQL 5.6 Community Server - S disabled
mysql57-community-dmr/x86_64 MySQL 5.7 Community Server Dev disabled
mysql57-community-dmr-source MySQL 5.7 Community Server Dev disabled
安装mysql
yum install mysql-server
[root@bogon yum.repos.d]# yum install mysql-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirror.bit.edu.cn
* epel: fedora.cs.nctu.edu.tw
* extras: mirrors.huaweicloud.com
* updates: mirror.bit.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.46-2.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) = 5.6.46-2.el7 for package: mysql-community-server-5.6.46-2.el7.x86_64
--> Processing Dependency: mysql-community-client(x86-64) >= 5.6.10 for package: mysql-community-server-5.6.46-2.el7.x86_64
--> Processing Dependency: perl(Data::Dumper) for package: mysql-community-server-5.6.46-2.el7.x86_64
--> Processing Dependency: perl(DBI) for package: mysql-community-server-5.6.46-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.46-2.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.46-2.el7.x86_64
---> Package mysql-community-common.x86_64 0:5.6.46-2.el7 will be installed
---> Package perl-DBI.x86_64 0:1.627-4.el7 will be installed
--> Processing Dependency: perl(RPC::PlServer) >= 0.2001 for package: perl-DBI-1.627-4.el7.x86_64
--> Processing Dependency: perl(RPC::PlClient) >= 0.2000 for package: perl-DBI-1.627-4.el7.x86_64
---> Package perl-Data-Dumper.x86_64 0:2.145-3.el7 will be installed
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.46-2.el7 will be installed
---> Package perl-PlRPC.noarch 0:0.2020-14.el7 will be installed
--> Processing Dependency: perl(Net::Daemon) >= 0.13 for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Test) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Net::Daemon::Log) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Processing Dependency: perl(Compress::Zlib) for package: perl-PlRPC-0.2020-14.el7.noarch
--> Running transaction check
---> Package perl-IO-Compress.noarch 0:2.061-2.el7 will be installed
--> Processing Dependency: perl(Compress::Raw::Zlib) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
--> Processing Dependency: perl(Compress::Raw::Bzip2) >= 2.061 for package: perl-IO-Compress-2.061-2.el7.noarch
---> Package perl-Net-Daemon.noarch 0:0.48-5.el7 will be installed
--> Running transaction check
---> Package perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7 will be installed
---> Package perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7 will be installed
--> Finished Dependency Resolution
/var/cache/yum/x86_64/7/base/gen/comps.xml: no element found: line 1, column 0
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
mysql-community-server x86_64 5.6.46-2.el7 mysql56-community 86 M
Installing for dependencies:
mysql-community-client x86_64 5.6.46-2.el7 mysql56-community 39 M
mysql-community-common x86_64 5.6.46-2.el7 mysql56-community 289 k
mysql-community-libs x86_64 5.6.46-2.el7 mysql56-community 4.1 M
perl-Compress-Raw-Bzip2 x86_64 2.061-3.el7 base 32 k
perl-Compress-Raw-Zlib x86_64 1:2.061-4.el7 base 57 k
perl-DBI x86_64 1.627-4.el7 base 802 k
perl-Data-Dumper x86_64 2.145-3.el7 base 47 k
perl-IO-Compress noarch 2.061-2.el7 base 260 k
perl-Net-Daemon noarch 0.48-5.el7 base 51 k
perl-PlRPC noarch 0.2020-14.el7 base 36 k
Transaction Summary
================================================================================
Install 1 Package (+10 Dependent packages)
Total download size: 130 M
Installed size: 527 M
Is this ok [y/d/N]: y
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql56-community/packages/mysql-community-common-5.6.46-2.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-5.6.46-2.el7.x86_64.rpm is not installed
(1/11): mysql-community-common-5.6.46-2.el7.x86_64.rpm | 289 kB 00:01
(2/11): mysql-community-libs-5.6.46-2.el7.x86_64.rpm | 4.1 MB 00:01
(3/11): perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64.rpm | 32 kB 00:00
(4/11): perl-DBI-1.627-4.el7.x86_64.rpm | 802 kB 00:00
(5/11): perl-Compress-Raw-Zlib-2.061-4.el7.x86_64.rpm | 57 kB 00:00
(6/11): perl-IO-Compress-2.061-2.el7.noarch.rpm | 260 kB 00:00
(7/11): perl-Data-Dumper-2.145-3.el7.x86_64.rpm | 47 kB 00:00
(8/11): perl-Net-Daemon-0.48-5.el7.noarch.rpm | 51 kB 00:00
(9/11): perl-PlRPC-0.2020-14.el7.noarch.rpm | 36 kB 00:00
(10/11): mysql-community-client-5.6.46-2.el7.x86_64.rpm | 39 MB 00:08
(11/11): mysql-community-server-5.6.46-2.el7.x86_64.rpm | 86 MB 00:17
--------------------------------------------------------------------------------
Total 6.5 MB/s | 130 MB 00:19
Retrieving key from file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
Userid : "MySQL Release Engineering "
Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
Package : mysql-community-release-el7-5.noarch (installed)
From : file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Is this ok [y/N]: y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 2 pre-existing rpmdb problem(s), 'yum check' output follows:
2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-7.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
Installing : mysql-community-common-5.6.46-2.el7.x86_64 1/11
Installing : perl-Data-Dumper-2.145-3.el7.x86_64 2/11
Installing : mysql-community-libs-5.6.46-2.el7.x86_64 3/11
Installing : mysql-community-client-5.6.46-2.el7.x86_64 4/11
Installing : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 5/11
Installing : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 6/11
Installing : perl-IO-Compress-2.061-2.el7.noarch 7/11
Installing : perl-Net-Daemon-0.48-5.el7.noarch 8/11
Installing : perl-PlRPC-0.2020-14.el7.noarch 9/11
Installing : perl-DBI-1.627-4.el7.x86_64 10/11
Installing : mysql-community-server-5.6.46-2.el7.x86_64 11/11
Verifying : perl-Net-Daemon-0.48-5.el7.noarch 1/11
Verifying : perl-Data-Dumper-2.145-3.el7.x86_64 2/11
Verifying : mysql-community-libs-5.6.46-2.el7.x86_64 3/11
Verifying : perl-IO-Compress-2.061-2.el7.noarch 4/11
Verifying : mysql-community-server-5.6.46-2.el7.x86_64 5/11
Verifying : 1:perl-Compress-Raw-Zlib-2.061-4.el7.x86_64 6/11
Verifying : mysql-community-common-5.6.46-2.el7.x86_64 7/11
Verifying : perl-DBI-1.627-4.el7.x86_64 8/11
Verifying : perl-Compress-Raw-Bzip2-2.061-3.el7.x86_64 9/11
Verifying : perl-PlRPC-0.2020-14.el7.noarch 10/11
Verifying : mysql-community-client-5.6.46-2.el7.x86_64 11/11
Installed:
mysql-community-server.x86_64 0:5.6.46-2.el7
Dependency Installed:
mysql-community-client.x86_64 0:5.6.46-2.el7
mysql-community-common.x86_64 0:5.6.46-2.el7
mysql-community-libs.x86_64 0:5.6.46-2.el7
perl-Compress-Raw-Bzip2.x86_64 0:2.061-3.el7
perl-Compress-Raw-Zlib.x86_64 1:2.061-4.el7
perl-DBI.x86_64 0:1.627-4.el7
perl-Data-Dumper.x86_64 0:2.145-3.el7
perl-IO-Compress.noarch 0:2.061-2.el7
perl-Net-Daemon.noarch 0:0.48-5.el7
perl-PlRPC.noarch 0:0.2020-14.el7
Complete!
[root@bogon yum.repos.d]#
检查mysql是否安装成功
rpm -qa | grep mysql
[root@bogon yum.repos.d]# rpm -qa | grep mysql
mysql-community-client-5.6.46-2.el7.x86_64
mysql-community-common-5.6.46-2.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-libs-5.6.46-2.el7.x86_64
mysql-community-server-5.6.46-2.el7.x86_64
启动 mysql 服务
systemctl start mysqld.service #启动 mysql
systemctl restart mysqld.service #重启 mysql
systemctl stop mysqld.service #停止 mysql
systemctl enable mysqld.service #设置 mysql 开机启动
设置密码
# mysql -u root
mysql> use mysql;
mysql> update user set password=PASSWORD("这里输入root用户密码") where User='root';
mysql> flush privileges;
例如:
[root@bogon yum.repos.d]# mysql -u root
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.46 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> update user set password=PASSWORD("root") where User='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
[root@bogon yum.repos.d]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@bogon yum.repos.d]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.46 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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> create database mydb;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mydb |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
mysql>
设置远程主机登录
mysql> GRANT ALL PRIVILEGES ON *.* TO 'your username'@'%' IDENTIFIED BY 'your password';
执行以下命令,为root 用户添加远程登录的能力。
mysql> GRANT ALL PRIVILEGES ON *.* TO root@"%" IDENTIFIED BY "root";