Ubuntu18.04下安裝MySQL5.7(支持win10-wsl環境)

来源:https://www.cnblogs.com/chenyc2020/archive/2020/03/30/12579257.html
-Advertisement-
Play Games

註意: 本文操作環境為win10系統wsl下的Ubuntu18.04,對於原生的Ubuntu18.04同樣適用。MySQL預設版本為5.7,其他版本不適用。 安裝步驟 1.更新源: 2.安裝mysql: wsl下使用上述命令安裝就直接安裝上去了,沒有設置密碼的地方,這時候無論怎麼登陸,都無法登錄上去 ...


註意: 本文操作環境為win10系統wsl下的Ubuntu18.04,對於原生的Ubuntu18.04同樣適用。MySQL預設版本為5.7,其他版本不適用。

安裝步驟

1.更新源:

sudo apt update

2.安裝mysql:

sudo apt install mysql-server 

wsl下使用上述命令安裝就直接安裝上去了,沒有設置密碼的地方,這時候無論怎麼登陸,都無法登錄上去。

chenyc@DESKTOP-Q5J25HR:~$ mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
chenyc@DESKTOP-Q5J25HR:~$

設置root用戶密碼:

chenyc@DESKTOP-Q5J25HR:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root:
Error: Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

發現無法設置,看報錯信息,說的是不能連接到mysqld.sock套接字,猜想是mysql服務沒有開啟。

開啟mysql服務:

chenyc@DESKTOP-Q5J25HR:~$ sudo service mysql start
 * Starting MySQL database server mysqld         
No directory, logging in with HOME=/               [ OK ]

重新設置密碼:
使用sudo mysql_secure_installation命令,有幾個地方需要用戶確認。

Press y|Y for Yes, any other key for No:y

y,前面提示大致的意思是:預設使用空的密碼連接,該種連接方式可以作為測試使用,但是不安全,問是否要重新設置密碼。

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0

這裡是讓選擇密碼強度,從前面提示可以知道,有LOWMEDIUMSTRONG三種強度可選,我們選擇0即可。

New password:

Re-enter new password:

這個沒什麼好說的,讓設置密碼,並確認新密碼。

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

問是否移除匿名用戶,匿名用戶留著也沒什麼用,可以移除掉,選擇y

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

是否禁止root用戶遠程登錄,在沒有設置其他用戶之前,只能通過root用戶登錄,所以不能禁止,選擇n

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

是否移除測試資料庫,選擇n

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : n

是否重新載入特權表,最好不動它,選否。

下麵是設置密碼的全過程。

chenyc@DESKTOP-Q5J25HR:~$ sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 25
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : n

 ... skipping.
All done!

以上設置完成後,使用剛剛設置的root用戶密碼,再次連接資料庫,成功連接上。

chenyc@DESKTOP-Q5J25HR:~$ sudo 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.29-0ubuntu0.18.04.1 (Ubuntu)

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> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | latin1                     |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | latin1                     |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

可以看到,剛剛安裝好的mysql預設有很多是latin1的編碼格式,對漢字的支持不好,因此需要改成utf8編碼格式。

修改mysql資料庫編碼的步驟:
1.停止mysql服務:

chenyc@DESKTOP-Q5J25HR:~$ /etc/init.d/mysql stop
 * Stopping MySQL database server mysqld                                  
cat: /var/run/mysqld/mysqld.pid: Permission denied
                                                                                                                                               [fail]

發現沒有許可權,加上sudo再去執行:

chenyc@DESKTOP-Q5J25HR:~$ sudo /etc/init.d/mysql stop
 * Stopping MySQL database server mysqld                           [ OK ] 

服務停止成功。
2.修改配置文件:
進入配置文件目錄:

cd /etc/mysql/mysql.conf.d

修改之前先備份:

sudo cp mysqld.cnf mysqld.cnf.2

接下來修改配置文件,執行如下命令:

sudo vim mysqld.cnf

修改兩個地方:

# 修改處1:添加以下2行 
[client] 
default-character-set=utf8
[mysqld]
# 修改處2:添加以下3行 
default-storage-engine=INNODB
character-set-server=utf8 
collation-server=utf8_general_ci

修改完成後,重啟資料庫:

chenyc@DESKTOP-Q5J25HR:~$ sudo service mysql start
 * Starting MySQL database server mysqld                                  [ OK ] 

再次登錄,發現登不上了:

chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

使用sudo cat /etc/mysql/debian.cnf查看debian-sys-maint密碼,發現密碼是下麵這個玩意:

chenyc@DESKTOP-Q5J25HR:~$ sudo cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host     = localhost
user     = debian-sys-maint
password = LMCuPijw9kX5cRsS
socket   = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host     = localhost
user     = debian-sys-maint
password = LMCuPijw9kX5cRsS
socket   = /var/run/mysqld/mysqld.sock

因此,可以使用debian-sys-maint用戶先登錄上去,修改密碼:

chenyc@DESKTOP-Q5J25HR:~$ mysql -udebian-sys-maint -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

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>

使用上面的password,成功登錄。
修改密碼,執行如下語句:

UPDATE mysql.user SET authentication_string=PASSWORD('你的密碼'), PLUGIN='mysql_native_password'
 WHERE USER='root';

執行時發現報錯:

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('cyc2010'), PLUGIN='mysql_native_password' WHERE USER='root';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

說不符合安全規範,估計是之前設置安全級別的那地方的問題,需要重新設置一下:

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

validate_password_length(密碼長度)參數預設為8,修改為1:

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

再次修改密碼成功。

mysql> UPDATE mysql.user SET authentication_string=PASSWORD('cyc2010'), PLUGIN='mysql_native_password' WHERE USER='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

授權遠程登錄:

mysql> grant all privileges on *.* to 'root'@'%' identified by 'cyc2010' with grant option;
Query OK, 0 rows affected, 1 warning (0.01 sec)

設置完成後,再次重啟服務:

chenyc@DESKTOP-Q5J25HR:~$ sudo /etc/init.d/mysql restart
 * Stopping MySQL database server mysqld                                                                                                       [ OK ]  * Starting MySQL database server mysqld                                                                                                              No directory, logging in with HOME=/                    [ OK ]

使用root用戶登錄:

chenyc@DESKTOP-Q5J25HR:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

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> show variables like 'character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

發現都變成了utf8,說明設置成功了。

系統重啟後失效問題

重啟wsl-Linux子系統:

//WSL-Ubuntu18.04 LTS 重啟方法
//以管理員許可權運行cmd
>net stop LxssManager	//停止
>net start LxssManager	//啟動

重啟系統後,再次登錄,發現又登不上了:

chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

看報錯信息,應該是mysql服務沒有啟動,啟動服務:

chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ sudo service mysql start
[sudo] password for chenyc:
 * Starting MySQL database server mysqld                                                                                         No directory, logging in with HOME=/
                                                                                                                          [ OK ]

再次登錄,發現登陸成功。

chenyc@DESKTOP-Q5J25HR:/mnt/c/Users/cheny$ mysql -u root -p                                                                      Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.29-0ubuntu0.18.04.1 (Ubuntu)

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>

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • Cassandra簡介: Apache Cassandra最初由Facebook開發,用於儲存收件箱等簡單格式數據,集GoogleBigTable的數據模型與Amazon Dynamo的完全分散式的架構於一身Facebook於2008將 Cassandra 開源, Apache Cassandra是 ...
  • 預設隔離級別下 , mysql沒有解決幻讀問題 , 需要應用代碼裡加一個鎖來解決 幻讀問題是啥? 預設的隔離級別是可重覆讀 REPEATABLE-READ , 在這個模式下出現幻讀的例子一般是這兩種情況: 事務1和事務2同時 , 事務1讀數據 , 事務2插入數據提交 , 事務1插入同樣的數據時報錯說 ...
  • mysql的innodb引擎本身存儲的形式就必須是聚簇索引的形式 , 在磁碟上樹狀存儲的 , 但是不一定是根據主鍵聚簇的 , 有三種情形: 1. 有主鍵的情況下 , 主鍵就是聚簇索引 2. 沒有主鍵的情況下 , 第一個非空null的唯一索引就是聚簇索引 3. 如果上面都沒有 , 那麼就是有一個隱藏的 ...
  • 在上一篇文章中,通過分析執行計劃的欄位說明,大體說了一下索引優化過程中的一些註意點,那麼如何才能避免索引失效呢?本篇文章將來討論這個問題。 避免索引失效的常見方法 1.對於複合索引的使用,應按照索引建立的順序使用,儘量不要跨列(最佳左首碼原則) 為了說明問題,我們仍然使用上一篇文章中的test01表 ...
  • 前言 研究SQL性能問題,其實本質就是優化索引,而優化索引,一個非常重要的工具就是執行計劃(explain),它可以模擬SQL優化器執行SQL語句,從而讓開發人員知道自己編寫的SQL的運行情況。 執行計劃語法 執行計劃的語法非常簡單,就是在要執行的SQL語句前加上 即可。 以我們在上一篇文章中創建的 ...
  • SQL為什麼需要優化? 對於初學者來說,能夠寫出實現功能的SQL語句而不出錯,查詢出所需要的結果,就已經能夠滿足日常使用了。但在某些場景,對性能的要求比較高,因此,要求SQL的執行響應速度快,就需要對SQL進行一定程度的優化。 在實際應用場景中,MySQL經常會存在諸如性能低、執行時間過長、等待時間 ...
  • `MySQL`的行鎖在引擎層由各個引擎自己實現的,但並不是所有的引擎都支持行鎖。 不支持行鎖意味著併發控制只能使用表鎖,對於這種引擎的表,同一張表任何時刻只能有一個更新在執行,這會影響到業務併發度。 引擎下的事務中,行鎖是需要的時候才加上的,但並不是不需要了立刻釋放,而是要等到事務結束時(commi ...
  • 上一篇文章主要介紹了MySQL在Ubuntu18.04系統上的安裝,以及安裝過程中可能會遇到的一些問題的解決方案。 在這篇文章里,開始介紹MySQL資料庫的邏輯分層。通過本文的介紹,可以大致瞭解到MySQL的語句從客戶端發出請求後,在伺服器經歷了怎樣的過程。有助於後面MySQL優化的加深理解。 My ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...