WSL 2 ติดตั้งได้บน Windows 11 หรือ Windows 10 version 2004+ (Build 19041 and higher)
การติดตั้ง WSL 2
Open PowerShell or Windows Command Prompt in administrator mode by right-clicking and selecting “Run as administrator“
Enter the command: wsl --install
You’ll need to restart your machine following this installation process to begin using WSL.
คำสั่ง wsl --install จะติดตั้ง Ubuntu distribution เป็นค่า default แต่เราสามารถติดตั้ง Linux distributions อื่นได้
ดู Linux distribution ที่มีใน Microsoft Store ใช้คำสั่ง wsl --list --online
> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install <Distro>'.
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
OracleLinux_8_5 Oracle Linux 8.5
OracleLinux_7_9 Oracle Linux 7.9
SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4
openSUSE-Leap-15.4 openSUSE Leap 15.4
openSUSE-Tumbleweed openSUSE Tumbleweed
เลือก Linux distribution ที่จะติดตั้งด้วยคำสั่ง wsl --install --distribution <Distribution Name>
เปลี่ยน default Linux distribution ด้วย wsl --set-default <Distribution Name>
ที่ PowerShell เปิด Bash command line ไปที่ home directory ด้วย wsl ~ หรือ เปิด Bash command line โดยยังอยู่ที่พาทเดิมด้วย wsl และออกจาก Bash ด้วย exit
ที่ Bash เรียก Windows File Explorer ให้ชี้ไปที่ current directory path ด้วย explorer.exe . (อย่าลืมพิมพ์จุดด้วย)
คำสั่งของ Ubuntu ใน WSL2
คำสั่ง systemctl ใช้ไม่ได้
$ sudo systemctl status apache2
[sudo] password for jack:
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
$ gcloud version
Google Cloud SDK 334.0.0
alpha 2021.03.26
beta 2021.03.26
bq 2.0.66
core 2021.03.26
gsutil 4.60
View information about gcloud commands and other topics:
$ gcloud help
List accounts whose credentials are stored on the local system:
$ gcloud auth list
Credentialed Accounts
ACTIVE ACCOUNT
* example-user-1@gmail.com
example-user-2@gmail.com
To set the active account, run:
$ gcloud config set account `ACCOUNT`
List the properties in your active gcloud CLI configuration:
res = cur.execute("SELECT name FROM sqlite_master")
print(res.fetchone())
# ('movie',)
insert ข้อมูล
cur.execute("""
INSERT INTO movie VALUES
('Monty Python and the Holy Grail', 1975, 8.2),
('And Now for Something Completely Different', 1971, 7.5)
""")
con.commit()
select ข้อมูล
res = cur.execute("SELECT score FROM movie")
print(res.fetchall())
insert ข้อมูลด้วย executemany()
data = [
("Monty Python Live at the Hollywood Bowl", 1982, 7.9),
("Monty Python's The Meaning of Life", 1983, 7.5),
("Monty Python's Life of Brian", 1979, 8.0),
]
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", data)
con.commit() # Remember to commit the transaction after executing INSERT.
for row in cur.execute("SELECT year, title FROM movie ORDER BY year"):
print(row)
Verify that the database has been written to disk by calling con.close() to close the existing connection
BlazorServerApp6.Pages.Counter: Information: This is an INFORMATION message.
BlazorServerApp6.Pages.Counter: Warning: This is a WARNING message.
BlazorServerApp6.Pages.Counter: Error: This is an ERROR message.
using BlazorServerApp6.Data;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
var builder = WebApplication.CreateBuilder(args);
ILoggerFactory _loggerFactory = (ILoggerFactory)new LoggerFactory();
_loggerFactory.AddLog4Net();
builder.Services.AddSingleton(_loggerFactory);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapBlazorHub();
app.MapFallbackToPage("/_Host");
app.Run();