編碼的世界 / 優質文選 / 歷史

不卸載原有mysql直接安裝mysql8.0


2022年7月28日
-   

作者:小小明

下載zip安裝包


下載地址:https://dev.mysql.com/downloads/mysql/
百度雲下載地址:
https://pan.baidu.com/s/1vORXjeap7US2bdWDZA6pNQ 提取碼:cu79
下載完成後,解壓壓縮包,進入壓縮的目錄,新建文本文件my.ini。
假設你解壓出來的目錄為E:Program Filesmysql-8.0.19-winx64,my.ini內容如下:
[mysqld]
# 設置3306端口
port=3306
# 設置mysql的安裝目錄
basedir=E:Program Filesmysql-8.0.19-winx64
# 設置mysql數據庫的數據的存放目錄
datadir=E:Program Filesmysql-8.0.19-winx64data
# 允許最大連接數
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

初始化數據庫


在MySQL安裝目錄的bin目錄執行命令:
mysqld initialize console

此時,我的win7系統報錯:

解決方案:去百度下載一個vcruntime140_1.dll放入%windir%system32目錄中。
我使用的下載地址:https://www.jb51.net/dll/vcruntime140_1.dll.html
%windir%system32在我的系統中表示C:WindowsSystem32
再次執行上述命令後:
E:Program Filesmysql-8.0.19-winx64in>mysqld initialize console
2020-06-06T16:34:34.062007Z 0 [System] [MY-013169] [Server] E:Program Filesmys
ql-8.0.19-winx64inmysqld.exe (mysqld 8.0.19) initializing of server in progre
ss as process 7816
2020-06-06T16:34:37.513202Z 5 [Note] [MY-010454] [Server] A temporary password i
s generated for root@localhost: /rfMQ+bGi22z

表示初始化成功。
注意:我這裏生成的臨時密碼是/rfMQ+bGi22z,下文會使用到。

登陸mysql數據庫


啟動mysql服務端,需要另外啟動一個命令後,執行mysqld:
E:Program Filesmysql-8.0.19-winx64in>mysqld


就如上圖一樣,讓這個命令行一直後台掛著,mysql的服務進程占用著它,不能關閉,關閉後mysql的服務進程也會跟著被關閉。
再用mysql客戶端登陸後,順利登陸:
E:Program Filesmysql-8.0.19-winx64in>mysql -uroot -p/rfMQ+bGi22z
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 8
Server version: 8.0.19
Copyright (c) 2000, 2020, 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>

修改密碼為123456:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1
23456';
Query OK, 0 rows affected (0.01 sec)
mysql>

注冊/刪除mysql服務


經過上述操作mysql8.0已經可以正常使用,但啟動mysql服務需要占用一個命令行未免有些麻煩,現在我們通過將mysqld注冊為系統服務解決這個問題。
在MySQL安裝目錄的bin目錄執行命令:
E:Program Filesmysql-8.0.19-winx64in>mysqld install mysql8.0
Service successfully installed.

mysql8.0可以改為其他你喜歡的服務的名字。
注冊成功後,任務管理器上可以看到服務的名字:

只需要右鍵啟動服務,mysql8.0就可以正常使用了。
然後mysql客戶端就可以使用新密碼正常的登陸了:
E:Program Filesmysql-8.0.19-winx64in>mysql -uroot -p123456
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.19 MySQL Community Server - GPL
Copyright (c) 2000, 2020, 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>

如果你希望在任何目錄下都能執行mysql客戶端命令,則需要將mysql的安裝目錄下的bin目錄加入環境變量中,例如:E:Program Filesmysql-8.0.19-winx64in。
刪除服務:
sc delete mysql8.0 -remove

mysql8.0是你要刪除的服務名稱

熱門文章