前置準備
windows系統(tǒng):windows 10 專業(yè)版(家庭版無法開啟虛擬機)
windows用戶:Administrator (普通用戶可能會有莫名其妙的一些問題)
WIN10 開啟 wsl2 支持
-
找到windows控制面板中的程序和功能派昧,點擊啟動或關閉windows功能
勾選Hyper-v和適用于Linux的windows子系統(tǒng)
下載適用于 x64 計算機的 WSL2 Linux 內核更新包
運行上一步中下載的更新包進行安裝黔姜。
安裝完成后,設置 wsl 默認版本為 2
wsl --set-default-version 2
安裝ubuntu
-
打開win10應用商店蒂萎,搜索ubuntu秆吵,選第一個安裝。
安裝ubuntu
查看ubuntu 的wsl版本
wsl -l -v
version 為 2 表示 ubuntu 是以 wsl2 運行的五慈。如果這里是 1帮毁,可以再次運行如下命令
wsl --set-version Ubuntu 2
安裝Windows Terminal (可選)
-
打開應用商店,搜索windows terminal豺撑,選第一個安裝。
windows terminal
windows terminal
安裝gsudo https://github.com/gerardog/gsudo (可選)
- 打開windos terminal黔牵,在powershell中執(zhí)行下面的命令安裝gsudo
PowerShell -Command "Set-ExecutionPolicy RemoteSigned -scope Process; iwr -useb https://raw.githubusercontent.com/gerardog/gsudo/master/installgsudo.ps1 | iex"
- 打開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
就好了
這樣以后再打開 powershell 或 cmd 就是以管理員身份運行的了陆错。
ubuntu 替換阿里源
- 進入linux子系統(tǒng)
輸入用戶名:hezhigang (以hezhigang為例)
輸入密碼:**** - 查看一下ubuntu 版本
lsb_release -c
focal
代表ubuntu20.04eoan
代表ubuntu19.10disco
代表ubuntu19.04bionic
代表ubuntu18.04xenial
代表ubuntu16.04
- 原來的軟件源并另存
sudo cp -v /etc/apt/sources.list /etc/apt/sources.list.backup
- 執(zhí)行chmod命令更改文件權限使軟件源文件可編輯
sudo chmod 777 /etc/apt/sources.list
- 替換成阿里源
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
- 按照官網文檔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
- 安裝mysql docker鏡像測試一下
查詢mysql鏡像金赦,我這里查詢mysql-employee音瓷,這是一個帶實例employee示例數據庫的版本
sudo docker search mysql-employee
選第一個 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
-
下載
以管理員方式運行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文件度液,看其中是否多了下圖中的一行
此后厕宗,每次wsl重啟,wsl2host都會自動把wsl中的IP更新到host文件中堕担,我們只要使用ubuntu.wsl
即可訪問到wsl中的docker服務了已慢。
讓docker服務隨windows系統(tǒng)啟動而自動啟動
一、讓docker服務器隨ubuntu 系統(tǒng)啟動而自動啟動
- 把當前用戶hezhigang加入 docker 用戶組
sudo usermod -aG docker hezhigang
- wsl2中創(chuàng)建啟動腳本
sudo vim /usr/local/sbin/start_docker.sh
- 然后, 把以下內容添加到腳本中
#!/usr/bin/env bash
sudo cgroupfs-mount
sudo service docker start
- 給腳本添加執(zhí)行權限
sudo chmod 755 /usr/local/sbin/start_docker.sh
- 給當前用戶添加權限, 使在使用 sudo命令的時候不需要輸入密碼
sudo vim /etc/sudoers
在打開的文本中找到 %sudo ALL=(ALL:ALL) ALL
這一行后面添加如下代碼
hezhigang ALL=(ALL:ALL) NOPASSWD: ALL
二霹购、讓ubuntu系統(tǒng)隨windows啟動而自動啟動
-
通過創(chuàng)建任務計劃, 在windows登錄的時候, 啟動 wsl2中的docker
image.png -
創(chuàng)建新的任務計劃
創(chuàng)建任務
常規(guī)
觸發(fā)器
在操作tab頁中程序或腳本欄填入C:\Windows\System32\bash.exe
蛇受,
添加參數欄填入-c "sudo /bin/sh /usr/local/sbin/start_docker.sh"
把 只有計算機使用交流電....
這個選項的勾給去掉
-
重啟后直接用navicate或其它的工具鏈接mysql,主機填寫host文件中wsl2host給你自動生成的域名, 看是否可以成功連接兢仰,如果能乍丈,表示大功告成。
連接mysql