环境:CentOS 7.4 1708
从CentOS 7.0发布以来,yum源中开始使用mariadb来代替MySQL的安装。即使你输入的是yum install mysql , 显示的也是mariadb的安装内容。
输入yum install mysql-server,提示yum没有可用的安装包。
因此,如果使用yum安装MySQL的话,就需要去下载官方指定的yum源。
网址为: https://dev.mysql.com/downloads/repo/yum/
先卸载mariadb。
检查mariadb是否已安装
[root@localhost ~]# yum list installed | grep mariadb
mariadb-libs.x86_64 1:5.5.56-2.el7 @anaconda
全部卸载
[root@localhost ~]# yum -y remove mariadb*
已加载插件:fastestmirror, langpacks
正在解决依赖关系
--> 正在检查事务
---> 软件包 mariadb-libs.x86_64.1.5.5.56-2.el7 将被 删除
--> 正在处理依赖关系 libmysqlclient.so.18()(64bit),它被软件包 2:postfix-2.10.1-6.el7.x86_64 需要
--> 正在处理依赖关系 libmysqlclient.so.18(libmysqlclient_18)(64bit),它被软件包 2:postfix-2.10.1-6.el7.x86_64 需要
--> 正在检查事务
---> 软件包 postfix.x86_64.2.2.10.1-6.el7 将被 删除
--> 解决依赖关系完成
依赖关系解决
====================================================================================================
Package 架构 版本 源 大小
====================================================================================================
正在删除:
mariadb-libs x86_64 1:5.5.56-2.el7 @anaconda 4.4 M
为依赖而移除:
postfix x86_64 2:2.10.1-6.el7 @anaconda 12 M
事务概要
====================================================================================================
移除 1 软件包 (+1 依赖软件包)
安装大小:17 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在删除 : 2:postfix-2.10.1-6.el7.x86_64 1/2
正在删除 : 1:mariadb-libs-5.5.56-2.el7.x86_64 2/2
验证中 : 1:mariadb-libs-5.5.56-2.el7.x86_64 1/2
验证中 : 2:postfix-2.10.1-6.el7.x86_64 2/2
删除:
mariadb-libs.x86_64 1:5.5.56-2.el7
作为依赖被删除:
postfix.x86_64 2:2.10.1-6.el7
完毕!
[root@localhost ~]# yum list installed | grep mariadb
[root@localhost ~]#
下面进行MySQL的安装。
一、下载安装官方提供的yum rpm包
打开网页:https://dev.mysql.com/downloads/repo/yum/
点击Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package后面的download,进入新的页面点击No thanks, just start my download.就可以看到下载源地址了。
进入如下目录
[root@localhost ~]# cd /usr/local/src/
下载rpm包:wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
[root@localhost src]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
--2018-02-11 14:26:03-- https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
正在解析主机 dev.mysql.com (dev.mysql.com)... 137.254.60.11
正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm [跟随至新的 URL]
--2018-02-11 14:26:05-- https://repo.mysql.com//mysql57-community-release-el7-11.noarch.rpm
正在解析主机 repo.mysql.com (repo.mysql.com)... 96.6.11.178
正在连接 repo.mysql.com (repo.mysql.com)|96.6.11.178|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:25680 (25K) [application/x-redhat-package-manager]
正在保存至: “mysql57-community-release-el7-11.noarch.rpm”
100%[==========================================================>] 25,680 99.4KB/s 用时 0.3s
2018-02-11 14:26:06 (99.4 KB/s) - 已保存 “mysql57-community-release-el7-11.noarch.rpm” [25680/25680])
[root@localhost src]# ll
总用量 28
-rw-r--r--. 1 root root 25680 4月 27 2017 mysql57-community-release-el7-11.noarch.rpm
[root@localhost src]#
安装rpm包:rpm -ivh mysql57-community-release-el7-11.noarch.rpm
[root@localhost src]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm
警告:mysql57-community-release-el7-11.noarch.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:mysql57-community-release-el7-11 ################################# [100%]
检查mysql的yum源是否安装成功:yum repolist enabled | grep "mysql.*-community.*"
[root@localhost src]# yum repolist enabled | grep "mysql.*-community.*"
mysql-connectors-community/x86_64 MySQL Connectors Community 45
mysql-tools-community/x86_64 MySQL Tools Community 57
mysql57-community/x86_64 MySQL 5.7 Community Server 247
看到如上,表示安装成功。
再次使用yum来安装mysql-server,就不会提示没有可用软件包。
二、使用yum install mysql-server安装
[root@localhost src]# yum install mysql-server
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.zju.edu.cn
* extras: centos.ustc.edu.cn
* updates: mirrors.zju.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 mysql-community-server.x86_64.0.5.7.21-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-common(x86-64) = 5.7.21-1.el7,它被软件包 mysql-community-server-5.7.21-1.el7.x86_64 需要
--> 正在处理依赖关系 mysql-community-client(x86-64) >= 5.7.9,它被软件包 mysql-community-server-5.7.21-1.el7.x86_64 需要
--> 正在检查事务
---> 软件包 mysql-community-client.x86_64.0.5.7.21-1.el7 将被 安装
--> 正在处理依赖关系 mysql-community-libs(x86-64) >= 5.7.9,它被软件包 mysql-community-client-5.7.21-1.el7.x86_64 需要
---> 软件包 mysql-community-common.x86_64.0.5.7.21-1.el7 将被 安装
--> 正在检查事务
---> 软件包 mysql-community-libs.x86_64.0.5.7.21-1.el7 将被 安装
--> 解决依赖关系完成
依赖关系解决
====================================================================================================
Package 架构 版本 源 大小
====================================================================================================
正在安装:
mysql-community-server x86_64 5.7.21-1.el7 mysql57-community 164 M
为依赖而安装:
mysql-community-client x86_64 5.7.21-1.el7 mysql57-community 24 M
mysql-community-common x86_64 5.7.21-1.el7 mysql57-community 272 k
mysql-community-libs x86_64 5.7.21-1.el7 mysql57-community 2.1 M
事务概要
====================================================================================================
安装 1 软件包 (+3 依赖软件包)
总下载量:190 M
安装大小:858 M
Is this ok [y/d/N]: y
Downloading packages:
警告:/var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-common-5.7.21-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
mysql-community-common-5.7.21-1.el7.x86_64.rpm 的公钥尚未安装
(1/4): mysql-community-common-5.7.21-1.el7.x86_64.rpm | 272 kB 00:00:02
(2/4): mysql-community-libs-5.7.21-1.el7.x86_64.rpm | 2.1 MB 00:00:02
(3/4): mysql-community-client-5.7.21-1.el7.x86_64.rpm | 24 MB 00:00:27
(4/4): mysql-community-server-5.7.21-1.el7.x86_64.rpm | 164 MB 00:03:25
----------------------------------------------------------------------------------------------------
总计 927 kB/s | 190 MB 00:03:30
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering "
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql57-community-release-el7-11.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
是否继续?[y/N]:y
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
警告:RPM 数据库已被非 yum 程序修改。
正在安装 : mysql-community-common-5.7.21-1.el7.x86_64 1/4
正在安装 : mysql-community-libs-5.7.21-1.el7.x86_64 2/4
正在安装 : mysql-community-client-5.7.21-1.el7.x86_64 3/4
正在安装 : mysql-community-server-5.7.21-1.el7.x86_64 4/4
验证中 : mysql-community-server-5.7.21-1.el7.x86_64 1/4
验证中 : mysql-community-common-5.7.21-1.el7.x86_64 2/4
验证中 : mysql-community-libs-5.7.21-1.el7.x86_64 3/4
验证中 : mysql-community-client-5.7.21-1.el7.x86_64 4/4
已安装:
mysql-community-server.x86_64 0:5.7.21-1.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.7.21-1.el7 mysql-community-common.x86_64 0:5.7.21-1.el7
mysql-community-libs.x86_64 0:5.7.21-1.el7
完毕!
查看版本信息:
[root@localhost src]# rpm -qi mysql-community-server
Name : mysql-community-server
Version : 5.7.21
Release : 1.el7
Architecture: x86_64
Install Date: 2018年02月11日 星期日 14时40分22秒
Group : Applications/Databases
Size : 775546923
License : Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Under GPLv2 license as shown in the Description field.
Signature : DSA/SHA1, 2017年12月28日 星期四 20时54分13秒, Key ID 8c718d3b5072e1f5
Source RPM : mysql-community-5.7.21-1.el7.src.rpm
Build Date : 2017年12月28日 星期四 12时11分13秒
Build Host : siv27.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
Description :
The MySQL(TM) software delivers a very fast, multi-threaded, multi-user,
and robust SQL (Structured Query Language) database server. MySQL Server
is intended for mission-critical, heavy-load production systems as well
as for embedding into mass-deployed software. MySQL is a trademark of
Oracle and/or its affiliates
The MySQL software has Dual Licensing, which means you can use the MySQL
software free of charge under the GNU General Public License
(http://www.gnu.org/licenses/). You can also purchase commercial MySQL
licenses from Oracle and/or its affiliates if you do not wish to be bound by the terms of
the GPL. See the chapter "Licensing and Support" in the manual for
further info.
The MySQL web site (http://www.mysql.com/) provides the latest news and
information about the MySQL software. Also please see the documentation
and the manual for more information.
This package includes the MySQL server binary as well as related utilities
to run and administer a MySQL server.
三、启动mysql-server
[root@localhost src]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
四、使用初始密码登陆
[root@localhost src]# cat /var/log/mysqld.log|grep 'A temporary password'
2018-02-11T06:47:54.773267Z 1 [Note] A temporary password is generated for root@localhost: 3=v/i;z/Y;P>
最后一行冒号后面的部分3=v/i;z/Y;P>就是初始密码。
使用此密码登录MySQL:
mysql -u root -p
[root@localhost src]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21
Copyright (c) 2000, 2018, 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>
五、修改初始密码
[root@localhost src]# mysqladmin -u root password 'new password'
出现错误信息:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
解决方法:
[root@localhost src]# systemctl stop mysqld.service
[root@localhost src]# mysqld --user=root --skip-grant-tables &
[root@localhost src]# mysql -u root mysql
mysql> UPDATE user SET authentication_string=PASSWORD('new password') where USER='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
重启mysql-server,用新密码登陆OK。
[root@localhost src]# systemctl start mysqld
[root@localhost src]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.21
Copyright (c) 2000, 2018, 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>
正常登陆后发现一个问题:
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决方法如下:
mysql> alter user 'root'@'localhost' identified by 'yourpassword';
Query OK, 0 rows affected (0.00 sec)
mysql> set password=password("yourpassword");
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql>
常用的命令:
systemctl start mysqld #启动mysqld
systemctl stop mysqld #停止mysqld
systemctl restart mysqld #重启mysqld
systemctl enable mysqld #设置开机启动
systemctl status mysqld #查看 MySQL Server 状态
直接使用匿名登录失败:
[dup@localhost ~]$ mysql
ERROR 1045 (28000): Access denied for user 'dup'@'localhost' (using password: NO)
这是因为5.7版本已经默认没有匿名用户了。我们看下安装MySQL 5.7.21后默认的用户和数据库
mysql> mysql> SELECT User, Host, HEX(authentication_string) FROM mysql.user;
+---------------+-----------+------------------------------------------------------------------------------------+
| User | Host | HEX(authentication_string) |
+---------------+-----------+------------------------------------------------------------------------------------+
| root | localhost | 2A38314635453231453335343037443838344136434434413733314145424642364146323039453142 |
| mysql.session | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
| mysql.sys | localhost | 2A5448495349534E4F544156414C494450415353574F52445448415443414E42455553454448455245 |
+---------------+-----------+------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
3 rows in set (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
关于默认的3个user的说明,详见官网:https://dev.mysql.com/doc/refman/5.7/en/reserved-users.html
One part of the MySQL installation process is data directory initialization (see Section 2.10.1.1, “Initializing the Data Directory Manually Using mysqld”). During data directory initialization, MySQL creates user accounts that should be considered reserved:
'root'@'localhost
: Used for administrative purposes. This account has all privileges and can perform any operation.
Strictly speaking, this account name is not reserved, in the sense that some installations rename the root
account to something else to avoid exposing a highly privileged account with a well-known name.
'mysql.sys'@'localhost'
: Used as the DEFINER
for sys
schema objects. Use of the mysql.sys
account avoids problems that occur if a DBA renames or removes the root
account. This account is locked so that it cannot be used for client connections.
'mysql.session'@'localhost'
: Used internally by plugins to access the server. This account is locked so that it cannot be used for client connections.
关于账号默认权限的说明,详见官网:https://dev.mysql.com/doc/refman/5.7/en/default-privileges.html