CentOS7.0下安装mysql5.7的步骤以及遇到的问题!

由于Mysql被Oracle收购,所以从Centos7开始不再默认安装Mysql而用Mariadb代替,需要使用一些方法才能正确安装Mysql

1.首先卸载Mariadb,否则安装Mysql会产生冲突

rpm –qa | grep mariadb #查看mariadb安装包
rpm -e --nodeps mariadb-libs-xxxxxxxxx.x86_64 #卸载mariadb

2.安装mysql-release
   我是从官网下载的rpm包(GPL版本的),很小。然后安装:

rpm -ivh mysql-community-release-xxxxxxxxxx.rpm

3.配置Mysql环境安装mysql-server、mysqlclient以及依赖包:

yum install mysql mysql-server mysql mysql-devel
systemctl start mysqld.service #启动mysql服务
systemctl enable mysqld.service #开机启动mysql服务

那么问题来了!!!

mysql -uroot -p 这个时候报错
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

 什么原因呢? 
  

原来Mysql 5.6及以后版本出处于安全考虑,root密码已经不为空了

Now that the password MySQL had generated is expired, the problem is reduced to getting this password to work again 
(1) or generate a new one 
(2). This can be accomplished by running MySQL with the skip-grant-tables option which would make it ignore the access rights:

怎么解决呢?

我在网上艰辛的收索终于找到了解决方法,后来又找到一种,先说后者

1.为了加强安全性,MySQL5.7为root用户随机生成了一个密码,在error log中,关于error log的位置,如果安装的是RPM包,则默认是/var/log/mysqld.log

用vi命令打开如图,在文件开头前几行我就找到了临时密码,拷贝过就能登录进去了。

CentOS7.0下安装mysql5.7的步骤以及遇到的问题!_第1张图片

登录进去之后,我输入sql语句执行时又报了一个错误:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

如果只是修改为一个简单的密码,会报以下错误:

mysql>  ALTER USER USER() IDENTIFIED BY '12345678';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

参考网址:http://www.linuxidc.com/Linux/2016-01/127831.htm

这个其实与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

有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。

必须修改两个全局参数:

首先,修改validate_password_policy参数的值

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
这样,判断密码的标准就基于密码的长度了。这个由validate_password_length参数来决定

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

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

最后就可以成功设置密码了:

mysql> grant all privileges on zabbix.*to 'admin'@'localhost' identified by '123456'; 
Query OK, 0 rows affected, 1 warning (0.01 sec)

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


2.修改/etc目录下的配置文件my.cnf

 1)service mysqld stop

 2)在my.cnf文件的末尾添加skip-grant-tables,保存退出

 3)service mysqld start

 4)mysql -u root -p直接回车进入mysql

 5)修改root密码Type

 UPDATE user SET password_expired = 'N' WHERE User = 'root'; 

   to let MySQL know the password is not expired (1) or

 UPDATE user SET authentication_string = PASSWORD('YourNewPassword'), password_expired = 'N' WHERE User = 'root';

  6)将第二步中添加的代码注释掉,然后重启mysqld,你就可以用root的新密码登录了

你可能感兴趣的:(Linux)