最近在我的Windows 10电脑上搭建MySQL数据库环境,没想到居然遇到了不少问题,特记录下来,希望给大家帮助,少走弯路。
下载MySQL Community Server
https://dev.mysql.com/downloads/mysql/
MySQL Community Server is the world's most popular open source database.
这个社区版是免费的,企业版是收费的
解压缩mysql-8.0.17-winx64.zip到本机目录:
C:\Users\erih\Tools\mysql-8.0.17-winx64
然后在系统变量添加:
右击“此电脑”-->“属性”-->“高级系统设置”-->选择“高级”选项卡,点击“环境变量”按钮
MYSQL_HOME: C:\Users\erih\Tools\mysql-8.0.17-winx64
PATH: %MYSQL_HOME%\bin
下载安装mysql workbench(mysql 的客户端工具)。免费的,文件只有40M。
https://dev.mysql.com/downloads/workbench/
然后打开cmd窗口,启动mysql报错:
C:\Users\erih>mysqld --console
2019-09-03T10:20:21.667069Z 0 [System] [MY-010116] [Server] C:\Users\erih\Tools\mysql-8.0.17-winx64\bin\mysqld.exe (mysqld 8.0.17) starting as process 42776
2019-09-03T10:20:21.678038Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Users\erih\Tools\mysql-8.0.17-winx64\data\mysqld_tmp_file_case_insensitive_test.lower-test
2019-09-03T10:20:21.678874Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Users\erih\Tools\mysql-8.0.17-winx64\data\mysqld_tmp_file_case_insensitive_test.lower-test
2019-09-03T10:20:21.679299Z 0 [ERROR] [MY-013276] [Server] Failed to set datadir to 'C:\Users\erih\Tools\mysql-8.0.17-winx64\data\' (OS errno: 2 - No such file or directory)
2019-09-03T10:20:21.701488Z 0 [ERROR] [MY-010119] [Server] Aborting
2019-09-03T10:20:21.701835Z 0 [System] [MY-010910] [Server] C:\Users\erih\Tools\mysql-8.0.17-winx64\bin\mysqld.exe: Shutdown complete (mysqld 8.0.17) MySQL Community Server - GPL.
把上面的错误信息网上搜索,找到了下面帖子解决问题。
https://blog.csdn.net/lx318/article/details/82686925
在mysql根目录下新建初始化配置文件“my-default.ini”。
C:\Users\erih\Tools\mysql-8.0.17-winx64\my-default.ini 文件内容如下:
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir= C:\\Users\\erih\\Tools\\mysql-8.0.17-winx64
# 设置mysql数据库的数据的存放目录
datadir= C:\\Users\\erih\\Tools\\mysql-8.0.17-winx64\\data
# 允许最大连接数
max_connections=20
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
在mysql根目录下新建目录:data
D:\Tools\mysql-8.0.22-winx64\data
以管理员身份运行“cmd”(命令提示符),进入mysql的bin文件目录下,然后执行下面命令。下面是过程:
C:\Users\erih\Tools\mysql-8.0.17-winx64\bin>mysqld -install
Service successfully installed.
C:\Users\erih\Tools\mysql-8.0.17-winx64\bin>mysqld --initialize
输入“mysqld --initialize”初始化mysql配置,随机生成root账户的密码,成功则无信息弹出,且mysql安装目录会生成data文件夹。
root账户的密码保存在data文件夹的一个后缀名为“.err”文件中。
C:\Users\erih\Tools\mysql-8.0.17-winx64\data\ERIH-CN.err 文件内容:
2019-09-03T10:27:47.954974Z 0 [System] [MY-013169] [Server] C:\Users\erih\Tools\mysql-8.0.17-winx64\bin\mysqld.exe (mysqld 8.0.17) initializing of server in progress as process 38812
2019-09-03T10:27:53.367115Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: OvyeqoxKC9)G
2019-09-03T10:27:56.009930Z 0 [System] [MY-013170] [Server] C:\Users\erih\Tools\mysql-8.0.17-winx64\bin\mysqld.exe (mysqld 8.0.17) initializing of server has completed
接下来输入“net start mysql”命令启动mysql服务
如果启动发生错误,则先手动删除这个目录里的所有文件:\mysql-8.0.22-winx64\data
然后在CMD窗口输入下面命令
mysqld --remove
再重新执行下面命令:
mysqld -install
mysqld --initialize
C:\Users\erih\Tools\mysql-8.0.17-winx64\bin>net start mysql
MySQL 服务正在启动 ..
MySQL 服务已经启动成功。
C:\Users\erih\Tools\mysql-8.0.17-winx64\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.17
Copyright (c) 2000, 2019, 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 respectiveowners.
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 '88888888';
Query OK, 0 rows affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
SHOW DATABASES可以显示所有的数据库,SHOW TABLES将会显示所有的数据表
然后运行Mysql Workbench,创建连接,配置信息如下:
Connection Method: Standard(TCP/IP)
Hostname:127.0.0.1
Port:3306
Username:root
Password:88888888
Default Schema:mysql
由于mysql-workbench-community需要VC++2019的环境,安装包也比较大,建议使用其他数据库给工具,比如下面这个:
https://www.navicat.com.cn/
这个是收费软件,建议采用其他免费的客户端工具也可以。