Linux下安装MySQL,环境是centos7,Ubuntu下的安装类似,只不是包管理命令是apt-get
而已。
内容的介绍分为上下两篇:最新版本MySQL8.0的详细安装配置(上),以及5.5和10.x版本的MariaDB安装介绍(下),分两部分介绍。
MySQL 8 目前最新稳定版为 8.0.26 ,官方表示 MySQL 8 比 MySQL 5.7 快 2 倍,包含大量的改进和更快的性能!
删除系统默认或以前安装的MySQL/MariaDB
首先将系统默认的MySQL,或者以前安装的其他版本的MySQL删除。
下面的方法也是正确卸载/删除MySQL的方法。
rpm删除MySQL
最简便的方式,使用 rpm -q
查询已经安装的mysql相关的软件,使用 rpm -e
卸载软件包。
- (1) 使用shell命令循环查询所有 mysql 名称相关的软件,并依次卸载:
[root@VM_0_15_centos ~]# for i in $(rpm -qa|grep mysql);do rpm -e $i --nodeps;done
rpm -qa
:-q
查询,-a
查询所有的软件包。
rpm -e
卸载包,-e
是--erase
的缩写(erase——擦除,清除);
--nodeps
表示忽略依赖关系,通常结合--force
一起使用,强制卸载。
- (2) 删除MariaDB。
CentOS 7.0以后,系统自带的数据库默认MariaDB。而MariaDB会和MySQL冲突。同样需要删除。
[root@VM_0_15_centos ~]# for i in $(rpm -qa|grep MariaDB);do rpm -e $i --nodeps;done
- 删除MySQL相关的目录(mysql软件目录和配置目录)【可选,不删除新的安装将使用旧的配置和数据库】
比起删除,更推荐重命名MySQL的软件和数据目录,见下一小节介绍。
[root@VM_0_15_centos ~]# rm -rf /var/lib/mysql && rm -rf /etc/my.cnf
非root用户执行时,需要使用 sudo 命令提升权限,如下:
for i in $(rpm -qa|grep mysql);do sudo rpm -e $i --nodeps;done 或 for i in $(rpm -qa|grep MariaDB);do sudo rpm -e $i --nodeps;done sudo rm -rf /var/lib/mysql && sudo rm -rf /etc/my.cnf
使用 yum 包管理命令删除
使用yum remove
等命令移除软件包就很方便。
- 使用
yum remove
删除:
为了删除所有的MySQL包及依赖,仍需要使用循环:
$ for i in $(rpm -qa|grep mysql);do sudo yum remove -y $i;done
或 for i in $(rpm -qa|grep MariaDB);do sudo yum remove -y $i;done
- 重命名mysql相关目录。
推荐重命名 /var/lib/mysql
软件数据目录 和 /etc/my.cnf
配置目录文件,作为一个备份。
$ sudo mv /var/lib/mysql /var/lib/mysql_old_backup
$ sudo mv /etc/my.cnf /etc/my.cnf_old_backup
yum 安装 Mysql8.0
安装过程总体分为三步:
- 配置MySQL的yum仓库地址
- 安装
- 初始化配置
两种方式配置yum仓库地址
对于 MySQL8.0 的 yum 仓库地址的配置,下面提供两种方式:
- 一种是MySQL官方提供的yum repo rpm包。
- 另一种是配置国内镜像源(以 清华 镜像为例)
使用官方提供的配置yum仓库的rpm包
等到官方的下载地址:repo.mysql.com//mysql80-co…
- 使用
yum localinstall
安装【推荐】
$ sudo yum localinstall https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
由于是直接指定rpm包地址,此处也可以使用
yum install
。
- 或先下载rpm包,然后使用
rpm -i
安装
如下,使用wget先下载。
$ wget -c https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
--2021-09-02 14:46:23-- https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm
Resolving repo.mysql.com (repo.mysql.com)... 23.72.33.24
Connecting to repo.mysql.com (repo.mysql.com)|23.72.33.24|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 26024 (25K) [application/x-redhat-package-manager]
Saving to: ‘mysql80-community-release-el7-3.noarch.rpm’
100%[======================================>] 26,024 116KB/s in 0.2s
2021-09-02 14:46:24 (116 KB/s) - ‘mysql80-community-release-el7-3.noarch.rpm’ saved [26024/26024]
使用rpm -i
安装(也可使用yum localinstall
)
$ sudo rpm -ivh mysql80-community-release-el7-3.noarch.rpm
warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:mysql80-community-release-el7-3 ################################# [100%]
- 查看安装
$ rpm -q mysql80-community-release
mysql80-community-release-el7-3.noarch
- 查看安装的 mysql-community 仓库源
使用清华镜像配置MySQL8.0仓库源
进入清华镜像的MySQL仓库地址:mirrors.tuna.tsinghua.edu.cn/mysql/yum/m…
可以找到对应的 mysql80-community-release-el7-3.noarch.rpm
包。
如下所示:
原本想演示 aliyun 的镜像源。但是 aliyun 现在的镜像源查找,真实是...一言难尽。
还是清华镜像的组织结构清晰,方便查找。比如上面截图,进入
mysql/yum/mysql80-community-el7/
路径后,查找release的rpm包,可以看到,只有三个 release 包。找到mysql80。
下图可以看出,进入清华镜像源地址后,可以很方便的依照要安装的软件名,一级级找到自己想要的包。
执行安装仓库源的包:
$ sudo yum install https://mirrors.tuna.tsinghua.edu.cn/mysql/yum/mysql80-community-el7/mysql80-community-release-el7-3.noarch.rpm
安装MySQL
安装mysql-community-server
安装好仓库源后,就可以安装了。
$ sudo yum install -y mysql-community-server
启动mysqld
$ sudo systemctl start mysqld
开机运行mysqld
$ sudo systemctl enable mysqld
查看mysqld状态
$ sudo systemctl status mysqld
配置MySQL
查看MySQL的临时密码
MySQL默认安装时,会为 root 用户生成一个临时密码,这个密码只能使用一次,在初次登陆后,必须重置密码。
临时密码位于log目录下的 /var/log/mysqld.log
文件中,使用关键字 'temporary password' 查找即可。
查看临时密码:
$ sudo grep 'temporary password' /var/log/mysqld.log
2021-09-03T09:11:28.993923Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: jus?>Y6kkd1d
mysql -p<临时密码>
登陆
如下,mysql -p
使用临时密码登陆。
注意 sudo 权限。
$ sudo mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.26
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> select version();
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
临时密码登陆后必须重置密码,否则无法执行任何语句。
通常使用mysql -uroot -p登陆,即指定用户名,默认不指定为root用户。
修改密码
修改 root 用户密码的语句为:ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxx';
密码策略验证
如下,由于安全策略的调整,默认无法为用户指定简单密码:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql>
严重不推荐使用简单密码!
修改 validate_password_policy 配置
validate_password是MySQL的一个插件(在MySQL5.6引入,后续随着版本自动安装)。
validate_password_policy则是用于判断修改的密码是否符合当前的策略。
- validate_password_policy的取值
Policy | Tests Performed |
---|---|
0 or LOW | Length |
1 or MEDIUM | Length; numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file |
修改密码策略为低级别,且长度要求最低为1(实际为4,最小值为4)。
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
完成密码修改
记得使用 flush
保存权限。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.03 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
还可以直接使用
SET PASSWORD = '123456';
设置密码:
mysql> SET PASSWORD = '123456'; Query OK, 0 rows affected (0.02 sec)
5.7.6版本以前的MySQL,可以使用
SET PASSWORD = PASSWORD('123456');
设置登陆用户的密码。
PASSWORD()
在MySQL8中不支持。
退出并使用新密码重新登录。
mysql> exit
Bye
[root_test@VM_0_15_centos ~]$ sudo mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.26 MySQL Community Server - GPL
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
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> select version();
+-----------+
| version() |
+-----------+
| 8.0.26 |
+-----------+
1 row in set (0.00 sec)
MySQL中几个信息查看设置
查看端口
mysql> show variables like 'port'; -- 或 show global variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
查看用户的授权信息
mysql> select host,user,authentication_string from mysql.user;
+-----------+------------------+------------------------------------------------------------------------+
| host | user | authentication_string |
+-----------+------------------+------------------------------------------------------------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
| localhost | root | $A$005$ l[QL*} _GW4qyR6aL6uR3/CYdIvRHPZA60sgS.Iwo5Sn9qzoiF.QBNQ8 |
+-----------+------------------+------------------------------------------------------------------------+
4 rows in set (0.00 sec)
注:MySQL5.6及以下的版本,查看用户的登录信息,可以使用
select host,user,password from mysql.user;
。在5.7之后,password 字段改成了 authentication_string 。
validate_password 参数查看和设置
通过 show variables like 'validate_password%';
查看密码验证的几个相关参数。
mysql> set global validate_password.policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password.length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'validate_password%';
+--------------------------------------+-------+
| Variable_name | Value |
+--------------------------------------+-------+
| validate_password.check_user_name | ON |
| validate_password.dictionary_file | |
| validate_password.length | 4 |
| validate_password.mixed_case_count | 1 |
| validate_password.number_count | 1 |
| validate_password.policy | LOW |
| validate_password.special_char_count | 1 |
+--------------------------------------+-------+
7 rows in set (0.00 sec)
如上,可以看到,设置 validate_password.length
为1,其值也会是4(最低长度)。
与 validate_password
相关的参数,都可以使用 set global validate_password.xxx
设置。
其中,validate_password.number_count —— 密码中数字的最小个数;validate_password.mixed_case_count —— 大小写的最小个数;validate_password.special_char_count —— 特殊字符的最小个数;dictionary_file 表示字典文件
作为全局变量,又可以使用
@@validate_password.length
查看。
mysql> select @@validate_password.length; +----------------------------+ | @@validate_password.length | +----------------------------+ | 4 | +----------------------------+ 1 row in set (0.00 sec)
MySQL8.0中不支持 PASSWORD 函数
MySQL8.0中已经不支持 PASSWORD 函数,因此无法使用 password('xxx') 设置密码。否则会报语法错误(ERROR 1064)。
MySQL5.7.5以后不再支持 PASSWORD 函数。
mysql> select password('123456');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('123456')' at line 1
mysql> SET PASSWORD = PASSWORD('123456');
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PASSWORD('123456')' at line 1
关于修改密码的坑【注意】
密码的设置,实际是更新的 user 表中的 authentication_string 字段或 password 字段(MySQL5.7.6以前)。
但是,千万不要使用 UPDATE 语句直接赋值 authentication_string 更新用户的密码:update user set authentication_string = 'xxx' where user='root';
,并执行 flush privileges;
刷新权限,这,将导致无法登陆MySQL。
因为,authentication_string 字段存储的是 sha2 加密后的字符串,直接赋值,将导致登录时验证,无法和加密后的密码对应。
但是,
可以使用 SET PASSWORD = 'xxx';
极其方便的更改当前用户密码。
也可以使用 ALTER USER 'root' IDENTIFIED BY 'xxx';
快速的修改指定用户的密码。
SET PASSWORD
中使用 FOR
关键字也可以指定用户:
SET PASSWORD FOR 'root'@'localhost' = 'xxx';
推荐及首选修改密码的语句是:ALTER USER 'user_name' IDENTIFIED BY 'xxx';
MySQL5.7.5及以前的版本,即 user 表中有 password 字段的版本,支持 password() 函数。则可以直接执行下面的 UPDATE 语句,更新密码:
update user set password = password('123456') where user='root';
附:安装指定版本的MySQL
查看可用的MySQL软件包
yum repolist all | grep mysql
可以看到,默认安装的版本为 mysql80-community
。
注:上面安装的
mysql80-community-release-el7-3.noarch.rpm
仓库源包,里面包含5.5、5.6、5.7版本的mysql-community-server。
安装指定版本的MySQL
如果想要安装 mysql57-community
版本,有两个办法:
一是修改 /etc/yum.repos.d/mysql-community.repo
配置文件,修改 enabled 参数【不推荐】。
二是,使用 yum-config-manager
命令配置。
-
使用
yum-config-manager --disable mysql80-community
取消 mysql80-community 的默认安装 -
使用
yum-config-manager --enable mysql57-community
设置 mysql57-community 成为yum的默认安装版本。
再次查看默认安装的版本:
此时,就可以安装 mysql57 版本的MySQL:
yum install -y mysql-community-server
远古时代(MySQL小于5.5版本),MySQL的安装包名为
mysql mysql-server
。如:
yum install mysql mysql-server
或yum remove mysql mysql-server
疑问:
如下,同样是使用
yum repolist all | grep mysql
查看mysql可用的包。不知为何,
!mysql80-community
多了感叹号。暂时不知其含义。可正常安装。