包括初始化root用户密码password的过程以及两个常见问题的解决方法
1. 下载MySQL zip包
进入[MySQL官网](http://dev.mysql.com/downloads/mysql)按需选择zip包下载并解压,
比如现在我电脑里面下载的是mysql-5.7.17-winx64
http://dev.mysql.com/downloads/mysql/
2. 编辑MySQL配置文件
打开解压的mySQL.zip包,在里面找到my-defalult.ini, 该文件是MySQL的默认配置文件
此处推荐大家复制一份,并改名my.ini
编辑my.ini,此处我只配置了端口,MySQL的安装目录以及MySQL数据库存放目录
> [mysqld]
> # 设置3306端口
> port = 3306
> # 设置MySQL的安装目录
> basedir=C:mysql-5.7.17-winx64mysql-5.7.17-winx64
> # 设置MySQL数据库的数据的存放目录
> datadir=C:mysql-5.7.17-winx64mysql-5.7.17-winx64data
3. 安装并配置MySQL服务
使用admin权限打开CMD运行窗口,进入到MySQL的bin目录下执行以下install命令
C:mysql-5.7.17-winx64mysql-5.7.17-winx64in>mysqld -install
Service successfully installed.
运行net start mysql命令打开MySQL服务
net start mysql
PS:问题1
描述:启动MySQL服务失败
C:mysql-5.7.17-winx64mysql-5.7.17-winx64in>net start mysql
The MySQL service is starting.
The MySQL service could not be started.
The service did not report an error.
More help is available by typing NET HELPMSG 3534.
解决方法:
通过一些网络上面的搜索得知,版本5.7以后在启动MySQL服务前,需要初始化bindata目录,
我的做法是:
- 创建bindata目录,如果存在删掉以前的
- 在刚刚的admin权限的运行窗口执行初始化命令,以此来生成没有密码的root用户:
C:mysql-5.7.17-winx64mysql-5.7.17-winx64in>mysqld --initialize-insecure
- 再次尝试打开MySQL服务,不出意外,会返回success:
C:mysql-5.7.17-winx64mysql-5.7.17-winx64in>net start mysql
The MySQL service is starting.
The MySQL service was started successfully.
检查MySQL服务已经打开
运行net start命令列出所有已经打开的windows服务,在输出中找到MySQL代表成功:
C:mysql-5.7.17-winx64mysql-5.7.17-winx64in>net start
These Windows services are started:
...
MySQL
...
4. 初始化root用户密码
进入MySQL
由于我们刚刚生成的root没有附带密码,所以可以通过以下命令免密码进入MySQL
mysql -u root
选择使用MySQL数据库
mysql> use mysql;
通过sql语句查看user表数据, 可以确定目前root无密码
mysql> select user, authentication_string from user;
+-----------+-------------------------------------------+
| user | authentication_string |
+-----------+-------------------------------------------+
| root | |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
为MySQL root用户初始化密码
mysql> update user set authentication_string=password('密码') where user='root';
Query OK, 1 row affected, 1 warning (0.02 sec)
Rows matched: 1 Changed: 1 Warnings: 1
PS:问题2
描述:使用以下命令初始化密码失败
mysql> update user set password=PASSWORD('密码') where user='root';
解决方法:
可以通过查看user表信息得知,在新版的MySQL的user表中,已经去除掉了password字段,
而用authentication_string替换,因此使用该的命令,会返回错误。
再次确认user表下面的root用户信息,可以看到现在root用户已经拥有了密码。
mysql> select user, authentication_string from user;
+-----------+-------------------------------------------+
| user | authentication_string |
+-----------+-------------------------------------------+
| root | *8B62E5775164CCBD6B3F9FFFC5ABCEFGHIGKLMNO |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
+-----------+-------------------------------------------+
2 rows in set (0.00 sec)
执行flush privileges命令使更改生效
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
退出MySQL
mysql> exit
Bye
使用root密码登陆MySQL
C:mysql-5.7.17-winx64mysql-5.7.17-winx64in>mysql -u root -p
Enter password: *********
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 5
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 2016, 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>