
1.前言
首先声明,自己虽然从长期来看,并不看好MySQL,但并不等于不碰它。实际上我使用的第一个真正的关系数据库就是MySQL。最早期版本是MySQL3的时代。使用的还是MyISAM引擎,用于网站上的数据存储以及全文检索(自己DIY处理)。自MySQL5.3以后的版本,就使用的越来越少了。再捡起来未免有些生疏。
有时候由于工作需要,不得不在Windows上去简单配置安装一份MySQL,仅用于做一些评估和验证。
本文就以MySQL5.7.X以及MySQL8.0.28的当前最新版本为例,来介绍一下windows上的解压安装。目的就是通过简单的配置,让系统能快速用起来。省去你徒手安装的时间。
2.分析实作
2.1、下载压缩包
国内用户,从sohu镜像或者阿里镜像都可以得到。推荐三个列表:
SOHU: https://mirrors.sohu.com/mysql/MySQL-8.0/
阿里:https://mirrors.aliyun.com/mysql/MySQL-8.0/
国外站点:https://mirrors.dotsrc.org/mysql/Downloads/MySQL-5.7/
因为有镜像,我们不必去Oracle官网使用帐号,去费劲下载了(除非你想用到最新的版本和特性,各取所需)
这里我们就下载一份:8.0.28式一下(目前2023.8.12为止,好像只能到这个版本。Oracle官网上已经到了8.0.33。)
取这个文件即可:https://mirrors.sohu.com/mysql/MySQL-8.0/mysql-8.0.28-winx64.zip
2.2、解压并配置
将下载的文件解压至:c:\mysql,得到目录结构:c:\mysql\mysql-8.0.28-winx64。后边您就可以拿大刀随便改造了。由于可能是在同一台机器上安装多个版本的MySQL,所以,就不设什么环境变量:MYSQL_HOME了。到时候运行的时候,自行进到相应的PATH即可。
在MySQL相应版本的根目录下边c:\mysql\mysql-8.0.28-winx64,创建它的配置文件。
典型的,尽量少的配置参数。
[mysql]
default-character-set=gbk
[mysqld]
character-set-server=utf8mb4
# collation_server=utf8mb4_general_ci
default_storage_engine=InnoDB
default-time-zone='+0:00'
basedir=c:\\mysql\\mysql-8.0.28-winx64
# datadir=c:\\mysql\\mysql-8.0.28-winx64\\data
innodb_data_home_dir=c:\\mysql\\mysql-8.0.28-winx64\\data
innodb_data_file_path=ibdata1:50M;ibdata2:10M:autoextend:max:2048M
transaction-isolation=READ-COMMITTED
port = 3306
max_allowed_packet = 64M
参数解释:
[mysql] 指的是作为客户端的连接配置参数,曾经也可以使用[client]。两者含义差不多相同。
[mysqld] 服务器端配置参数。
character-set-server, 指定的是utf8mb4,如果只是utf8,它默认给您弄成utf8mb3,它(utf8mb3)表达不了整个UTF8字符集。
default_storage_engine,默认存储引擎。这个需要指定。InnoDB用于事务处理的标配
default-time-zone,这个呢,似乎不配,有的时候会冒出错误。指定一下。
basedir, datadir,用于指定MySQL的根目录以及存储数据的目录。
innodb_data_file_path,这个参数是变化了的。很早以前叫: innodb_data_file。又多了一个:innodb_data_home_dir。定义的格式跟以前一样:
ibdata1:50M;ibdata2:10M:autoextend[:max:2048M]50M起步建一个数据文件。满了之后建一个10M的,然后空间不够,会自动扩展大小至最大值2048M,如果去掉
:max:2048M
,则会扩展至操作系统文件大小的上限。这里只是作为一个示例。如果这些个大小不指定,那么默认起始大小是12M并自动扩展。transaction-isolation: 事务隔离级,不多作解释
port:服务器运行的端口号
max_allowed_packet:网络包的大小。默认值是4M. 见:https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html ,对于些BLOB数据的传输之类,这个参数还是挺重要的。
数据库的初始化
刚解压完,data目录里头什么都没有。有一个系统内置的数据库mysql也没有。用户信息也没有建立起来。这些在过去都依赖于: mysql_install_db, https://dev.mysql.com/doc/refman/5.7/en/mysql-install-db.html 在5.7.6之后被废弃。
现在直接使用:mysqld来完成。
::: 使用administrator用户或身份来执行
cd /d c:\mysql\mysql-8.0.28-winx64\bin
mysqld --install-manual "mysql-8.0.28" --defaults-file="c:\mysql\mysql-8.0.28-winx64\my.ini"
mysqld --initialize-insecure --user=mysql --console
第一条命令,就是创建一个windows的服务:mysql-8.0.28。后边用起来方便。是manual方式启动。
上边使用初始化参数:--initialize-insecure,以允许不安全的用户登录方式是行登录。主要是为了后边的初始化方便。
初始化完成之后,试着用administrator方式启动服务:
net start mysql-8.0.28
在这之后,就基本上有一个雏形了。后边设置一下root用户的密码,并建一个简单的mydb以及相应的用户。
::: 使用root用户登录
mysql -u root
alter user 'root'@'localhost' identified with mysql_native_password by 'test123';
flush privileges;
create database mydb;
CREATE USER 'mydb'@'localhost' IDENTIFIED WITH mysql_native_password BY 'test123';
CREATE USER 'mydb'@'%' IDENTIFIED WITH mysql_native_password BY 'test123';
grant all on mydb.* to 'mydb'@'localhost','mydb'@'%';
flush privileges;
整个解压安装算是基本完成。
则MySQL-5.7.37的解压安装基本雷同,就是my.ini中的配置参数名,跟过去上期版本相比,变化还是不小的。不确定的地方,去读读文档。
上边的配置弄完之后:mysql -u mydb -p mydb
登录,测试简单验证。基本也没什么问题。
C:\mysql\mysql-8.0.28-winx64\bin>mysql -u mydb -p mydb
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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> create table t(id int, col2 varchar(32));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into t values(1, 'Zhao'), (2, 'Qian');
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t;
+------+------+
| id | col2 |
+------+------+
| 1 | Zhao |
| 2 | Qian |
+------+------+
2 rows in set (0.00 sec)
2.3、让下载及安装自动化
我从以前的老代码里翻了翻,发现可能有点用。将期打包整理在github上:https://github.com/iihero/MySQL_all 喜欢懒人式安装的筒子可以直接拿去用。
取出代码,命令行进到:mysql_install_auto_v2.0子目录,执行:
mysql_install.bat 5.7.37
或者
mysql_install.bat 8.0.28
最后会自动为你完成安装和初始化。
改变下载的镜像站点位置,您只需要修改: mysql_install.bat中下边的这一段:
if not exist %fullname%.zip (
wget https://mirrors.dotsrc.org/mysql/Downloads/MySQL-%version%/%fullname%.zip
:: wget https://mirrors.aliyun.com/mysql/MySQL-%version%/%fullname%.zip
:: wget https://mirrors.sohu.com/mysql/MySQL-%version%/%fullname%.zip
使用sohu的,就把第一个站点的那一行注掉,把sohu的那一行取消注释即可。
下边贴一个自动下载安装5.7.37版本的执行结果输出:
d:\iihero\MySQL_all\mysql_install_auto_v2.0>mysql_install.bat 5.7.37
ROOTDIR is not set, will be using default value.
The major version is: 5.7
The full version with arch is: 5.7.37-winx64
You will fetch "" ......
--2023-08-12 01:18:45-- https://mirrors.dotsrc.org/mysql/Downloads/MySQL-5.7/mysql-5.7.37-winx64.zip
Resolving mirrors.dotsrc.org (mirrors.dotsrc.org)... 130.225.254.116, 2001:878:346::116
Connecting to mirrors.dotsrc.org (mirrors.dotsrc.org)|130.225.254.116|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 387873841 (370M) [application/zip]
Saving to: 'mysql-5.7.37-winx64.zip'
mysql-5.7.37-winx64.zip 50%[================> ] 188.61M 52.9MB/s eta 4s
..............
Service successfully installed.
.........................................................................................
Finished creating service "mysql-5.7.37" for mysql-. Please check it in the service panel.
You can use net start mysql-5.7.37 to start the mysql service. Good luck.
If you want to delete the install, just do as below:
1. If you have started the service, just run: net stop mysql-5.7.37 to stop it.
2. sc delete mysql-5.7.37 to delete the service
3. Delete the whole directory of c:\mysql\mysql-5.7.37-winx64\
.........................................................................................
2023-08-12T00:19:16.033165Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2023-08-12T00:19:16.530698Z 0 [Warning] InnoDB: New log files created, LSN=45790
2023-08-12T00:19:16.601198Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2023-08-12T00:19:16.675456Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: df9284ee-38a5-11ee-837b-fa163eecc991.
2023-08-12T00:19:16.678954Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2023-08-12T00:19:20.224721Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2023-08-12T00:19:20.227596Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2023-08-12T00:19:20.232437Z 0 [Warning] CA certificate ca.pem is self signed.
2023-08-12T00:19:21.305659Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
start mysql-5.7.37 service and init the database and users.
The mysql-5.7.37 service is starting.
The mysql-5.7.37 service was started successfully.
"Let's sleep 3 seconds"
"Initialization finished. "
"The password of root and mydb are both test123, and please don't forget to modify if required!!!"
The installation finished for "mysql-5.7.37-winx64"....
最后设定的初始化密码是:test123, 两个用户:mydb以及root。并初始化了一个数据库mydb。
3.总结
自己DIY使用脚本解压并初始化安装的过程,实际上也是对一个软件包初始化使用进行了解的一个过程。适合于我这一类懒人。上边的wget我是从mingw64下边摘出来的。如果您的系统上不能单跑,也可以告诉我。我可以用其它软件工具来替换。
参考:
iihero's github for MySQL: https://github.com/iihero/MySQL_all
SOHU: https://mirrors.sohu.com/mysql/MySQL-8.0/
阿里:https://mirrors.aliyun.com/mysql/MySQL-8.0/
国外站点:https://mirrors.dotsrc.org/mysql/Downloads/MySQL-5.7/
MySQL文档:https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html





