centos7 安装 mysql 详解

/ 数据库 / 没有评论 / 526浏览

查看linux系统版本

[root@maruifu ~]# cat /etc/redhat-release   查看操作系统版本
CentOS Linux release 7.6.1810 (Core) 
[root@maruifu ~]# uname -r      查看系统内核版本
3.10.0-957.21.3.el7.x86_64 

下载

下载地址 : https://dev.mysql.com/downloads/mysql/

centos7 选择 Red Hat Enterprise Linux / Oracle Linux

版本就自己根据自己系统下载就好了

可以选择 RPM Bundle 使用wget 下载 mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

[root@nfs_client ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

也可以细化下载,下载须要的mysql组件,有4个:分别是 server、client、common、libs

卸载旧版本的MySql (没有的话,则跳过此步骤)

1、查看旧版本MySql rpm -qa | grep mysql 将会列出旧版本MySql的组件列表,如:

[root@maruifu ~]#  rpm -qa | grep mysql
mysql-community-libs-5.7.28-1.el7.x86_64
mysql-community-server-5.7.28-1.el7.x86_64
mysql-community-common-5.7.28-1.el7.x86_64
mysql-community-client-5.7.28-1.el7.x86_64

 2、逐个删除掉旧的组件       使用命令rpm -e --nodeps {-file-name}进行移除操作,移除的时候可能会有依赖,要注意一定的顺序。

rpm -e --nodeps mysql-community-libs-5.7.28-1.el7.x86_64

安装

解压

[root@maruifu tools]# pwd /usr/local/tools [root@maruifu tools]# tar xvf mysql-5.7.28-1.el7.x86_64.rpm-bundle.tar

卸载

centos7自带的mariadb-lib

  1. [root@maruifu tools]# rpm -qa|grep mariadb
  2. mariadb-libs-5.5.56-2.el7.x86_64
  3. [root@maruifu tools]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64 --nodeps

安装

这里我们只安装mysql-server服务,只需要安装如下4个软件包即可,

使用命令rpm -ivh {-file-name}进行安装操作。

**注:ivh中, i-install安装;v-verbose进度条;h-hash哈希校验**

按照依赖关系依次安装rpm包 依赖关系依次为common→libs→client→server

1.  rpm -ivh  mysql-community-common-5.7.28-1.el7.x86_64.rpm
2.  rpm -ivh  mysql-community-libs-5.7.28-1.el7.x86_64.rpm
3.  rpm -ivh  mysql-community-client-5.7.28-1.el7.x86_64.rpm
4.  rpm -ivh  mysql-community-server-5.7.28-1.el7.x86_64.rpm

特殊情况1

在阿里云ECS云服务器上安装mysql5.7,当安装 mysql-community-server-5.7.28-1.el7.x86_64.rpm 时报错,报错如下

[root@maruifu ~]# rpm -ivh mysql-community-server-5.7.28-1.el7.x86_64.rpm
warning: mysql-community-server-5.7.28-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
        libaio.so.1()(64bit) is needed by mysql-community-server-5.7.28-1.el7.x86_64
        libaio.so.1(LIBAIO_0.1)(64bit) is needed by mysql-community-server-5.7.28-1.el7.x86_64
        libaio.so.1(LIBAIO_0.4)(64bit) is needed by mysql-community-server-5.7.28-1.el7.x86_64
        

解决法案就是:安装libaio

[root@maruifu ~]# yum -y install libaio

安装libaio后,再重新安装一次mysql-community-server-5.7.28-1.el7.x86_64.rpm,此时就能正常安装了

特殊情况2

比如解决了“特殊情况1”,但在启动mysql的时候,启动不起来,或启动后,去查找临时密码,使用命令没反应。查看日志mysqld.log(可在/etc/my.cnf中查找到mysqld.log的配置位置),报如下错误,此时怎么解决?

[ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.user' doesn't exist

1>先通过rpm -e --nodeps xxx  卸载掉server,卸载后删除datadir目录,

2>卸载后查看 /etc/my.cnf 中,datadir的配置情况,将datedir目录删除,

3>最后通过命令rpm -ivh xxx 重新安装server,此时就能正常使用mysql了

命令代码如下:

[root@maruifu ~]# rpm -e --nodeps mysql-community-server-5.7.22-1.el7.x86_64

[root@maruifu ~]# cat /etc/my.cnf

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock


[root@maruifu ~]# cd /var/lib
[root@maruifu lib]# rm -rf mysql
[root@maruifu ~]# rpm -ivh mysql-community-server-5.7.22-1.el7.x86_64.rpm
[root@maruifu ~]# systemctl start mysqld.service
[root@maruifu ~]# ps -ef|grep mysql
mysql    21001     1  0 Nov22 ?        00:00:00 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
root     22218 20782  0 00:21 pts/1    00:00:00 grep --color=auto mysql
root     24317 31119  0 Nov21 pts/0    00:00:00 mysql -u root -p

登录并创建MySql密码

启动MySql

安装完后,使用命令 service mysqld start 或 systemctl start mysqld.service 启动MySQL服务。(如果mysql服务无法启动,就重启一下系统)

systemctl start mysqld.service    启动mysql
systemctl status mysqld.service  查看mysql状态
systemctl stop mysqld.service   关闭mysql

查看mysql进程 ps -ef|grep mysql
查看3306端口 netstat -anop|grep 3306

登陆mysql修改root密码

由于MySQL5.7.4之前的版本中默认是没有密码的,登录后直接回车就可以进入数据库,进而进行设置密码等操作。其后版本对密码等安全相关操作进行了一些改变,在安装过程中,会在安装日志中生成一个临时密码。

怎么找到这个临时密码呢?

grep 'temporary password' /var/log/mysqld.log

[root@maruifu ~]# grep 'temporary password' /var/log/mysqld.log
2019-11-21T08:07:27.278434Z 1 [Note] A temporary password is generated for root@localhost: rj3pj;g3XWIh

rj3pj;g3XWIh即为登录密码。使用这个随机密码登录进去,然后修改密码,使用命令:

mysql -uroot -p

执行下面的命令修改MySql root密码

set password for root@localhost=password('12345678'); 

在5.6后,mysql内置密码增强机制,低强度密码会报错:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

更改策略,设置 validate_password_policy=0;

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

修改密码

1. mysql> set password for root@localhost=password('TianTianIT12345');
2. Query OK, 0 rows affected, 1 warning (0.00 sec)

此时,虽然防火墙我时关着的,但root用户只能用于本机访问,不能用于远程访问,否则会报以下错误。因此,接下来要做的是授予root用户远程访问权限。

查看当前授予过的权限:

use mysql;
select user,host from user; 
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> select user,host from user;
+---------------+-----------+
| user          | host      |
+---------------+-----------+
| root          | %         |
| mysql.session | localhost |
| mysql.sys     | localhost |
| root          | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
 
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost                                           |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION        |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)

授予root用户远程访问权限:

mysql> grant all privileges on *.* to root@'%' identified by 'TianTianIT12345';
Query OK, 0 rows affected, 1 warning (0.05 sec)

刷新权限,使设置生效, OK。

mysql> flush privileges;
Query OK, 0 rows affected (0.36 sec)