Centos7.x版本下针对Mysql的安装和使用多少跟之前的Centos6之前版本有所不同的,废话就不多赘述了,下面介绍下在centos7.x环境里安装mysql5.7的几种方法:
一、yum方式安装
从CentOS 7.0发布以来,yum源中开始使用Mariadb来代替MySQL的安装。即使你输入的是yum install -y mysql , 显示的也是Mariadb的安装内容。
使用源代码进行编译安装又太麻烦。因此,如果想使用yum安装MySQL的话,就需要去下载官方指定的yum源.
yum下载网址为: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.就可以下载到yum源了。
1)安装MySQL YUM资源库
[[email protected] ~] # yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
2)安装MySQL 5.7
[[email protected] ~] # yum install -y mysql-community-server
3)启动MySQL服务器和MySQL的自动启动
[[email protected] ~] # systemctl start mysqld.service
[[email protected] ~] # systemctl enable mysqld.service
4)密码问题
由于MySQL从5.7开始不允许首次安装后使用空密码进行登录!为了加强安全性,系统会随机生成一个密码以供管理员首次登录使用,
这个密码记录在 /var/log/mysqld .log文件中,使用下面的命令可以查看此密码:
[[email protected] ~] # cat /var/log/mysqld.log|grep ‘A temporary password‘
2018-01-24T02:32:20.210903Z 1 [Note] A temporary password is generated for [email protected]: DOqInortw9/<
最后一行冒号后面的部分DOqInortw9/<就是初始密码。
使用此密码登录MySQL:
[[email protected] ~] # mysql -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
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> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
有两种方法解决上面的报错(如下的123456是修改后的密码):
mysql> set password=password( "123456" );
或者
mysql> alter user ‘root‘ @ ‘localhost‘ identified by ‘123456‘ ;
刷新权限
mysql> flush privileges;
===============================================================================================
如果上面在执行 set password=password( "123456" );命令后出现下面的报错:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决办法:
这个与Mysql 密码安全策略validate_password_policy的值有关,validate_password_policy可以取0、1、2三个值:
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
默认的数值是1,符合长度,且必须含有数字,小写或大写字母,特殊字符。
所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
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)
修改上面两个参数后,就可以解决这个报错了。
=======================================================================================================
注意一点:
mysql5.7之后的数据库里mysql.user表里已经没有password这个字段了,password字段改成了authentication_string。
所以修改密码的命令如下:
mysql> update mysql.user set authentication_string=password( ‘[email protected]‘ ) 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>
=======================================================================================================
查看mysql版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.21 |
+-----------+
1 row in set (0.00 sec)
mysql>
=======================================================================================================
修改mysql5.7的编码由latin1为utf8
默认编码:
mysql> show variables like "%character%" ;show variables like "%collation%" ;
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | utf8_general_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set (0.01 sec)
调整操作:
[[email protected] ~] # cat /etc/my.cnf
......
[mysqld]
......
character- set -server=utf8 // 注意这个不能写成default-character- set =utf8,否则会导致5.7版本mysql无法打开
[client]
default-character- set =utf8
[[email protected]~] # systemctl restart mysqld.service
[[email protected]~] # mysql -p
......
mysql> show variables like "%character%" ;show variables like "%collation%" ;
+--------------------------+----------------------------+
| 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.01 sec)
+----------------------+-----------------+
| Variable_name | Value |
+----------------------+-----------------+
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
+----------------------+-----------------+
3 rows in set (0.00 sec)
mysql>
|