CentOS 6.5/6.6 安装mysql 5.7教程及遇到错误的解决方案

CentOS 6.5/6.6 安装mysql 5.7教程及遇到错误的解决方案

1、检测系统是否自带安装mysql

#yum list installed | grep mysql

2、删除系统自带的MySQL及其依赖(防止后面安装发生冲突)

#yum -y remove mysql-libs.x86_64

3、给CentOS添加rpm源,并且选择较新的源

#wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm

4、安装第一步下载的rpm文件

#yum install mysql-community-release-el6-5.noarch.rpm

安装成功后,我们可以看到/etc/yum.repos.d/目录下增加了以下两个文件

查看mysql57的安装源是否可用,如不可用请自行修改配置文件(/etc/yum.repos.d/mysql-community.repo)使mysql57下面的enable=1

#yum repolist enabled | grep mysql

5、使用yum安装MySQL

#yum install mysql-community-server

6、启动mysql服务

#service mysqld start

===================================================
报错如下:
Initializing MySQL database: 2020-10-13T23:12:30.647106Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-13T23:12:30.648621Z 0 [ERROR] –initialize specified but the data directory has files in it. Aborting.
2020-10-13T23:12:30.648656Z 0 [ERROR] Aborting

解决方案:
查看MySQL配置文件,知道data文件在哪
#vim /etc/my.cnf


移除并备份data文件,重新启动MySQL
#mv mysql mysql.bak
#mysqld --initialize


继续错误:
重新启动MySQL后,报错如上,查看datadir发现mysql目录又重新回来了
解决方案:
修改配置文件中的datadir
#vim /etc/my.cnf

新建/var/lib/mysql/data目录,启动MySQL


继续错误如下:
Initializing MySQL database: 2020-10-14T00:59:34.955730Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-14T00:59:34.957918Z 0 [ERROR] Can’t change data directory owner to mysql
2020-10-14T00:59:34.957944Z 0 [ERROR] Aborting

解决方案:
关闭防火墙:
#setenforce 0
重新启动,终于成功了

===================================================

7、查看mysql是否自启动,并且设置开启自启动

#chkconfig --list | grep mysqld (0 1 2 3 4 5 6 代表centOS启动状态)
#chkconfig mysqld on

8、修改字符集为UTF-8

#vim /etc/my.cnf
在[mysqld]部分添加:
character-set-server=utf8
在文件末尾新增[client]段,并在[client]段添加:
default-character-set=utf8

#service mysqld restart

9、修改默认配置

#mysql_secure_installation

第一个空为初始密码,用以下冒号后的任一个都可
#grep ‘temporary password’ /var/log/mysqld.log
密码不安全,重新输入

之后除了远程登陆,都Y

10、授权远程登陆

将权限改为ALL PRIVILEGES
#mysql –u root -p

mysql> use mysql;
mysql> flush privileges; #立即生效 (刷新MySQL的系统权限相关表)

mysql> select host,user,authentication_string from user; #此处authentication_string相当于password( 5.7版本下的mysql数据库下已经没有password这个字段)

这样机器就可以以用户名root密码root远程访问该机器上的MySql.
• 实现远程连接(改表法)
use mysql;
update user set host = ‘%’ where user = ‘root’;
fiush privileges
这样在远端就可以通过root用户访问Mysql.

11、关闭防火墙

关闭iptables
#/etc/init.d/iptables stop
永久关闭
#chkconfig iptables off
检查
#chkconfig --list iptables

12、CMD测试连接

mysql -h 192.168.88.147 -u root -p

===================================================
报错如下:
ERROR 1130 (HY000): Host ‘192.168.88.141’ is not allowed to connect to this MySQL server
本机不允许连接到MySQL服务器上
解决方案1:改表法
在MySQL服务器端进行配置
登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从"localhost"改成"%"
……>mysql -u root -p
mysql> use mysql;
mysql> update user set host = ‘%’ where user = ‘root’;

出现报错,用以下命令查看,有%存在,证明成功:
mysql> select host, user from user where user = ‘root’;

mysql> flush privileges;
重启MySQL服务,重新进行远程登录,成功

但此方法可以让任意MySQL客户端登录,会有安全风险


解决方案2:授权法
允许用户user1从ip为192.168.88.141的主机连接到mysql服务器,并使用user1@PASS作为密码,命令如下:
mysql>CREATE USER ‘user1’@‘host’ IDENTIFIED BY ‘user1@PASS’; //创建用户user1
mysql>GRANT ALL PRIVILEGES ON . TO ‘user1’@‘192.168.88.141’ IDENTIFIED BY ‘user1@PASS’ WITH GRANT OPTION;
mysql> flush privileges;
mysql> select host,user from user where user=‘user1’;

远程登录验证:

===================================================

13、报错

第二天重启MySQL服务,显示报错

1)报错如下(同安装过程报错):

–initialize specified but the data directory has files in it. Aborting.
解决方案:
将data下的文件删除,关闭防火墙,重启服务,

2) 报错如下:

ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
解决方案:
在配置文件下加跳过授权,重启MySQL服务
#vi /etc/my.cnf

登录后修改root的密码
mysql> use mysql;
mysql> update user set authentication_string=password(“你的新密码”) where user=“root”;
mysql> flush privileges;
mysql> quit
修改完成后,将之前给配置文件添加的” skip-grant-tables”命令删掉

3) 报错如下:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决方案:
mysql> alter user ‘root’@‘localhost’ identified by ‘1234@qweR’;



参考链接:安装

CentOS 6.5/6.6 安装mysql 5.7教程及遇到错误的解决方案

CentOS 6.5/6.6 安装mysql 5.7教程及遇到错误的解决方案

1、检测系统是否自带安装mysql

#yum list installed | grep mysql

2、删除系统自带的MySQL及其依赖(防止后面安装发生冲突)

#yum -y remove mysql-libs.x86_64

3、给CentOS添加rpm源,并且选择较新的源

#wget dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm

4、安装第一步下载的rpm文件

#yum install mysql-community-release-el6-5.noarch.rpm

安装成功后,我们可以看到/etc/yum.repos.d/目录下增加了以下两个文件

查看mysql57的安装源是否可用,如不可用请自行修改配置文件(/etc/yum.repos.d/mysql-community.repo)使mysql57下面的enable=1

#yum repolist enabled | grep mysql

5、使用yum安装MySQL

#yum install mysql-community-server

6、启动mysql服务

#service mysqld start

===================================================
报错如下:
Initializing MySQL database: 2020-10-13T23:12:30.647106Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-13T23:12:30.648621Z 0 [ERROR] –initialize specified but the data directory has files in it. Aborting.
2020-10-13T23:12:30.648656Z 0 [ERROR] Aborting

解决方案:
查看MySQL配置文件,知道data文件在哪
#vim /etc/my.cnf


移除并备份data文件,重新启动MySQL
#mv mysql mysql.bak
#mysqld --initialize


继续错误:
重新启动MySQL后,报错如上,查看datadir发现mysql目录又重新回来了
解决方案:
修改配置文件中的datadir
#vim /etc/my.cnf

新建/var/lib/mysql/data目录,启动MySQL


继续错误如下:
Initializing MySQL database: 2020-10-14T00:59:34.955730Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-10-14T00:59:34.957918Z 0 [ERROR] Can’t change data directory owner to mysql
2020-10-14T00:59:34.957944Z 0 [ERROR] Aborting

解决方案:
关闭防火墙:
#setenforce 0
重新启动,终于成功了

===================================================

7、查看mysql是否自启动,并且设置开启自启动

#chkconfig --list | grep mysqld (0 1 2 3 4 5 6 代表centOS启动状态)
#chkconfig mysqld on

8、修改字符集为UTF-8

#vim /etc/my.cnf
在[mysqld]部分添加:
character-set-server=utf8
在文件末尾新增[client]段,并在[client]段添加:
default-character-set=utf8

#service mysqld restart

9、修改默认配置

#mysql_secure_installation

第一个空为初始密码,用以下冒号后的任一个都可
#grep ‘temporary password’ /var/log/mysqld.log
密码不安全,重新输入

之后除了远程登陆,都Y

10、授权远程登陆

将权限改为ALL PRIVILEGES
#mysql –u root -p

mysql> use mysql;
mysql> flush privileges; #立即生效 (刷新MySQL的系统权限相关表)

mysql> select host,user,authentication_string from user; #此处authentication_string相当于password( 5.7版本下的mysql数据库下已经没有password这个字段)

这样机器就可以以用户名root密码root远程访问该机器上的MySql.
• 实现远程连接(改表法)
use mysql;
update user set host = ‘%’ where user = ‘root’;
fiush privileges
这样在远端就可以通过root用户访问Mysql.

11、关闭防火墙

关闭iptables
#/etc/init.d/iptables stop
永久关闭
#chkconfig iptables off
检查
#chkconfig --list iptables

12、CMD测试连接

mysql -h 192.168.88.147 -u root -p

===================================================
报错如下:
ERROR 1130 (HY000): Host ‘192.168.88.141’ is not allowed to connect to this MySQL server
本机不允许连接到MySQL服务器上
解决方案1:改表法
在MySQL服务器端进行配置
登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从"localhost"改成"%"
……>mysql -u root -p
mysql> use mysql;
mysql> update user set host = ‘%’ where user = ‘root’;

出现报错,用以下命令查看,有%存在,证明成功:
mysql> select host, user from user where user = ‘root’;

mysql> flush privileges;
重启MySQL服务,重新进行远程登录,成功

但此方法可以让任意MySQL客户端登录,会有安全风险


解决方案2:授权法
允许用户user1从ip为192.168.88.141的主机连接到mysql服务器,并使用user1@PASS作为密码,命令如下:
mysql>CREATE USER ‘user1’@‘host’ IDENTIFIED BY ‘user1@PASS’; //创建用户user1
mysql>GRANT ALL PRIVILEGES ON . TO ‘user1’@‘192.168.88.141’ IDENTIFIED BY ‘user1@PASS’ WITH GRANT OPTION;
mysql> flush privileges;
mysql> select host,user from user where user=‘user1’;

远程登录验证:

===================================================

13、报错

第二天重启MySQL服务,显示报错

1)报错如下(同安装过程报错):

–initialize specified but the data directory has files in it. Aborting.
解决方案:
将data下的文件删除,关闭防火墙,重启服务,

2) 报错如下:

ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
解决方案:
在配置文件下加跳过授权,重启MySQL服务
#vi /etc/my.cnf

登录后修改root的密码
mysql> use mysql;
mysql> update user set authentication_string=password(“你的新密码”) where user=“root”;
mysql> flush privileges;
mysql> quit
修改完成后,将之前给配置文件添加的” skip-grant-tables”命令删掉

3) 报错如下:

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
解决方案:
mysql> alter user ‘root’@‘localhost’ identified by ‘1234@qweR’;



参考链接:安装