目录
安装环境
win10
下载zip安装包
下载地址:https://dev.mysql.com/downloads/file/?id=479669
进入页面,点击底部"No thanks, just start my download."。开始下载。
环境配置
解压zip包到安装目录
我的解压在C:\software\tools\mysql-8.0.12-winx64
配置环境变量
其中MYSQL_HOME=C:\software\tools\mysql-8.0.12-winx64
配置初始化的my.ini文件
我们发现解压后的目录并没有my.ini文件,没关系可以自行创建。在安装根目录下添加 my.ini(新建文本文件,将文件类型改为.ini),写入基本配置:(你创建了这个新文件之后,里面会有一些生成的内容,在复制下面的内容之前,要把这部分内容删除掉。并对下面的内容中的一些路径进行修改调整)
-
[mysqld]
-
#设置端口号
-
port=3306
-
#设置mysql的安装目录
-
#切记此处一定要用双斜杠\\
-
basedir=C:\\software\\tools\\mysql-8.0.12-winx64
-
#设置mysql数据库的数据存放目录
-
datadir=C:\\software\\tools\\mysql-8.0.12-winx64\\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
放入根目录中。
安装
初始化数据库
在安装时,必须以管理员身份运行cmd,否则在安装时会报错,会导致安装失败的情况。
在MySQL安装目录的 bin 目录下执行命令:
mysqld --initialize --console
执行完成后,会打印 root 用户的初始默认密码,比如:
-
C:\Windows\system32>cd C:\software\tools\mysql-8.0.12-winx64\bin
-
C:\software\tools\mysql-8.0.12-winx64\bin>mysqld --initialize --console
-
2018-08-23T13:56:18.046640Z 0 [System] [MY-013169] [Server] C:\software\tools\mysql-8.0.12-winx64\bin\mysqld.exe (mysqld 8.0.12) initializing of server in progress as process 11180
-
2018-08-23T13:56:23.403547Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 07+e+Cj.Us0P
-
2018-08-23T13:56:25.952809Z 0 [System] [MY-013170] [Server] C:\software\tools\mysql-8.0.12-winx64\bin\mysqld.exe (mysqld 8.0.12) initializing of server has completed
其中,初始密码为:07+e+Cj.Us0P。
安装服务
在MySQL安装目录的 bin 目录下执行命令:
mysqld --install [服务名]
后面的服务名可以不写,默认的名字为 mysql。当然,如果你的电脑上需要安装多个MySQL服务,就可以用不同的名字区分了,比如 mysql5 和 mysql8。
安装完成之后,就可以通过命令net start mysql启动MySQL的服务了。通过命令net stop mysql停止服务。通过命令sc delete MYSQL/mysqld -remove卸载MYSQL服务。
-
C:\software\tools\mysql-8.0.12-winx64\bin>net start mysql
-
mysql 服务正在启动 .
-
mysql 服务已经启动成功。
更改密码
在MySQL安装目录的 bin 目录下执行命令:
mysql -u root -p
这时候会提示输入密码,记住了上面安装时的初始密码,填入即可登录成功,进入MySQL命令模式。
-
C:\software\tools\mysql-8.0.12-winx64\bin>mysql -uroot -p
-
Enter password: ************
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 10
-
Server version: 8.0.12
-
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> alter user 'root'@'localhost' identified with mysql_native_password by 'root';
-
Query OK, 0 rows affected (0.10 sec)
-
mysql> exit;
-
Bye
-
C:\software\tools\mysql-8.0.12-winx64\bin>mysql -uroot -proot
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 11
-
Server version: 8.0.12 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>
在MySQL中执行命令:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
修改密码,注意命令尾的;一定要有 ; 这是mysql的语法。
配置远程登陆
管理员root的host是localhost,代表仅限localhost登录访问。如果要允许开放其他ip登录,则需要添加新的host。如果要允许所有ip访问,可以直接修改成“%”。
创建用户:
CREATE USER 'hadoop'@'%' IDENTIFIED WITH mysql_native_password BY 'hadoop';
#(需要注意:mysql8.0加密方式修改了)
#检查用户
-
mysql> select user, host, plugin, authentication_string from user;
-
+------------------+-----------+-----------------------+------------------------------------------------------------------------+
-
| user | host | plugin | authentication_string |
-
+------------------+-----------+-----------------------+------------------------------------------------------------------------+
-
| hadoop | % | mysql_native_password | *B34D36DA2C3ADBCCB80926618B9507F5689964B6 |
-
| mysql.infoschema | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
-
| mysql.session | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
-
| mysql.sys | localhost | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED |
-
| root | localhost | mysql_native_password | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
-
+------------------+-----------+-----------------------+------------------------------------------------------------------------+
授权远程数据库
#授权所有权限
GRANT ALL PRIVILEGES ON *.* TO 'hadoop'@'%';
#授权基本的查询修改权限,按需求设置
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON *.* TO 'hadoop'@'%';
查看用户权限
show grants for 'hadoop'@'%';
-
mysql> show grants for 'hadoop'@'%';
-
+------------------------------------+
-
| Grants for hadoop@% |
-
+------------------------------------+
-
| GRANT USAGE ON *.* TO `hadoop`@`%` |
-
+------------------------------------+
-
1 row in set (0.00 sec)
结束!