工具善其事,必先利其器惜索,WIN10 配置wsl2的開發(fā)環(huán)境

前置準備

windows系統(tǒng):windows 10 專業(yè)版(家庭版無法開啟虛擬機)
windows用戶:Administrator (普通用戶可能會有莫名其妙的一些問題)


win10

WIN10 開啟 wsl2 支持

  1. 找到windows控制面板中的程序和功能派昧,點擊啟動或關閉windows功能


    勾選Hyper-v和適用于Linux的windows子系統(tǒng)

下載適用于 x64 計算機的 WSL2 Linux 內核更新包

運行上一步中下載的更新包進行安裝黔姜。
安裝完成后,設置 wsl 默認版本為 2

wsl --set-default-version 2

安裝ubuntu

  1. 打開win10應用商店蒂萎,搜索ubuntu秆吵,選第一個安裝。


    安裝ubuntu

查看ubuntu 的wsl版本

wsl -l -v
wsl -l -v

version 為 2 表示 ubuntu 是以 wsl2 運行的五慈。如果這里是 1帮毁,可以再次運行如下命令

wsl --set-version Ubuntu 2

安裝Windows Terminal (可選)

  1. 打開應用商店,搜索windows terminal豺撑,選第一個安裝。


    windows terminal

    windows terminal

安裝gsudo https://github.com/gerardog/gsudo (可選)

  1. 打開windos terminal黔牵,在powershell中執(zhí)行下面的命令安裝gsudo
PowerShell -Command "Set-ExecutionPolicy RemoteSigned -scope Process; iwr -useb https://raw.githubusercontent.com/gerardog/gsudo/master/installgsudo.ps1 | iex"
  1. 打開windows terminal的設置聪轿,將配置文件修改為:
// This file was initially generated by Windows Terminal 1.1.2233.0
// It should still be usable in newer versions, but newer versions might have additional
// settings, help text, or changes that you will not see unless you clear this file
// and let us generate a new one for you.

// To view the default settings, hold "alt" while clicking on the "Settings" button.
// For documentation on these settings, see: https://aka.ms/terminal-documentation
{
    "$schema": "https://aka.ms/terminal-profiles-schema",

    "defaultProfile": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",

    // You can add more global application settings here.
    // To learn more about global settings, visit https://aka.ms/terminal-global-settings

    // If enabled, selections are automatically copied to your clipboard.
    "copyOnSelect": false,

    // If enabled, formatted data is also copied to your clipboard
    "copyFormatting": false,

    // A profile specifies a command to execute paired with information about how it should look and feel.
    // Each one of them will appear in the 'New Tab' dropdown,
    //   and can be invoked from the commandline with `wt.exe -p xxx`
    // To learn more about profiles, visit https://aka.ms/terminal-profile-settings
    "profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
        },
        "list":
        [
            {
                // Make changes here to the powershell.exe profile.
                "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
                "name": "Windows PowerShell",
                "commandline": "gsudo powershell.exe",
                "hidden": false
            },
            {
                // Make changes here to the cmd.exe profile.
                "guid": "{0caa0dad-35be-5f56-a8ff-afceeeaa6101}",
                "name": "命令提示符",
                "commandline": "gsudo cmd.exe",
                "hidden": false
            },
            {
                "guid": "{2c4de342-38b7-51cf-b940-2309a097f518}",
                "hidden": false,
                "name": "Ubuntu",
                "source": "Windows.Terminal.Wsl"
            },
            {
                "guid": "{b453ae62-4e3d-5e58-b989-0a998ec441b8}",
                "hidden": false,
                "name": "Azure Cloud Shell",
                "source": "Windows.Terminal.Azure"
            }
        ]
    },

    // Add custom color schemes to this array.
    // To learn more about color schemes, visit https://aka.ms/terminal-color-schemes
    "schemes": [],

    // Add custom keybindings to this array.
    // To unbind a key combination from your defaults.json, set the command to "unbound".
    // To learn more about keybindings, visit https://aka.ms/terminal-keybindings
    "keybindings":
    [
        // Copy and paste are bound to Ctrl+Shift+C and Ctrl+Shift+V in your defaults.json.
        // These two lines additionally bind them to Ctrl+C and Ctrl+V.
        // To learn more about selection, visit https://aka.ms/terminal-selection
        { "command": {"action": "copy", "singleLine": false }, "keys": "ctrl+c" },
        { "command": "paste", "keys": "ctrl+v" },

        // Press Ctrl+Shift+F to open the search box
        { "command": "find", "keys": "ctrl+shift+f" },

        // Press Alt+Shift+D to open a new pane.
        // - "split": "auto" makes this pane open in the direction that provides the most surface area.
        // - "splitMode": "duplicate" makes the new pane use the focused pane's profile.
        // To learn more about panes, visit https://aka.ms/terminal-panes
        { "command": { "action": "splitPane", "split": "auto", "splitMode": "duplicate" }, "keys": "alt+shift+d" }
    ]
}

其實里面要改的地方就兩個,在powershell 和 cmd 中分別找到 commandline 屬性 猾浦,在原有的命令前面加上 gsudo 就好了

修改wondows terminal設置

這樣以后再打開 powershell 或 cmd 就是以管理員身份運行的了陆错。


ubuntu 替換阿里源

  1. 進入linux子系統(tǒng)
    輸入用戶名:hezhigang (以hezhigang為例)
    輸入密碼:****
  2. 查看一下ubuntu 版本
lsb_release -c

查看ubuntu系統(tǒng)版本

focal代表ubuntu20.04
eoan代表ubuntu19.10
disco代表ubuntu19.04
bionic代表ubuntu18.04
xenial代表ubuntu16.04

  1. 原來的軟件源并另存
 sudo cp -v /etc/apt/sources.list /etc/apt/sources.list.backup
  1. 執(zhí)行chmod命令更改文件權限使軟件源文件可編輯
sudo chmod 777 /etc/apt/sources.list
  1. 替換成阿里源
vim /etc/apt/sources.list

把所有文本用下面的替換掉

deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse

完成后更新一下

sudo apt update

ubuntu安裝docker

  1. 按照官網文檔https://docs.docker.com/engine/install/ubuntu/指示,依次執(zhí)行如下命令
$ sudo apt-get update

$ sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common


$ sudo apt remove gpg
$ sudo apt install gnupg1

$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
$ sudo apt-key fingerprint 0EBFCD88

$ sudo apt-get update
$ sudo apt-get install docker-ce docker-ce-cli containerd.io
  1. 安裝mysql docker鏡像測試一下
    查詢mysql鏡像金赦,我這里查詢mysql-employee音瓷,這是一個帶實例employee示例數據庫的版本
sudo docker search mysql-employee
image.png

選第一個 pull 下來

sudo docker pull genschsa/mysql-employees

等pull完后,執(zhí)行下面的命令將其啟動夹抗,其中參數--restart=always 表示該容器隨docker啟動后會自動啟動绳慎。

sudo docker run -d --name mysql-employees -p 3306:3306  -e MYSQL_ROOT_PASSWORD=123456 -v $PWD/data:/var/lib/mysql genschsa/mysql-employees --restart=always

在win10中安裝 wsl2host

  1. 下載wsl2host

    下載

  2. 以管理員方式運行powershell,輸入以下命令安裝wsl2host

> .\wsl2host.exe install
Windows Username: <username-you-use-to-login-to-windows>
Windows Password: <password-for-this-user>

輸入win10系統(tǒng)的用戶名和登錄密碼漠烧,沒有報錯即表示安裝成功(注意杏愤,我第一次用非超級管理員賬戶安裝后,啟動后就立刻停止了已脓,后來重新用administrator用戶安裝后才啟動成功)
如果想卸載或重新安裝:

>.\wsl2host.exe remove

安裝成功后珊楼,重啟wsl, 打開host文件度液,看其中是否多了下圖中的一行


wsl2host自動添加的host映射

此后厕宗,每次wsl重啟,wsl2host都會自動把wsl中的IP更新到host文件中堕担,我們只要使用ubuntu.wsl即可訪問到wsl中的docker服務了已慢。


讓docker服務隨windows系統(tǒng)啟動而自動啟動

一、讓docker服務器隨ubuntu 系統(tǒng)啟動而自動啟動

  1. 把當前用戶hezhigang加入 docker 用戶組
sudo usermod -aG docker hezhigang
  1. wsl2中創(chuàng)建啟動腳本
sudo vim  /usr/local/sbin/start_docker.sh
  1. 然后, 把以下內容添加到腳本中
#!/usr/bin/env bash
sudo cgroupfs-mount
sudo service docker start
  1. 給腳本添加執(zhí)行權限
sudo chmod 755 /usr/local/sbin/start_docker.sh
  1. 給當前用戶添加權限, 使在使用 sudo命令的時候不需要輸入密碼
sudo  vim  /etc/sudoers

在打開的文本中找到 %sudo ALL=(ALL:ALL) ALL 這一行后面添加如下代碼

hezhigang  ALL=(ALL:ALL) NOPASSWD: ALL
用 wq! 命令強制保存文件

二霹购、讓ubuntu系統(tǒng)隨windows啟動而自動啟動

  1. 通過創(chuàng)建任務計劃, 在windows登錄的時候, 啟動 wsl2中的docker


    image.png
  2. 創(chuàng)建新的任務計劃


    創(chuàng)建任務

    常規(guī)

    觸發(fā)器

在操作tab頁中程序或腳本欄填入C:\Windows\System32\bash.exe蛇受,
添加參數欄填入-c "sudo /bin/sh /usr/local/sbin/start_docker.sh"

操作

只有計算機使用交流電.... 這個選項的勾給去掉

條件

  1. 重啟后直接用navicate或其它的工具鏈接mysql,主機填寫host文件中wsl2host給你自動生成的域名, 看是否可以成功連接兢仰,如果能乍丈,表示大功告成。


    連接mysql
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末把将,一起剝皮案震驚了整個濱河市轻专,隨后出現的幾起案子,更是在濱河造成了極大的恐慌察蹲,老刑警劉巖请垛,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異洽议,居然都是意外死亡宗收,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門亚兄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來混稽,“玉大人,你說我怎么就攤上這事审胚⌒傺” “怎么了?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵膳叨,是天一觀的道長洽洁。 經常有香客問我,道長菲嘴,這世上最難降的妖魔是什么饿自? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮龄坪,結果婚禮上璃俗,老公的妹妹穿的比我還像新娘。我一直安慰自己悉默,他們只是感情好城豁,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著抄课,像睡著了一般唱星。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上跟磨,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天间聊,我揣著相機與錄音,去河邊找鬼抵拘。 笑死哎榴,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播尚蝌,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼迎变,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了飘言?” 一聲冷哼從身側響起衣形,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎姿鸿,沒想到半個月后谆吴,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡苛预,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年句狼,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片热某。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡腻菇,死狀恐怖,靈堂內的尸體忽然破棺而出苫拍,到底是詐尸還是另有隱情,我是刑警寧澤旺隙,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布绒极,位于F島的核電站,受9級特大地震影響蔬捷,放射性物質發(fā)生泄漏垄提。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一周拐、第九天 我趴在偏房一處隱蔽的房頂上張望铡俐。 院中可真熱鬧,春花似錦妥粟、人聲如沸审丘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽滩报。三九已至,卻和暖如春播急,著一層夾襖步出監(jiān)牢的瞬間脓钾,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工桩警, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留可训,地道東北人。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像握截,于是被迫代替她去往敵國和親飞崖。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354