windows 安装配置mysql 8,以及远程连接访问
2018-12-04 本文已影响1人
程序鱼
官网下载mysql8,https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.11-winx64.zip
1.解压zip包到安装目录
比如我的安装目录是:D:\Program Files\MySQL
2.编写配置文件
在D:\Program Files\MySQL目录下创建my.ini文件
image.png
内容如下:
[mysqld]
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=D:\Program Files\MySQL
# 设置mysql数据库的数据的存放目录
datadir=D:\Program Files\MySQL\Data
# 允许最大连接数
max_connections=200
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8
注意,里面的 basedir 是我本地的安装目录,datadir 是我数据库数据文件要存放的位置,各项配置需要根据自己的环境进行配置。
使用default_authentication_plugin=mysql_native_password是为了后续navicat可以远程连接
2.使用管理员身份打开CMD窗口初始化安装
注意,一定要使用管理员身份
然后cd到D:\Program Files\MySQL\bin目录下
执行
mysqld --initialize --console
成功之后输入如下:
D:\Program Files\MySQL\bin>mysqld --initialize --console
2018-12-04T03:26:47.785247Z 0 [System] [MY-013169] [Server] D:\Program Files\MySQL\bin\mysqld.exe (mysqld 8.0.13) initializing of server in progress as process 7748
2018-12-04T03:26:47.922126Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2018-12-04T03:27:05.431061Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: _nv04Hp%DXuc
2018-12-04T03:27:19.755014Z 0 [System] [MY-013170] [Server] D:\Program Files\MySQL\bin\mysqld.exe (mysqld 8.0.13) initializing of server has completed
注意,里面有一句
A temporary password is generated for root@localhost: _nv04Hp%DXuc
这个_nv04Hp%DXuc就是mysql为我们生成的一个临时root密码,务必记住
如果忘记了,就把mysql目录删掉重头再来
3.安装mysql服务
mysqld --install mysql
这里的mysql可以改为你想要自定义的,比如你机器上有很好几个mysql,你可以起名为mysql8
安装成功后,输出是这样的:
D:\Program Files\MySQL\bin>mysqld --install mysql
Service successfully installed.
3.启动mysql服务
直接执行
net start mysql
4.登录连接mysql
在命令行输入:
mysql -u root -p
然后会提示你输入密码
这里的密码就是第2步让你记住的密码
登录成功后输出如下
D:\Program Files\MySQL\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.13
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.
可以执行下
show databases
检测下
4.修改root密码
连接进入后,直接输入下面语句,我这里是将默认密码设置为123456,你可以修改为自己想要定义的.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
4.允许远程连接访问
执行
use mysql;
然后查看下当前连接允许情况
select host, user, authentication_string, plugin from user;
我这里的输出如下:
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
4 rows in set (0.00 sec)
然后执行
CREATE USER 'root'@'%' IDENTIFIED BY 'root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER;
FLUSH PRIVILEGES;
这个时候再查看下权限,输出如下
mysql> select host, user, authentication_string, plugin from user;
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| host | user | authentication_string | plugin |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
| % | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | mysql_native_password |
| localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
| localhost | root | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 | mysql_native_password |
+-----------+------------------+------------------------------------------------------------------------+-----------------------+
5 rows in set (0.00 sec)