腾讯云CentOS 7.6 安装MySQL 5.7

一、卸载MariaDB数据库

1、查询系统默认安装的MariaDB数据库

rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64

2、卸载系统安装的MariaDB数据库

rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64

二、安装MySQL

1、下载yum repo

CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件,官方地址:https://dev.mysql.com/downloads/repo/yum/

腾讯云CentOS 7.6 安装MySQL 5.7_第1张图片

腾讯云CentOS 7.6 安装MySQL 5.7_第2张图片
2、下载MySQL yum源

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

3、安装yum源

yum -y install mysql80-community-release-el7-3.noarch.rpm

4、查看yum源中的MySQL安装包

yum repolist all | grep mysql
[root@jeespring ~]# yum repolist all | grep mysql
mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community   disabled
mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled
mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community   disabled
mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled
mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community   disabled
mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled
mysql-connectors-community/x86_64  MySQL Connectors Community    enabled:    131
mysql-connectors-community-source  MySQL Connectors Community -  disabled
mysql-tools-community/x86_64       MySQL Tools Community         enabled:    100
mysql-tools-community-source       MySQL Tools Community - Sourc disabled
mysql-tools-preview/x86_64         MySQL Tools Preview           disabled
mysql-tools-preview-source         MySQL Tools Preview - Source  disabled
mysql55-community/x86_64           MySQL 5.5 Community Server    disabled
mysql55-community-source           MySQL 5.5 Community Server -  disabled
mysql56-community/x86_64           MySQL 5.6 Community Server    disabled
mysql56-community-source           MySQL 5.6 Community Server -  disabled
mysql57-community/x86_64           MySQL 5.7 Community Server    disabled
mysql57-community-source           MySQL 5.7 Community Server -  disabled
mysql80-community/x86_64           MySQL 8.0 Community Server    enabled:    145
mysql80-community-source           MySQL 8.0 Community Server -  disabled

yum源中默认启用的安装包版本为MySQL 8.0,如果安装MySQL 5.7,需要手动切换到 5.7

yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community

查看MySQL 5.7安装的相关依赖

[root@jeespring ~]# yum repolist enabled | grep mysql.*
mysql-connectors-community/x86_64    MySQL Connectors Community              131
mysql-tools-community/x86_64         MySQL Tools Community                   100
mysql57-community/x86_64             MySQL 5.7 Community Server              384
[root@jeespring ~]# 

三、安装MySQL服务器

1、安装MySQL服务

yum -y install mysql-community-server

MySQL的相关依赖就会列出来,MySQL版本和文件的 下载路径
腾讯云CentOS 7.6 安装MySQL 5.7_第3张图片
2、腾讯云下载MySQL依赖过慢解决方案(如果感觉下载太慢,可以参考下面解决方案,如果速度还行,可以略过此步骤。)
  通过yum安装MySQL的时候,mysql-community-server-5.7.28-1.el7.x86_64比较大,大约190M左右,腾讯云下载可能比较慢,可以通过下面网址进行手动下载:

MySQL软件下载:https://centos.pkgs.org/7/mysql-5.7-x86_64/2/

找到上面安装MySQL的版本:
mysql-community-server-5.7.28-1.el7.x86_64.rpm
腾讯云CentOS 7.6 安装MySQL 5.7_第4张图片
腾讯云CentOS 7.6 安装MySQL 5.7_第5张图片
文件下载完,上传到MySQL默认下载的目录(上面MySQL安装目录)

/var/cache/yum/x86_64/7/mysql57-community/packages/

这个目录可以在第一步图片里面找到
这个下载目录存储MySQL相关的下载软件,把下载完的软件上传到这个目录,将原来的替换即可。

[root@jeespring packages]# ll /var/cache/yum/x86_64/7/mysql57-community/packages/
total 94664
-rw-r--r-- 1 root root 45109364 Oct 11 00:06 mysql-community-client-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 root root   318768 Oct 11 00:06 mysql-community-common-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 root root  4374364 Oct 11 00:07 mysql-community-libs-5.7.28-1.el7.x86_64.rpm
-rw-r--r-- 1 root root 47112642 Nov 15 10:01 mysql-community-server-5.7.28-1.el7.x86_64.rpm
[root@jeespring packages]# 

覆盖腾讯云未下载完成的mysql-community-server-5.7.28-1.el7.x86_64.rpm
腾讯云CentOS 7.6 安装MySQL 5.7_第6张图片
替换完成后,重新执行安装命令

yum -y install mysql-community-server

安装成功Complete!
腾讯云CentOS 7.6 安装MySQL 5.7_第7张图片
3、启动MySQL服务

systemctl start  mysqld.service

运行一下命令查看一下运行状态

systemctl status mysqld.service

四、初始化数据库密码

1、登录MySQL
第一次安装完没有设置密码,所以只能使用MySQL安装时生成的密码进行登录,查看MySQL安装时的初始密码

grep "password" /var/log/mysqld.log
[root@jeespring ~]# grep "password" /var/log/mysqld.log
2019-11-15T02:44:18.428256Z 1 [Note] A temporary password is generated for root@localhost: Bg//CX=fB7#%
[root@jeespring ~]# 

复制临时密码,登录MySQL

mysql -uroot -p

2、修改MySQL初始密码

ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

修改密码报错提示:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

如果仅仅修改简单密码,可以依次执行以下操作步骤:

set global validate_password_policy=0;
set global validate_password_length=1;

然后进行密码更改

ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

至此密码修改完成,可以使用新密码登录MySQL服务器。

3、MySQLvalidate_password_policy策略介绍,不感兴趣可以直接略过

MySQL默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。修改密码时新密码是否符合当前的策略,不满足则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误

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

0 or LOW :校验级别最低,只校验密码长度,只要长度跟validate_password_length一样即可,默认长度是8位。
1 or MEDIUM:满足validate_password_policy=0,并且必须含有数字,小写或大写字母,特殊字符。
2 or STRONG:满足0,1的要求,且密码中任意连续4个(或4个让上)字符不得是字典中的单词

修改validate_password_policy

set global validate_password_policy=0;

查询修改后的validate_password_policy

mysql> select @@validate_password_policy;
+----------------------------+
| @@validate_password_policy |
+----------------------------+
| LOW                        |
+----------------------------+
1 row in set (0.00 sec)

判断密码的长度由validate_password_length参数来决定。
查看validate_password_length的值

mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
|                          8 |
+----------------------------+
1 row in set (0.00 sec)

validate_password_length参数默认为8,它有最小值的限制,最小值为:
validate_password_number_count

  • validate_password_special_char_count
  • validate_password_mixed_case_count * 2

不管设置 validate_password_length=1,还是2,3,4 ,密码长度实际值都是4。超过4后为设置的值。

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

mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
|                          4 |
+----------------------------+
1 row in set (0.00 sec)

mysql5.7.x中密码控制的变量

Policy Tests Performed 默认值 说明
1 validate_password_policy MEDIUM 用于控制validate_password的验证策略
2 validate_password_length 8 密码长度的最小值(最小值4)
3 validate_password_number_count 1 密码中数字最小个数
4 validate_password_mixed_case_count 1 大小写字符最小个数
5 validate_password_special_char_count 1 特殊字符最小个数
6 validate_password_dictionary_file 字典文件

五、数据库授权

数据库没有授权,只支持localhost本地访问

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

mysql>FLUSH PRIVILEGES;

六、设置自动启动

systemctl enable mysqld

systemctl daemon-reload

七、修改MySQL字符编码

1、查看MySQL字符集

mysql> show variables like '%char%';
+--------------------------------------+----------------------------+
| Variable_name                        | Value                      |
+--------------------------------------+----------------------------+
| character_set_client                 | utf8                       |
| character_set_connection             | utf8                       |
| character_set_database               | latin1                     |
| character_set_filesystem             | binary                     |
| character_set_results                | utf8                       |
| character_set_server                 | latin1                     |
| character_set_system                 | utf8                       |
| character_sets_dir                   | /usr/share/mysql/charsets/ |
| validate_password_special_char_count | 1                          |
+--------------------------------------+----------------------------+
9 rows in set (0.00 sec)

2、修改MySQL字符集

修改MySQL的配置文件my.cnf,文件默认位置:/etc/my.cnf

vi /etc/my.cnf

在[mysqld]结束位置添加:character_set_server=utf8

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

character_set_server=utf8

3、重新启动mysql服务。
停止命令:

systemctl stop mysqld.service

启动命令:

systemctl start mysqld.service

查询修改后的MySQL字符集

mysql> show variables like 'character_set_%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

你可能感兴趣的:(MySQL)