浏览器访问 https://dev.mysql.com/downloads/mysql/ 地址,根据自己操作系统选择 MySQL8 安装包。
将下载的 MySQL8 安装包解压到指定的目录。
在 MySQL8 主目录中创建 my.ini 文件,配置内容如下:
[mysqld] # 设置3306端口 port=3306 # 设置mysql的安装目录 basedir="D:/server/mysql-8.0.25-winx64" # 此处目录应该改为/反斜杠,根据自己解压目录设置 # 设置mysql数据库的数据的存放目录 datadir="D:/server/mysql-8.0.25-winx64/data" # 此处同上 # 设置导入文件目录 secure-file-priv="D:/server/mysql-8.0.25-winx64/infile" # 允许最大连接数 max_connections=200 # 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统 max_connect_errors=10 # 服务端使用的字符集默认为UTF8 character-set-server=UTF8MB4 # 创建新表时将使用的默认存储引擎 default-storage-engine=INNODB # 默认使用“mysql_native_password”插件认证 default_authentication_plugin=mysql_native_password [mysql] # 设置mysql客户端默认字符集 default-character-set=UTF8MB4 [client] # 设置mysql客户端连接服务端时默认使用的端口 port=3306 default-character-set=UTF8MB4
注意:
(1)MySQL8 默认采用 UTF8MB4 编码,UTF8 编码是 UTF8MB4 编码的别名。
(2)上面配置文件中 secure-file-priv 指定的目录必须手动创建,datadir 目录 MySQL8 将自动创建。
在 MySQL8 中执行如下命令初始化数据库:
mysqld --initialize --user=mysql --console
初始化输出信息如下:
2022-12-02T02:44:49.439700Z 0 [System] [MY-013169] [Server] D:\server\mysql-8.0.25-winx64\bin\mysqld (mysqld 8.0.25) initializing of server in progress as process 10420 2022-12-02T02:44:49.453913Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started. 2022-12-02T02:44:53.506196Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended. 2022-12-02T02:44:55.912371Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: GU7geBgdt9.6
注意:其中 GU7geBgdt9.6 为 root 用户的临时密码,登录数据库时必须修改 root 用户的密码。
执行 mysqld --install 命令将 MySQL8 安装成服务,默认名称为 mysql。但是,我们可以通过下面命令将服务名改成其他名称:
mysqld --install 服务名
安装命令:
D:\server\mysql-8.0.25-winx64\bin> mysqld --install mysql Service successfully installed. 请按任意键继续. . .
使用 net start mysql 命令启动服务,输出如下:
mysql8 服务正在启动 . mysql8 服务已经启动成功。 请按任意键继续. . .
使用 mysql -u -p 命令连接到 mysql,如下:
D:\server\mysql-8.0.25-winx64\bin>mysql -uroot -p Enter password: 这里输入临时密码
将 root 用户的密码修改为 aaaaaa,如下:
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'aaaaaa'; Query OK, 0 rows affected (0.01 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec)
为了安装和启动 MySQL8 时方便,特意将上面的命令写在 bat 脚本中,分别可以对 MySQL8 进行安装、卸载、启动、停止。
@echo off setlocal if "%OS%"=="Windows_NT" goto nt echo This script only works with NT-based versions of Windows. goto :eof :nt set "CUR_DIR=%~dp0" mkdir %CUR_DIR%infile echo init mysql %CUR_DIR%bin\mysqld --initialize --user=mysql --console if %errorlevel% == 1 goto end echo install MySql Server %CUR_DIR%bin\mysqld --install mysql8 --defaults-file=%CUR_DIR%my.ini :end pause
@echo off setlocal if "%OS%"=="Windows_NT" goto nt echo This script only works with NT-based versions of Windows. goto :eof :nt set "CUR_DIR=%~dp0" echo uninstall MySql Server %CUR_DIR%bin\mysqld --remove mysql8 pause
@echo off setlocal if "%OS%"=="Windows_NT" goto nt echo This script only works with NT-based versions of Windows. goto :eof :nt NET START mysql8 pause
@echo off setlocal if "%OS%"=="Windows_NT" goto nt echo This script only works with NT-based versions of Windows. goto :eof :nt NET STOP mysql8 pause