MySQL 绿色版本搭建

46 阅读3分钟

MySQL 绿色版本搭建

下载

downloads.mysql.com/archives/co…

服务启动

  1. 环境变量

    为了方便使用,你可以将 MySQL 的 bin 目录添加到系统的环境变量中。具体做法是: 右键点击“此电脑”或“我的电脑”,选择“属性”。 点击左侧的“高级系统设置”,然后在弹出的窗口中点击“环境变量”。 在“系统变量”部分找到 Path 变量,选择后点击编辑。 添加 MySQL 的 bin 目录路径(例如 C:\mysql-5.7.44-winx64\bin),并保存更改。

  2. 初始化数据库

    首次运行时,你需要初始化数据库以生成必要的数据文件和目录结构。打开命令提示符(管理员身份运行),然后执行以下命令:

    mysqld --initialize --console
    
    2025-03-11T06:29:00.286858Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2025-03-11T06:29:00.567410Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2025-03-11T06:29:00.594377Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2025-03-11T06:29:00.664347Z 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: 1e9b5e40-fe42-11ef-963f-8447092466cb.
    2025-03-11T06:29:00.669297Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
    2025-03-11T06:29:01.136227Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
    2025-03-11T06:29:01.136916Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
    2025-03-11T06:29:01.139391Z 0 [Warning] CA certificate ca.pem is self signed.
    2025-03-11T06:29:01.245429Z 1 [Note] A temporary password is generated for root@localhost: itbsfCT2)bsl
    

安装服务

将 MySQL 作为 Windows 服务进行安装以便于管理

  1. 配置文件

    在 mysql 根目录创建文件 my.ini,内容如下。

    # 设置MySQL安装目录
    [client]
    port=3306
    default-character-set=utf8
    
    # 设置MySQL服务端的配置
    [mysqld]
    basedir=D:/program/mysql-5.7.44-winx64
    datadir=D:/program/mysql-5.7.44-winx64/data/
    port=3306
    character-set-server=utf8
    default-storage-engine=INNODB
    
    # 允许最大连接数
    max_connections=200
    
    # 服务端字符集
    character-set-server=utf8
    
    # 设置跳过权限表(仅用于测试环境,生产环境中请确保此行被注释或删除)
    #skip-grant-tables
    
    # InnoDB相关配置
    innodb_buffer_pool_size = 128M
    innodb_log_file_size = 48M
    
    # 日志设置
    log-error=D:/program/mysql-5.7.44-winx64/data/mysqld.err
    general-log=0
    general_log_file=D:/program/mysql-5.7.44-winx64/data/general.log
    slow_query_log=1
    slow_query_log_file=D:/program/mysql-5.7.44-winx64/data/slow.log
    
    # 其他配置
    sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
    
  2. 安装 MySQL 服务

    mysqld --install MySQL57 --defaults-file=D:\program\mysql-5.7.44-winx64\my.ini
    
    Service successfully installed.
    
  3. 启动 MySQL 服务

    net start MySQL57
    
    MySQL57 服务正在启动 .
    MySQL57 服务已经启动成功。
    
  4. 登录 MySQL

    mysql -u root -p
    
    Enter password: ************
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.44-log
    
    Copyright (c) 2000, 2023, 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>
    
  5. 移除服务

    net stop MySQL57
    mysqld --remove MySQL57
    
  6. 更改密码

    USE mysql;
    ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql'