【MySQL】Windows下安装第二套MySQL数据库
一、安装前提:Windows下本机已安装好MySQL8.0版本的数据库
安装过程不赘述了。
二、Windows下安装第二套MySQL数据库5.7
2.1、下载MySQL5.7版本
下载地址: https://downloads.mysql.com/archives/community/
下载后解压
2.2、配置配置文件
在mysql根目录创建my.ini配置文件
注意: 端口号要跟另一个MySQL服务的端口号区分开. 路径中使用右斜杠 ‘/’
[Client]
#设置3307端口
port = 3307
[mysqld]
#设置3307端口
port = 3307
# 设置mysql的安装目录
basedir=D:/tools/mysql-5.7.43-winx64
# 设置mysql数据库的数据的存放目录
datadir=D:/tools/mysql-5.7.43-winx64/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
log-bin=mysql-bin
server-id=1
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
2.3、初始化mysql信息,先不注册服务
进入到bin目录执行以下内容
# 初始化mysql信息
mysqld --initialize --console
与之前安装MySQL8.0的步骤一样
初始化之后 根目录会生成data文件夹
控制台会打印MySQL初始密码信息
执行过程如下:
D:\>cd D:\tools\mysql-5.7.43-winx64\bin
D:\tools\mysql-5.7.43-winx64\bin>mysqld --initialize --console
2024-01-12T11:59:55.524632Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-01-12T11:59:55.524673Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2024-01-12T11:59:55.524676Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2024-01-12T11:59:55.656925Z 0 [Warning] InnoDB: New log files created, LSN=45790
2024-01-12T11:59:55.683833Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2024-01-12T11:59:55.766307Z 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: 1a068a87-b142-11ee-975c-2cf05d4d6a70.
2024-01-12T11:59:55.769811Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2024-01-12T11:59:56.221862Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-01-12T11:59:56.222117Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-01-12T11:59:56.225526Z 0 [Warning] CA certificate ca.pem is self signed.
2024-01-12T11:59:56.387129Z 1 [Note] A temporary password is generated for root@localhost: gC3%pXKhRV2r
D:\tools\mysql-5.7.43-winx64\bin>
2.4、这里先使用bat命令来启动、停止MySQL5.7
启动,创建一个startup_mysql5.7.bat,内容如下:
"D:\tools\mysql-5.7.43-winx64\bin\mysqld.exe" --defaults-file=D:\tools\mysql-5.7.43-winx64\my.ini --datadir=D:\tools\mysql-5.7.43-winx64\data
双击启动:
C:\WINDOWS\system32>"D:\tools\mysql-5.7.43-winx64\bin\mysqld.exe" --defaults-file=D:\tools\mysql-5.7.43-winx64\my.ini --datadir=D:\tools\mysql-5.7.43-winx64\data --console
2024-01-12T12:00:32.064847Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2024-01-12T12:00:32.064889Z 0 [Warning] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2024-01-12T12:00:32.064892Z 0 [Warning] 'NO_AUTO_CREATE_USER' sql mode was not set.
2024-01-12T12:00:32.064908Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2024-01-12T12:00:32.065577Z 0 [Note] D:\tools\mysql-5.7.43-winx64\bin\mysqld.exe (mysqld 5.7.43-log) starting as process 16964 ...
2024-01-12T12:00:32.072210Z 0 [Note] InnoDB: Mutexes and rw_locks use Windows interlocked functions
2024-01-12T12:00:32.072512Z 0 [Note] InnoDB: Uses event mutexes
2024-01-12T12:00:32.072611Z 0 [Note] InnoDB: _mm_lfence() and _mm_sfence() are used for memory barrier
2024-01-12T12:00:32.072725Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.13
2024-01-12T12:00:32.073117Z 0 [Note] InnoDB: Number of pools: 1
2024-01-12T12:00:32.073320Z 0 [Note] InnoDB: Not using CPU crc32 instructions
2024-01-12T12:00:32.076196Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
2024-01-12T12:00:32.080785Z 0 [Note] InnoDB: Completed initialization of buffer pool
2024-01-12T12:00:32.122926Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
2024-01-12T12:00:32.171505Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
2024-01-12T12:00:32.172075Z 0 [Note] InnoDB: Setting file '.\ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
2024-01-12T12:00:32.181247Z 0 [Note] InnoDB: File '.\ibtmp1' size is now 12 MB.
2024-01-12T12:00:32.183333Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
2024-01-12T12:00:32.183480Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
2024-01-12T12:00:32.183908Z 0 [Note] InnoDB: Waiting for purge to start
2024-01-12T12:00:32.234482Z 0 [Note] InnoDB: 5.7.43 started; log sequence number 2766921
2024-01-12T12:00:32.235867Z 0 [Note] Plugin 'FEDERATED' is disabled.
2024-01-12T12:00:32.237071Z 0 [Note] InnoDB: Loading buffer pool(s) from D:\tools\mysql-5.7.43-winx64\data\ib_buffer_pool
2024-01-12T12:00:32.262204Z 0 [Note] InnoDB: Buffer pool(s) load completed at 240112 20:00:32
2024-01-12T12:00:32.265512Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
2024-01-12T12:00:32.265889Z 0 [Note] Skipping generation of SSL certificates as certificate files are present in data directory.
2024-01-12T12:00:32.266044Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2024-01-12T12:00:32.267063Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2024-01-12T12:00:32.272392Z 0 [Warning] CA certificate ca.pem is self signed.
2024-01-12T12:00:32.272865Z 0 [Note] Skipping generation of RSA key pair as key files are present in data directory.
2024-01-12T12:00:32.274021Z 0 [Note] Server hostname (bind-address): '*'; port: 3307
2024-01-12T12:00:32.274474Z 0 [Note] IPv6 is available.
2024-01-12T12:00:32.274661Z 0 [Note] - '::' resolves to '::';
2024-01-12T12:00:32.274991Z 0 [Note] Server socket created on IP: '::'.
2024-01-12T12:00:32.313567Z 0 [Note] Failed to start slave threads for channel ''
2024-01-12T12:00:32.328520Z 0 [Note] Event Scheduler: Loaded 0 events
2024-01-12T12:00:32.328893Z 0 [Note] D:\tools\mysql-5.7.43-winx64\bin\mysqld.exe: ready for connections.
Version: '5.7.43-log' socket: '' port: 3307 MySQL Community Server (GPL)
2024-01-12T12:00:38.507929Z 2 [Note] Access denied for user 'root'@'localhost' (using password: YES)
关闭服务,创建一个shutdown_mysql5.7.bat,内容如下:
"D:\tools\mysql-5.7.43-winx64\bin\mysqladmin.exe" -P 3307 shutdown --user=root --password=1
2.4、修改密码
执行过程如下:
D:\tools\mysql-5.7.43-winx64\bin>mysql -uroot -p
Enter password: *
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.43-log MySQL Community Server (GPL)
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> set password=password('1');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql>
2.5、登录MySQL
输入刚才的默认密码登录到数据库
mysql -uroot -p
2.6、启用远程登录
C:\WINDOWS\system32>D:
D:\>cd D:\tools\mysql-5.7.43-winx64\bin
D:\tools\mysql-5.7.43-winx64\bin>mysql -uroot -p
Enter password: *
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.43-log MySQL Community Server (GPL)
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> use mysql
Database changed
mysql> select host,user from user where user="root";
+-----------+------+
| host | user |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by '1' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql>
2.7、创建数据库测试
2.7.1、创建一个数据库
创建数据库tzqdb:
mysql> create database tzqdb;
Query OK, 1 row affected (0.00 sec)
2.7.2、创建用户与分配权限
2.7.2.1、创建用户tzq
mysql> create user 'tzq'@'%' identified by '1';
Query OK, 0 rows affected (0.00 sec)
2.7.2.2、授权tzq用户在所有主机上(%)有tzqdb数据库的所有对象的所有权限
mysql> grant all privileges on tzqdb.* to 'tzq'@'%' identified by '1';
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.7.2.3、授权本地登录
mysql> grant all privileges on tzqdb.* to 'tzq'@'localhost' identified by '1';
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.7.2.4、最强授权
mysql> grant all privileges on *.* to 'tzq'@'%' identified by '1' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
2.7.2.5、刷权限
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.7.2.6、检查用户
检查mysql数据下的user表信息
select host,user from mysql.`user`
host user
% root
% tzq
localhost mysql.session
localhost mysql.sys
localhost root
localhost tzq
三、卸载MySQL
3.1、停止mysql服务:
net stop MySQL57
3.2、移除Windows操作系统服务
mysqld -remove MySQL57
3.3、删除mysql目录、环境变量
附:
注册服务,这里有问题,先不整
这里,我们的前提是安装了MySQL8.0,而且环境变量也配置过了。
所以这里配置文件配置有一点不同:
注册服务 (默认注册名是MySQL, 新注册的这个服务要与之前的服务名区分开)
进入D:\tools\mysql-5.7.43-winx64\bin目录
然后重新安装服务
C:\WINDOWS\system32>D:
D:\>cd D:\tools\mysql-5.7.43-winx64\bin
D:\tools\mysql-5.7.43-winx64\bin>mysqld.exe -install mysql57 --defaults-file="D:\tools\mysql-5.7.43-winx64\my.ini"
Service successfully installed.
删除添加的mysql服务
mysqld -remove MySQL5.7