首页
学习
活动
专区
工具
TVP
发布

MySQL8让我们先登录它

1:MySql8的安装与使用

注意:以下命令都在MYSQL_HOME/bin目录下执行。

待设置了PATH=MYSQL_HOME/bin之后,就可以在任意的目录下执行以下命令了。

Mysql8的新特性:

https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-4.html

Mysql8的文档:

https://dev.mysql.com/doc/refman/8.0/en/

步1:解压并初始化数据库执行命令

mysqld --initialize

初始化完成以后,会在mysql解压的目录下,出现data目录。

在data目录下,找到:*.err的文件。打开,查看默认root用户的密码:

A temporary password is generated for root@localhost: a9&2uyvjfw,O

上面的:a9&2uyvjfw,O就是默认root用户的密码。

步2:启动mysql数据库[以测试的形式启动]

可以使用mysqld --console启动mysql数据库,等待都配置完成以后再安装成mysql的服务。

命令:

mysqld --console

现在启动mysql数据库。在测试状态下可以使用mysqld --console即在命令行启动。

D:\programfiles\mysql-8.0.11-winx64\bin>mysqld --console

2018-04-20T10:24:50.439301Z 0 [System] [MY-010116] [Server] D:\programfiles\mysq

l-8.0.11-winx64\bin\mysqld.exe (mysqld 8.0.11) starting as process 2420

2018-04-20T10:24:52.186504Z 0 [Warning] [MY-010068] [Server] CA certificate ca.p

em is self signed.

2018-04-20T10:24:52.233305Z 0 [System] [MY-010931] [Server] D:\programfiles\mysq

l-8.0.11-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.11' socket

: '' port: 3306 MySQL Community Server - GPL.

步3:登录mysql数据库

再次打开一个新的命令行窗口,然后输入:mysql -uroot -p登录:

mysql -uroot -p

Password:[请在这儿输入上面得到的mysql root用户的默认密码]

以下是登录过程:

D:\programfiles\mysql-8.0.11-winx64\bin>mysql -uroot -p

Enter password: ************

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.11

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.

步4:修改密码

默认的情况下,root用户使用临时密码登录,不能做任何的操作。 这一点与mysql5.7版本一样。所以,必须要先修改密码才可以使用mysql8。现在让我们来修改root用户的的密码:

mysql> alter user 'root'@'localhost' identified by '1234';

Query OK, 0 rows affected (0.12 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

在刷新权限以后,就可以执行一些操作了,如显示数据库列表:

mysql> show databases;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

| performance_schema |

| sys |

+--------------------+

4 rows in set (0.01 sec)

步5:创建一个新的用户并授权

注意:

在MySql5时,可以直接使用:

Grant all privileges on *.* to‘someBody’@’SomeIP’identified by‘somePassword’[with grant option];

的语法,直接创建一个新的用户且授权,但在mysql8中,上面的语句已经不再适用。

mysql8中必须要先创建一个用户,然后再进行授权,可以说是更加的规范了。

见官方说明:

https://dev.mysql.com/doc/refman/8.0/en/adding-users.html

以下是创建和授权的命令:[更请参考官网的示例]

mysql> create user 'wj'@'%' identified by '1234';

Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on *.* to 'wj'@'%' with grant option;

Query OK, 0 rows affected (0.11 sec)

步6:使用新创建的用户登录测试

>mysql -uwj -p -h192.168.2.104 -P3306

步7:安装成mysql的服务

在上面的测试都通过以后,现在就可以全部退出,再说一遍,全部退出。然后在MYSQL_HOME/bin目录下,执行mysqld --install,将mysql安装成服务。

D:\programfiles\mysql-8.0.11-winx64\bin>mysqld --install

Service successfully installed.

然后,使用net start启动mysql或是使用net stop关闭mysql的服务:

D:\programfiles\mysql-8.0.11-winx64\bin>net start mysql

MySQL服务正在启动.

MySQL服务已经启动成功。

启动成功以后,就可以关闭这个命令行窗口了。因为mysql已经做为服务启动了。

步8:现在配置mysql的环境变量

配置以下环境变量:

PATH=D:\programfiles\mysql-8.0.11-winx64\bin

测试登录:

C:\Users\Administrator>mysql -uroot -p

Enter password: ****

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 8

Server version: 8.0.11 MySQL Community Server - GPL

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;

+--------------------+

| Database |

+--------------------+

| information_schema |

| mysql |

更多新特性等我看完以下DOC以后再给大家一一总结。无论如何,学好SQL非常重要,无论是在垂直项目还是在大数据项目中,SQL都显得非常重要。

其他:

最主要的是mysql8登录的密码协议发生了变更,使用mysql命令行客户端完全可以登录,但是使用navicat或是sqlyog登录都不成功,见以下错误。显示的意思为“认证协议“错误。

见官方文档:

https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-4.html#mysqld-8-0-4-security

通过读取上面的文档我们可知:

1:mysql已经将之前的mysql_native_password认证,修改成了caching_sha2_password认证方式。

所以,使用类似于navicat或是sqlyog这些客户端时,默认使用还是mysql_native_password认证方式,所以即使输入正确的用户和密码依然登录不成功。

解决方式1:

在创建用户时,指定使用mysql_native_password认证方式:

mysql> create user 'Jack'@'%' identified with mysql_native_password by '1234';

Query OK, 0 rows affected (0.04 sec)

mysql> grant all privileges on *.* to 'Jack'@'%';

Query OK, 0 rows affected (0.13 sec)

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

官方示例:

CREATEUSER'nativeuser'@'localhost'IDENTIFIEDWITHmysql_native_passwordBY'password';

所以,为了更好的解决登录问题,可以以下处理方式:

创建一个root用户,密码认证方式为:mysql_native_password,且root用户可以在任意客户端登录,即root@%:

mysql> create user 'root'@'%' identified with mysql_native_password by '1234';

Query OK, 0 rows affected (0.11 sec)

给这个root@%进行授权:

mysql> grant all privileges on *.* to 'root'@'%' with grant option;

Query OK, 0 rows affected (0.06 sec)

刷新权限

mysql> flush privileges;

Query OK, 0 rows affected (0.00 sec)

删除root@localhost用户:

mysql> drop user 'root'@'localhost';

Query OK, 0 rows affected (0.06 sec)

刷新权限:

mysql> flush privileges;

Query OK, 0 rows affected (0.01 sec)

现在navicat就可以正常登录使用了:

新创建连接,输入用户名和密码:

查看数据库信息:

  • 发表于:
  • 原文链接http://kuaibao.qq.com/s/20180420G1W8MG00?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券