windows同时安装 5.7 8.0 mysql

电脑配置升级了,想着自己电脑本地安装数据库,重新学习一下MySQL,下面记录了本地Windows安装两个版本MySQL的过程。

一.安装5.7版本

(1)在解压目录创建配置文件 - my.ini

[mysqld]
# set basedir to your installation path
basedir=D:/Program/mysql/mysql-5.7.24-winx64
# set datadir to the location of your data directory
datadir=D:/Program/mysql/mysql-5.7.24-winx64/data

(2)创建data文件夹,初始化数据目录:注意有两种选项

  • --initialize:默认的安全安装模式,会生成随机初始的root密码,这种情况下,密码被标记为过期,第一次登陆进去后需要设置新的密码
  • --initialize-insecure:不安全的安装模式,不会root生成密码
  • --user:指定用户
## 初始化数据目录,使用第一种方式
PS D:\Program\mysql\mysql-5.7.24-winx64\bin> .\mysqld --initialize --user=hecg95

(3)首次启动:

## 启动MySQL
PS D:\Program\mysql\mysql-5.7.24-winx64\bin> .\mysqld --console
...
2020-01-06T12:06:55.258100Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2020-01-06T12:06:55.258762Z 0 [Note] InnoDB: Uses event mutexes
2020-01-06T12:06:55.259034Z 0 [Note] InnoDB: _mm_lfence() and _mm_sfence() are used for memory barrier
2020-01-06T12:06:55.259223Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
2020-01-06T12:06:55.259652Z 0 [Note] InnoDB: Number of pools: 1
2020-01-06T12:06:55.259933Z 0 [Note] InnoDB: Not using CPU crc32 instructions
2020-01-06T12:06:55.263195Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2020-01-06T12:06:55.268347Z 0 [Note] InnoDB: Completed initialization of buffer pool
2020-01-06T12:06:55.313150Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2020-01-06T12:06:55.366453Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2020-01-06T12:06:55.367217Z 0 [Note] InnoDB: Setting file '.\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2020-01-06T12:06:55.415184Z 0 [Note] InnoDB: File '.\ibtmp1' size is now 12 MB.
2020-01-06T12:06:55.422146Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2020-01-06T12:06:55.422578Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2020-01-06T12:06:55.423030Z 0 [Note] InnoDB: Waiting for purge to start
2020-01-06T12:06:55.484660Z 0 [Note] InnoDB: 5.7.24 started; log sequence number 2591440
2020-01-06T12:06:55.485287Z 0 [Note] Plugin 'FEDERATED' is disabled.
2020-01-06T12:06:55.485644Z 0 [Note] InnoDB: Loading buffer pool(s) from D:\Program\mysql\mysql-5.7.24-winx64\data\ib_buffer_pool
2020-01-06T12:06:55.495151Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
2020-01-06T12:06:55.497758Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
2020-01-06T12:06:55.498298Z 0 [Note] IPv6 is available.
2020-01-06T12:06:55.499141Z 0 [Note]   - '::' resolves to '::';
2020-01-06T12:06:55.499488Z 0 [Note] Server socket created on IP: '::'.
2020-01-06T12:06:55.503746Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200106 20:06:55
2020-01-06T12:06:55.540602Z 0 [Note] Event Scheduler: Loaded 0 events
2020-01-06T12:06:55.541097Z 0 [Note] D:\Program\mysql\mysql-5.7.24-winx64\bin\mysqld.exe: ready for connections.
Version: '5.7.24'  socket: ''  port: 3306  MySQL Community Server (GPL)

(4)查找密码及修改密码:

  • 随机密码在data目录下的hqiogwdt0114.err文件中:wD2q)XQF%*nz

    2020-01-06T12:04:46.034179Z 1 [Note] A temporary password is generated for root@localhost: wD2q)XQF%*nz
    
  • 使用初始密码登陆MySQL,重新设置新的密码:

    PS D:\Program\mysql\mysql-5.7.24-winx64\bin> .\mysql -u root -p
    Enter password: ************
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 6
    Server version: 5.7.24
    
    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> use mysql
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    
    ## 首次登陆,提示需要修改密码
    mysql> set password="123456";
    Query OK, 0 rows affected (0.00 sec)
    
    mysql> exit
    Bye
    PS D:\Program\mysql\mysql-5.7.24-winx64\bin>
    
  • 使用mysqladmin关闭和重新启动MySQL:

    PS D:\Program\mysql\mysql-5.7.24-winx64\bin> .\mysqladmin -u root shutdown
    mysqladmin: connect to server at 'localhost' failed
    error: 'Access denied for user 'root'@'localhost' (using password: NO)'
    ## 需要密码
    PS D:\Program\mysql\mysql-5.7.24-winx64\bin> .\mysqladmin -u root shutdown -p
    Enter password: ******
    
    ## 重新启动 --console 会输出日志
    PS D:\Program\mysql\mysql-5.7.24-winx64\bin> .\mysqld --console
    ...
    2020-01-06T12:36:09.767463Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
    2020-01-06T12:36:09.767981Z 0 [Note] InnoDB: Uses event mutexes
    2020-01-06T12:36:09.768364Z 0 [Note] InnoDB: _mm_lfence() and _mm_sfence() are used for memory barrier
    2020-01-06T12:36:09.776583Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
    2020-01-06T12:36:09.779705Z 0 [Note] InnoDB: Number of pools: 1
    2020-01-06T12:36:09.780609Z 0 [Note] InnoDB: Not using CPU crc32 instructions
    2020-01-06T12:36:09.784180Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
    2020-01-06T12:36:09.789972Z 0 [Note] InnoDB: Completed initialization of buffer pool
    2020-01-06T12:36:09.840754Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
    2020-01-06T12:36:09.896882Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
    2020-01-06T12:36:09.897805Z 0 [Note] InnoDB: Setting file '.\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
    2020-01-06T12:36:09.938564Z 0 [Note] InnoDB: File '.\ibtmp1' size is now 12 MB.
    2020-01-06T12:36:09.953197Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
    2020-01-06T12:36:09.953663Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
    2020-01-06T12:36:09.957244Z 0 [Note] InnoDB: Waiting for purge to start
    2020-01-06T12:36:10.010316Z 0 [Note] InnoDB: 5.7.24 started; log sequence number 2591496
    2020-01-06T12:36:10.012857Z 0 [Note] Plugin 'FEDERATED' is disabled.
    2020-01-06T12:36:10.014410Z 0 [Note] InnoDB: Loading buffer pool(s) from D:\Program\mysql\mysql-5.7.24-winx64\data\ib_buffer_pool
    2020-01-06T12:36:10.030225Z 0 [Warning] Failed to set up SSL because of the following SSL library error: SSL context is not usable without certificate and private key
    2020-01-06T12:36:10.033015Z 0 [Note] Server hostname (bind-address): '*'; port: 3306
    2020-01-06T12:36:10.037653Z 0 [Note] IPv6 is available.
    2020-01-06T12:36:10.038029Z 0 [Note]   - '::' resolves to '::';
    2020-01-06T12:36:10.039131Z 0 [Note] Server socket created on IP: '::'.
    2020-01-06T12:36:10.045001Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200106 20:36:10
    2020-01-06T12:36:10.076855Z 0 [Note] Event Scheduler: Loaded 0 events
    2020-01-06T12:36:10.077455Z 0 [Note] D:\Program\mysql\mysql-5.7.24-winx64\bin\mysqld.exe: ready for connections.
    Version: '5.7.24'  socket: ''  port: 3306  MySQL Community Server (GPL)
    
    ## 监听到关闭信号
    2020-01-06T12:36:28.761907Z 0 [Note] D:\Program\mysql\mysql-5.7.24-winx64\bin\mysqld.exe: Normal shutdown
    
    2020-01-06T12:36:28.762424Z 0 [Note] Giving 0 client threads a chance to die gracefully
    2020-01-06T12:36:28.763869Z 0 [Note] Shutting down slave threads
    2020-01-06T12:36:28.763943Z 0 [Note] Forcefully disconnecting 0 remaining clients
    2020-01-06T12:36:28.772298Z 0 [Note] Event Scheduler: Purging the queue. 0 events
    2020-01-06T12:36:28.775985Z 0 [Note] Binlog end
    2020-01-06T12:36:28.779654Z 0 [Note] Shutting down plugin 'ngram'
    2020-01-06T12:36:28.779782Z 0 [Note] Shutting down plugin 'partition'
    2020-01-06T12:36:28.780434Z 0 [Note] Shutting down plugin 'BLACKHOLE'
    2020-01-06T12:36:28.780889Z 0 [Note] Shutting down plugin 'ARCHIVE'
    2020-01-06T12:36:28.781495Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
    2020-01-06T12:36:28.782033Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
    2020-01-06T12:36:28.782711Z 0 [Note] Shutting down plugin 'MyISAM'
    2020-01-06T12:36:28.783141Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
    2020-01-06T12:36:28.783563Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
    2020-01-06T12:36:28.784021Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
    2020-01-06T12:36:28.784406Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
    2020-01-06T12:36:28.787567Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
    2020-01-06T12:36:28.794063Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
    2020-01-06T12:36:28.794995Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
    2020-01-06T12:36:28.795741Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
    2020-01-06T12:36:28.796564Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
    2020-01-06T12:36:28.797016Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
    2020-01-06T12:36:28.797522Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
    2020-01-06T12:36:28.798010Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
    2020-01-06T12:36:28.798490Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
    2020-01-06T12:36:28.798970Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
    2020-01-06T12:36:28.799438Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
    2020-01-06T12:36:28.799871Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
    2020-01-06T12:36:28.800360Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
    2020-01-06T12:36:28.804643Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
    2020-01-06T12:36:28.804719Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
    2020-01-06T12:36:28.811108Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
    2020-01-06T12:36:28.813588Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
    2020-01-06T12:36:28.815982Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
    2020-01-06T12:36:28.824638Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
    2020-01-06T12:36:28.825589Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
    2020-01-06T12:36:28.834224Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
    2020-01-06T12:36:28.837309Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
    2020-01-06T12:36:28.838494Z 0 [Note] Shutting down plugin 'INNODB_CMP'
    2020-01-06T12:36:28.838905Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
    2020-01-06T12:36:28.839309Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
    2020-01-06T12:36:28.839723Z 0 [Note] Shutting down plugin 'INNODB_TRX'
    2020-01-06T12:36:28.840115Z 0 [Note] Shutting down plugin 'InnoDB'
    2020-01-06T12:36:28.840561Z 0 [Note] InnoDB: FTS optimize thread exiting.
    2020-01-06T12:36:28.841021Z 0 [Note] InnoDB: Starting shutdown...
    2020-01-06T12:36:28.957313Z 0 [Note] InnoDB: Dumping buffer pool(s) to D:\Program\mysql\mysql-5.7.24-winx64\data\ib_buffer_pool
    2020-01-06T12:36:28.962702Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 200106 20:36:28
    2020-01-06T12:36:29.731460Z 0 [Note] InnoDB: Shutdown completed; log sequence number 2591524
    2020-01-06T12:36:29.732301Z 0 [Note] InnoDB: Removed temporary tablespace data file: "ibtmp1"
    2020-01-06T12:36:29.739414Z 0 [Note] Shutting down plugin 'MEMORY'
    2020-01-06T12:36:29.739625Z 0 [Note] Shutting down plugin 'CSV'
    2020-01-06T12:36:29.739704Z 0 [Note] Shutting down plugin 'sha256_password'
    2020-01-06T12:36:29.739775Z 0 [Note] Shutting down plugin 'mysql_native_password'
    2020-01-06T12:36:29.739990Z 0 [Note] Shutting down plugin 'binlog'
    2020-01-06T12:36:29.741858Z 0 [Note] D:\Program\mysql\mysql-5.7.24-winx64\bin\mysqld.exe: Shutdown complete
    

二.安装8.0版本

(1)在解压目录创建配置文件 - my.ini

[mysqld]
# set basedir to your installation path
basedir=D:/Program/mysql/mysql-8.0.18-winx64
# set datadir to the location of your data directory
datadir=D:/Program/mysql/mysql-8.0.18-winx64/data
# 多个MySQL时,修改port,换个端口号
port = 13306

(2)创建data文件夹,初始化数据目录:注意有两种选项

  • --initialize:默认的安全安装模式,会生成随机初始的root密码,这种情况下,密码被标记为过期,第一次登陆进去后需要设置新的密码
  • --initialize-insecure:不安全的安装模式,不会root生成密码,默认为空密码
  • --user:指定用户
  • 参考官网的文档 - 2.10.1 Initializing the Data Directory
## 初始化数据目录,使用第二种方初始化自动创建data文件夹
PS D:\Program\mysql\mysql-8.0.18-winx64\bin> .\mysqld --initialize-insecure --user=hecg95

(3)首次启动:

## 启动MySQL
PS D:\Program\mysql\mysql-8.0.18-winx64\bin> .\mysqld --console
2020-01-06T12:58:00.798580Z 0 [System] [MY-010116] [Server] D:\Program\mysql\mysql-8.0.18-winx64\bin\mysqld.exe (mysqld 8.0.18) starting as process 33124
2020-01-06T12:58:01.573236Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2020-01-06T12:58:01.604355Z 0 [System] [MY-010931] [Server] D:\Program\mysql\mysql-8.0.18-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.18'  socket: ''  port: 13306  MySQL Community Server - GPL.
2020-01-06T12:58:01.618988Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060

(4)初次登陆及修改密码:

  • 查看data目录下的hqiogwdt0114.err文件:提示目前使用默认的空密码

    2020-01-06T12:51:51.897459Z 5 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
  启动8.0 服务
  mysqld install MySQL80 --defaults-file="D:\DevelopTool\MySQL\mysql-5.7.42\my.ini"
  开启服务(管理员打开的cmd窗口开启服务)
  mysqld --install mysql88 --defaults-file="E:\mysql-8.0.39-winx64\mysql-8.0.39-winx64\my.ini"
 
 
  • 使用空密码登陆MySQL,重新设置新的密码:

    在MySQL 8.0中,默认的身份验证插件已从更改 mysql_native_password为 caching_sha2_password,并且 'root'@'localhost'管理帐户caching_sha2_password默认使用。

    PS D:\Program\mysql\mysql-8.0.18-winx64\bin> .\mysql -P 13306  -u root -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 8.0.18 MySQL Community Server - GPL
    
    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 respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> use mysql
    Database changed
    
    ## 注意此处不同于之前的版本
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
    Query OK, 0 rows affected (0.01 sec)
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.01 sec)
    
    ## 否则客户端连接会报错
    1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
    
  • 使用mysqladmin关闭和重新启动MySQL:

    ## 关闭MySQL
    PS D:\Program\mysql\mysql-8.0.18-winx64\bin> .\mysqladmin -P 13306 -u root shutdown -p
    Enter password: ******
    
    ## 监听到关闭信号
    2020-01-06T13:21:31.470768Z 15 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.18).
    2020-01-06T13:21:31.473233Z 0 [System] [MY-013105] [Server] D:\Program\mysql\mysql-8.0.18-winx64\bin\mysqld.exe: Normal shutdown.
    2020-01-06T13:21:31.766555Z 0 [System] [MY-010910] [Server] D:\Program\mysql\mysql-8.0.18-winx64\bin\mysqld.exe: Shutdown complete (mysqld 8.0.18)  MySQL Community Server - GPL.
    
    ## 重新启动 --console 会输出日志
    PS D:\Program\mysql\mysql-8.0.18-winx64\bin> .\mysqld --console
    2020-01-06T13:23:27.071931Z 0 [System] [MY-010116] [Server] D:\Program\mysql\mysql-8.0.18-winx64\bin\mysqld.exe (mysqld 8.0.18) starting as process 24388
    2020-01-06T13:23:27.696975Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    2020-01-06T13:23:27.732170Z 0 [System] [MY-010931] [Server] D:\Program\mysql\mysql-8.0.18-winx64\bin\mysqld.exe: ready for connections. Version: '8.0.18'  socket: ''  port: 13306  MySQL Community Server - GPL.
    2020-01-06T13:23:27.862052Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060