0%

WSL

Windows Subsystem for Linux(简称WSL)是一个在Windows 10\11上能够运行原生Linux二进制可执行文件(ELF格式)的兼容层。它是由微软与Canonical公司合作开发,开发人员可以在 Windows计算机上同时访问 Windows和Linux的强大功能。通过适用于Linux的Windows子系统(WSL),开发人员可以安装Linux发行版(例如 Ubuntu、OpenSUSE、Kali、Debian、Arch Linux等),并直接在Windows上使用Linux应用程序、实用程序和Bash命令行工具,不用进行任何修改,也无需承担传统虚拟机或双启动设置的费用。

WSL安装使用

一、安装

  1. 安装Terminal(非必须)
  2. 安装WSL(win10自带)
  3. 安装虚拟平台(启用WSL功能):wsl --install

二、使用

wsl –hep

  1. 查看列表:wsl -l
    • 包含状态信息:wsl -l -v
  2. 安装系统
  1. 进入系统:wsl -d Debian

    • 只有一个则只需:wsl
    • 多个则指定-d参数
  2. 关闭系统:wsl –shutdown

    • wsl -d Ubuntu-22.04 –shutdown
  3. 设置默认:wsl -s Debian

  4. 设置代理

  1. 取消代理
  • unset http_proxy
  • unset https_proxy
  1. 卸载系统:wsl –unregister Ubuntu-24.04

三、问题

  1. 安装多个相同版本的Linux
  2. 安装多个Linux发行版,ip地址一样

四、参考

  1. issues
  2. basic-commands

以Ubuntu-24.04为例

一、安装

  1. wsl –install Ubuntu-24.04

  2. wsl,进入系统

  • wsl –user root,以root用户进入系统
  1. 设置用户名和密码:liusir/123456

  2. sudo su -

  • 123456
  1. passwd root
  • 123456
  1. ifconfig
  • Command ‘ifconfig’ not found, but can be installed with:
    • apt install net-tools
  1. apt update

  2. apt upgrade

  3. systemctl start sshd

  • Failed to start sshd.service: Unit sshd.service not found.
    • apt install openssh-server
    • systemctl enable ssh.service
    • systemctl list-units –type=service | grep ssh,查看已安装的服务
    • vim /etc/ssh/sshd_config
      • PermitRootLogin yes
    • service sshd restart
    • 卸载服务
      • apt remove openssh-server
      • apt autoremove
  1. usermod -l liuyulong liusir,修改用户名

  2. userdel liuyulong

  3. groupdel liusir

  4. cd /home && rm -rf liusir

  5. useradd liusir

  6. passwd liusir

  7. useradd liuyulong

  • ssh登录报错
    • /home/liusir/.hushlogin file.
    • touch: cannot touch ‘/home/liusir/.motd_shown’: No such file or directory
    • cd /home
    • mkhomedir_helper liusir
    • mkhomedir_helper liuyulong
      • useradd liusir -m
      • useradd liuyulong -m
      • usermod -s /bin/bash liusir
      • usermod -s /bin/bash liuyulong
  • 再次登录,报错:/usr/bin/xauth: file /home/liusir/.Xauthority does not exist
    • 在对应主目录下创建:touch .Xauthority
  1. 卸载Ubuntu-24.04: wsl –unregister Ubuntu-24.04

二、其他

  1. 安装ssh服务
  • 安装服务:apt-get install openssh-server
  • 启动服务:systemctl start ssh
  • 开机启动:systemctl enable ssh
    • service ssh start
    • service ssh status
  1. 设置源
  • 备份:cp /etc/apt/sources.list /etc/apt/sources.list.bak
  • 修改:vim sources.list
1
2
3
4
5
6
7
8
9
10
deb http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ trusty-backports main restricted universe multiverse

安装多个相同发行版本的Linux

一、操作

  1. 下载:https://github.com/DDoSolitary/LxRunOffline/releases/download/v3.5.0/LxRunOffline-v3.5.0-msvc.zip

  2. 解压

  3. 执行:LxRunOffline.exe d -n Ubuntu-24.04 -d C:\WSL\Ubuntu-24.04-02 -N Ubuntu-24.04-02

  • 报错:[ERROR] The distro "Ubuntu-24.04-02" already exists.
  • 解决:wsl –unregister Ubuntu-24.04-02,卸载后重新复制
  1. 修改hostname:/etc/hostname
  • sudo hostnamectl set-hostname ubuntu-01
  • sudo hostnamectl set-hostname ubuntu-02
  1. 修改hosts:/etc/hosts
  • 127.0.1.1 ubuntu-01
  • 127.0.1.1 ubuntu-02
  1. 退出重进

二、问题

  1. 复制报错,多半为参数问题

  2. 复制后ip/mac什么的都一样

  • 修改IP地址和MAC地址

    • sudo ifconfig eth0 192.168.1.2 netmask 255.255.255.0

    • sudo route add default gw 192.168.1.1 dev eth0

    • sudo ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE

      • 报错:SIOCSIFHWADDR: Device or resource busy - you may need to down the interface
      • 处理
        • sudo ifconfig eth0 down
        • sudo ifconfig eth0 hw ether 00:AA:BB:CC:DD:EE
        • sudo ifconfig eth0 up
    • sudo ifconfig eth0 192.168.1.3 netmask 255.255.255.0

    • sudo route add default gw 192.168.1.1 dev eth0

    • sudo ifconfig eth0 hw ether 00:A1:B1:C1:D1:E1

      • sudo ifconfig eth0 down
      • sudo ifconfig eth0 hw ether 00:A1:B1:C1:D1:E1
      • sudo ifconfig eth0 up

三、参考

  1. https://github.com/DDoSolitary/LxRunOffline

Ubuntu-安装MySQL

  1. 目录规划

    • 安装目录:/usr/local/mysql
    • 数据目录:/mysql/data
    • 错误日志:/tmp/mysql/
    • 慢查询日志:/tmp/mysql/
    • socks目录:/var/run/mysqld/
  2. 用户设置和目录设置

    • groupadd mysql
    • useradd -r -g mysql mysql
    • 准备数据目录 mkdir -p /data/mysql
    • 赋予权限 chown mysql.mysql /data/mysql
  3. 安装

    • apt-get update
    • apt-get install cmake openssl libssl-dev libncurses5-dev libncursesw5-dev build-essential pkg-config git net-tools -f build-essential libncurses5-dev libncurses5
    • wget https://downloads.mysql.com/archives/get/p/23/file/mysql-boost-5.7.44.tar.gz
    • 解压源码至/usr/local/mysql-5.7.44
    • cmake . -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/mysql-5.7.44/boost/boost_1_59_0
    • make && make install
      • 有时候安装失败或想更换安装选项,重新设置后make clean
    • 切换到安装目录 cd /usr/local/mysql
    • sudo cp support-files/mysql.server /etc/init.d/mysqld
    • sudo chmod 700 /etc/init.d/mysqld
    • sudo chown -R mysql:mysql /usr/local/mysql
    • 修改配置文件,默认/etc/mysql/conf.d/mysql.cnf
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [client]
    port=3306

    [mysql]
    default-character-set=utf8mb4

    [mysqld]
    port = 3306
    socket = /var/run/mysqld/mysql.sock #有坑
    pid-file = /tmp/mysql/mysql.pid
    basedir = /usr/local/mysql
    datadir = /data/mysql
    tmpdir = /tmp/mysql
    character_set_server = utf8mb4
    collation_server = utf8mb4_bin
    log-error=/tmp/mysql/error.log
    secure-file-priv = /tmp/mysql/
    # bind-address=localhost # MySQL绑定IP
    wait_timeout = 86400
    binlog_cache_size = 16M
    slow_query_log = true
    slow_query_log_file = /tmp/mysql/slow_query_log.log
    long_query_time = 1
    • sudo mysqld –defaults-file=/etc/mysql/my.cnf –user=root –initialize,初始化数据库并得到初始密码:gae>=TdlA3Ff
    • 启动MySQL:sudo service mysqld start/sudo systemctl start mysql.service
    • 关闭MySQL:sudo service mysqld stop/sudo systemctl stop mysql.service
    • 登录客户端:mysql -h 127.0.0.1 -uroot -p
    • 修改root密码:alter user ‘root’@’localhost’ identified by ‘root’;

搭建MySQL主从

一、主库

  1. 主库设置:my.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 在 [mysqld] 中增加以下配置项 
server_id=1
# 复制过滤:需要备份的数据库,输出 binlog
#binlog-do-db=db_admin
# 复制过滤:不需要备份的数据库,不输出(mysql 库一般不同步)
binlog-ignore-db=mysql
# 开启二进制日志功能,可以随便取,最好有含义
log-bin=lyz-mysql-bin
## 为每个 session 分配的内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M
## 主从复制的格式(mixed,statement,row,默认格式是 statement)
binlog_format=mixed
# 二进制日志自动删除/过期的天数。默认值为 0,表示不自动删除。
expire_logs_days=7
# 跳过主从复制中遇到的所有错误或指定类型的错误,避免 slave 端复制中断。
# 如:1062 错误是指一些主键重复,1032 错误是因为主从数据库数据不一致
slave_skip_errors=1062
  1. 登录主库,设置复制账号
  • grant replication slave, replication client on . to ‘repl’@’127.0.0.1’ identified by ‘123456’;
  • flush privileges;
  1. 查看file和position
  • show master status;
1
2
3
4
5
6
7
mysql> show master status \G;
*************************** 1. row ***************************
File: mysql-bin.000207
Position: 484110173
Binlog_Do_DB: db_1,db_2,db_3,test
Binlog_Ignore_DB: information_schema,performance_schema,mysql
Executed_Gtid_Set:
  1. 备份主库
  • mysqldump -uroot -p db_admin >/tmp/db_admin.sql
  • XtraBackup
  1. 将备份的主库复制到从库

二、从库

  1. 修改从库配置my.conf
1
2
3
4
5
6
7
8
9
server_id=132
binlog-ignore-db=mysql
log-bin=lyz-mysql-slave1-bin
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=lyz-mysql-relay-bin
log_slave_updates=1
read_only=1
  1. 启动从库:service mysqld start
  2. 创建要复制的库
  3. 导入主库的sql
  4. 设置同步参数:change master to master_host=’127.0.0.1’,master_user=’repl’,master_password=’123456’,master_port=3306, master_log_file=’sync-mysql-bin.000002’,master_log_pos=154,master_connect_retry=30;
  5. 查看同步状态:show slave status;
  6. 开启同步:start slave;
  7. 再次查看同步状态:show slave status;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.20.44
Master_User: replication_user
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000207
Read_Master_Log_Pos: 483988407
Relay_Log_File: center-rds3-relay-bin.000420
Relay_Log_Pos: 483988620
Relay_Master_Log_File: mysql-bin.000207
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: db_1,db_2,db_3,test
Replicate_Ignore_DB: mysql
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 483988407
Relay_Log_Space: 483988880
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 2044
Master_UUID: 6bc97a8a-092d-11ef-aaf1-fa163eb2f36a
Master_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:

三、验证

  1. 插入记录
  2. 删除记录
  3. 更新记录
  4. 创建索引
  5. 创建表
  6. 修改表

四、问题

  1. 复制断了
1
2
3
4
5
6
show slave status\G;
STOP SLAVE;
RESET SLAVE;
CHANGE MASTER TO master_log_file='mysql-bin.000069', master_log_pos=941883577;
START SLAVE;
等到 Seconds_Behind_Master: 0,就是同步到最新
  1. 磁盘满了
  2. 主从复制延迟

Ubuntu-搭建GitLab

一、操作

  1. sudo apt-get update

  2. sudo apt-get install -y curl openssh-server ca-certificates tzdata perl

  3. curl -L get.gitlab.cn | bash

  4. sudo EXTERNAL_URL=”https://gitlab.liusir.me" apt-get install -y gitlab-jh

  5. 修改hosts:172.19.158.177 gitlab.liusir.me

    • 172.19.158.177是安装的Linux地址
  6. 安装成功,访问:https://gitlab.liusir.me

    • 默认用户:root
    • 默认密码:/etc/gitlab/initial_root_password
  7. 卸载:sudo gitlab-ctl stop && sudo gitlab-ctl remove-accounts

  8. 登录gitlab后台

  9. 修改密码

  10. 新建群组

  11. 新建项目

二、问题

  1. gitlab-ctl reconfigure

  2. SSL certificate problem: self-signed certificate

    • export GIT_SSL_NO_VERIFY=true
      • Windows: set GIT_SSL_NO_VERIFY=true git clone
      • Linux:env GIT_SSL_NO_VERIFY=true git push
    • git config –global http.sslVerify “false”

三、参考

  1. 参考一

Windows下开发,WSL下运行

一、VS Code

  1. 装扩展:扩展面板搜 Remote - WSL(发布者是 Microsoft,ID ms-vscode-remote.remote-wsl)
  2. 在WSL下进入项目目录
  3. code .
    • 第一次打开会提示下载组件
    • 左下角出现 WSL: OS-xx(如Ubuntu-24.04) 就是成功了
  4. 命令模式下:WSL: Reopen Folder in WSL补救
    • Ctrl + Shift + P

二、phpstorm

  1. 打开phpstorm
  2. File > Open,选择对应的路径
  3. 配置WSL解释器
    • Ctrl + Alt + S > PHP > CLI 解释器 > … > 左上角+ > 选 From Docker, Vagrant, VM, WSL, Remote… → 方法选 WSL:
  4. php -v,验证配置是否成功