前期准备工作:确认之前安装的MySQL服务是否启动,若启动则关闭。
一、下载MySQL
链接: https://dev.mysql.com/downloads/mysql
二、MySQL安装配置
2.1 解压压缩包
2.2 创建my.ini初始化文件
[client]
# 设置mysql客户端默认字符集
default-character-set=utf8
[mysqld]
# 设置3307端口 因为之前装MySQL5.6已经占用了3306端口,所以需要指定其他端口
port =3307
# 设置mysql的安装目录
basedir=C:\Develop\mysql-8.0.27-winx64
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
datadir=C:\Develop\mysql-8.0.27-winx64/data
# 允许最大连接数
max_connections=200
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
2.3 初始化MySQL
以管理员身份,进入MySQL的bin目录执行以下命令:
mysqld --initialize --user=mysql --console
如果命令执行成功,会在日志最后输出密码。
PS C:\Develop\mysql-8.0.27-winx64\bin> mysqld --initialize --user=mysql --console
2021-10-31T04:12:21.113827Z 0 [System] [MY-013169] [Server] C:\Develop\mysql-8.0.27-winx64\bin\mysqld.exe (mysqld 8.0.27) initializing of server in progress as process 2116
2021-10-31T04:12:21.153194Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2021-10-31T04:12:21.264140Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-10-31T04:12:24.281320Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-10-31T04:12:27.751347Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1 is enabled for channel mysql_main
2021-10-31T04:12:27.768773Z 0 [Warning] [MY-013746] [Server] A deprecated TLS version TLSv1.1 is enabled for channel mysql_main
2021-10-31T04:12:28.262196Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: zhj;iS!h)7sp
**数据库默认密码:**zhj;iS!h)7sp
以上命令成功执行完毕后,将在MySQL目录下新创建一个data文件夹。
2.4 正式安装MySQL
执行以下命令,其中MySQL8为服务名称,可以自定义,–defaults-file后跟my.ini的路径。
mysqld install MySQL8 --defaults-file="C:\Develop\mysql-8.0.27-winx64\my.ini"
出现上图所示日志,表示服务安装成功了!
PS C:\Develop\mysql-8.0.27-winx64\bin> mysqld install MySQL8 --defaults-file="C:\Develop\mysql-8.0.27-winx64\my.ini"
Service successfully installed.
通过任务管理器中可查看系统服务,发现刚注册的MySQL8服务已在服务列表中。
三、数据库使用
3.1 启动Mysql8服务
方式一:在Dos窗口下,输入命令:net start MySQL8
方式二:进入服务列表中,找到MySQL8服务后手动启动
3.2 登录数据库(使用默认密码)
输入以下命令,其中端口号为my.ini中设置的,然后按Enter键,在Enter password:后输入2.3初始化MySQL章节生成的默认密码,再次按Enter键。
mysql -uroot -p -P端口号
登录成功后,将显示MySQL数据库版本信息,如下:
C:\Users\Administrator>mysql -uroot -p -P3307
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.27
Copyright (c) 2000, 2021, 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.
3.3 修改数据库密码
mysql> alter user 'root'@'localhost' identified by 'root';
Query OK, 0 rows affected (0.01 sec)
以修改后的密码重新登录数据库
C:\Users\Administrator>mysql -uroot -proot -P3307
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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>
四、参考文献
https://blog.csdn.net/wnw001/article/details/104537137