windows系統vscode遠端除錯MySQL

eric0435發表於2022-04-09

這裡介紹如何使用windows上的 vscode遠端訪問Linux的mysql原始碼來執行遠端除錯MySQL

1.在本地windows安裝openssh

下載安裝包OpenSSH-Win64.zip並解壓

進行解壓後的目錄F:\OpenSSH-Win64\OpenSSH

在cmd命令視窗中執行下面命令進行安裝

powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

開放22號埠(如果你在windows關閉了防火牆並配置了入站規則可以不執行如下命令,多執行不影響)

netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22

配置開機自啟sshd服務

sc config sshd start= auto

將F:\OpenSSH-Win64\OpenSSH新增到環境變數path中,免得每次都要切到F:\OpenSSH-Win64\OpenSSH才能使用ssh,啟動ssh服務

net start sshd

2.1在Windows上生成金鑰

C:\Users\Administrator>ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (C:\Users\Administrator\.ssh\id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in id_rsa.

Your public key has been saved in id_ras.pub.

The key fingerprint is:

SHA256:2ya72QX0JQBxKTLM+3S1H3FFYnHHrve1t0cSbqXJiqI mysql@gbase

The key’s randomart image is:

+—[RSA 2048]—-+

| o ooo. ++*|

| = …….o+|

| + ……+ |

| . …..oo o|

| oS.. .+ B |

| .o . X +|

| o o..o ++|

| .*… .+|

| E.+o. .+|

+—-[SHA256]—–+

2.2 在Linux伺服器上生成金鑰

[root@gbase ~]# su – mysql

Last login: Sun Sep 26 09:24:11 CST 2021 on pts/5

[mysql@gbase ~]$ pwd

/home/mysql

[mysql@gbase ~]$ ls -lrt

total 0

drwxrwxr-x. 2 mysql mysql 6 Sep 24 16:19 perl5

[mysql@gbase ~]$ cd ~/.ssh

-bash: cd: /home/mysql/.ssh: No such file or directory

[mysql@gbase ~]$ mkdir ~/.ssh

[mysql@gbase ~]$ cd ~/.ssh

[mysql@gbase .ssh]$ pwd

/home/mysql/.ssh

[mysql@gbase .ssh]$ ls -lrt

total 0

增加root許可權:visudo 進入文字後找到root ALL=(ALL) ALL,另起一行,加入admin ALL=(ALL) NOPASSWD:ALL su admin 切換到新使用者 再使

用sudo su -切換回root,說明許可權正常,進行下一步

[root@gbase ~]# visudo

## Allow root to run any commands anywhere

root ALL=(ALL) ALL

mysql ALL=(ALL) NOPASSWD:ALL

[mysql@gbase ~]$ sudo su –

Last login: Sun Sep 26 11:41:38 CST 2021 on pts/1

[root@gbase ~]# su – mysql

Last login: Sun Sep 26 14:20:50 CST 2021 from 192.168.1.12 on pts/3

[mysql@gbase ~]$

2.3將windows中生成的公鑰上傳到/home/mysql/.ssh/目錄

C:\Users\Administrator>scp C:\Users\Administrator\.ssh\id_rsa.pud mysql@192.168.1.249:/home/mysql/.ssh/authorized_keys

[mysql@gbase ~]$ chmod 700 /home/mysql/.ssh

[mysql@gbase ~]$ chmod 600 /home/mysql/.ssh/authorized_keys

2.4切換到root,關閉root登入 sudo su – vim /etc/ssh/sshd_config 找到#PermitRootLogin yes去掉#把yes改為no systemctl restart sshd 重啟服務並生效

[root@gbase .ssh]# vim /etc/ssh/sshd_config

# $OpenBSD: sshd_config,v 1.100 2016/08/15 12:32:04 naddy Exp $

# This is the sshd server system-wide configuration file. See

# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/local/bin:/usr/bin

# The strategy used for options in the default sshd_config shipped with

# OpenSSH is to specify options with their default value where

# possible, but leave them commented. Uncommented options override the

# default value.

# If you want to change the port on a SELinux system, you have to tell

# SELinux about this change.

# semanage port -a -t ssh_port_t -p tcp #PORTNUMBER

#

Port 22

#AddressFamily any

#ListenAddress 0.0.0.0

#ListenAddress ::

HostKey /etc/ssh/ssh_host_rsa_key

#HostKey /etc/ssh/ssh_host_dsa_key

HostKey /etc/ssh/ssh_host_ecdsa_key

HostKey /etc/ssh/ssh_host_ed25519_key

# Ciphers and keying

#RekeyLimit default none

# Logging

#SyslogFacility AUTH

SyslogFacility AUTHPRIV

#LogLevel INFO

# Authentication:

#LoginGraceTime 2m

PermitRootLogin no #改成no

#StrictModes yes

#MaxAuthTries 6

#MaxSessions 10

RSAAuthentication yes #要開啟

PubkeyAuthentication yes #要開啟

# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2

# but this is overridden so installations will only check .ssh/authorized_keys

AuthorizedKeysFile .ssh/authorized_keys

[root@gbase ~]# service sshd restart

Redirecting to /bin/systemctl restart sshd.service

在windows中測試ssh登入

C:\Users\Administrator>ssh mysql@192.168.1.249

Last login: Sun Sep 26 14:22:54 2021

]ysql@gbase:~[mysql@gbase ~]$ pwd

/home/mysql

給vscode安裝remote-development外掛
windows系統vscode遠端除錯MySQL

在Linux伺服器上編譯安裝mysql(注意編譯時需要開啟-DWITH_DEBUG=1)並且啟動例項

1.Linux系統上安裝相關開發工具

[root@gbase yum.repos.d]# yum install gdb-gdbserver

[root@gbase yum.repos.d]# yum install gdb

[root@localhost ~]# yum -y install gcc* gcc-c++ ncurses* ncurses-devel* cmake* bison* libgcrypt* perl* make*

mysql原始碼存放在/soft/mysql-5.7.26目錄

建立相關目錄,build用於編譯,build/data用來儲存資料庫檔案,build/etc用來儲存my.cnf配置檔案

[mysql@gbase mysql-5.7.26]$ mkdir -p build/{data,etc}

[mysql@gbase mysql-5.7.26]$ cd build

[mysql@gbase build]$ ls -lrt

total 0

drwxrwxr-x. 2 mysql mysql 6 Sep 27 07:40 data

drwxrwxr-x. 2 mysql mysql 6 Sep 27 07:40 etc

預編譯MySQL

[mysql@gbase mysql-5.7.26]$ cmake . -DCMAKE_INSTALL_PREFIX=/soft/mysql-5.7.26/build -DMYSQL_DATADIR=/soft/mysql-5.7.26/build/data -DMYSQL_UNIX_ADDR=/soft/mysql-5.7.26/build/mysql.sock -DWITH_BOOST=/soft/mysql-5.7.26/boost/boost_1_59_0 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DENABLE_DTRACE=0 -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci -DWITH_EMBEDDED_SERVER=1 -DWITH_DEBUG=1

編譯原始碼,這時使用-j 8選項來使用8個程式同時進行編譯

[mysql@gbase mysql-5.7.26]$ make -j 8

Building CXX object sql/CMakeFiles/sql.dir/auth/sha2_password_common.cc.o

[100%] Building CXX object sql/CMakeFiles/sql.dir/mysqld_daemon.cc.o

[100%] Generating ../archive_output_directory/mysqlserver_depends.c

Scanning dependencies of target mysqlserver

[100%] Building C object libmysqld/CMakeFiles/mysqlserver.dir/__/archive_output_directory/mysqlserver_depends.c.o

Linking C static library ../archive_output_directory/libmysqld.a

Merging library mysqlserver

Linking CXX static library ../archive_output_directory/libsql.a

[100%] Built target sql

Scanning dependencies of target pfs_connect_attr-t

Scanning dependencies of target mysqld

[100%] Building CXX object sql/CMakeFiles/mysqld.dir/main.cc.o

Linking CXX executable mysqld

[100%] [100%] [100%] Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/__/__/__/sql/sql_builtin.cc.o

Building CXX object storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/pfs_connect_attr-t.cc.o

Building C object storage/perfschema/unittest/CMakeFiles/pfs_connect_attr-t.dir/__/__/__/mysys/string.c.o

Linking CXX executable pfs_connect_attr-t

[100%] Built target mysqlserver

Scanning dependencies of target mysqltest_embedded

Scanning dependencies of target mysql_embedded

Scanning dependencies of target mysql_client_test_embedded

[100%] [100%] [100%] [100%] Building CXX object libmysqld/examples/CMakeFiles/mysqltest_embedded.dir/__/__/client/mysqltest.cc.o

Building CXX object libmysqld/examples/CMakeFiles/mysql_embedded.dir/__/__/client/completion_hash.cc.o

Building CXX object libmysqld/examples/CMakeFiles/mysql_embedded.dir/__/__/client/mysql.cc.o

[100%] Building CXX object libmysqld/examples/CMakeFiles/mysql_embedded.dir/__/__/client/readline.cc.o

Building C object libmysqld/examples/CMakeFiles/mysql_client_test_embedded.dir/__/__/testclients/mysql_client_test.c.o

Linking CXX executable mysql_embedded

Linking CXX executable mysqltest_embedded

Linking CXX executable mysql_client_test_embedded

[100%] Built target mysqltest_embedded

[100%] Built target mysql_embedded

[100%] Built target mysqld

[100%] Built target pfs_connect_attr-t

[100%] Built target mysql_client_test_embedded

安裝

[mysql@gbase mysql-5.7.26]$ make install

— Installing: /soft/mysql-5.7.26/build/mysql-test/./t/xa_prepared_binlog_off.test

— Installing: /soft/mysql-5.7.26/build/mysql-test/./t/xml.test

— Installing: /soft/mysql-5.7.26/build/mysql-test/./valgrind.supp

— Installing: /soft/mysql-5.7.26/build/mysql-test/./mtr

— Installing: /soft/mysql-5.7.26/build/mysql-test/./mysql-test-run

— Installing: /soft/mysql-5.7.26/build/mysql-test/./Makefile

— Installing: /soft/mysql-5.7.26/build/mysql-test/./cmake_install.cmake

— Installing: /soft/mysql-5.7.26/build/mysql-test/./CTestTestfile.cmake

— Installing: /soft/mysql-5.7.26/build/./COPYING-test

— Installing: /soft/mysql-5.7.26/build/./README-test

— Up-to-date: /soft/mysql-5.7.26/build/mysql-test/mtr

— Up-to-date: /soft/mysql-5.7.26/build/mysql-test/mysql-test-run

— Installing: /soft/mysql-5.7.26/build/mysql-test/lib/My/SafeProcess/my_safe_process

— Up-to-date: /soft/mysql-5.7.26/build/mysql-test/lib/My/SafeProcess/my_safe_process

— Installing: /soft/mysql-5.7.26/build/mysql-test/lib/My/SafeProcess/Base.pm

— Installing: /soft/mysql-5.7.26/build/support-files/mysqld_multi.server

— Installing: /soft/mysql-5.7.26/build/support-files/mysql-log-rotate

— Installing: /soft/mysql-5.7.26/build/support-files/magic

— Installing: /soft/mysql-5.7.26/build/share/aclocal/mysql.m4

— Installing: /soft/mysql-5.7.26/build/support-files/mysql.server

配置mysql引數,只是設定幾個簡單的mysql執行引數

[mysql@gbase mysql-5.7.26]$ cd build/etc

[mysql@gbase etc]$ pwd

/soft/mysql-5.7.26/build/etc

[mysql@gbase etc]$ vi my.cnf

[mysqld]

basedir=/soft/mysql-5.7.26/build/

datadir=/soft/mysql-5.7.26/build/data

bind-address=0.0.0.0

user=mysql

port=3306

log-error=/soft/mysql-5.7.26/build/data/mysql.err

pid-file=/soft/mysql-5.7.26/build/data/mysqld.pid

socket = /soft/mysql-5.7.26/build/data/mysql.sock

character-set-server=utf8mb4

default-storage-engine=INNODB

explicit_defaults_for_timestamp = true

[mysql@gbase build]$ bin/mysqld –defaults-file=etc/my.cnf –initialize-insecure

[mysql@gbase build]$ more data/mysql.err

2021-09-27T00:22:49.060540Z 0 [Warning] Changed limits: max_open_files: 1024 (requested 5000)

2021-09-27T00:22:49.060786Z 0 [Warning] Changed limits: table_open_cache: 431 (requested 2000)

2021-09-27T00:22:59.734768Z 0 [Warning] InnoDB: New log files created, LSN=45790

2021-09-27T00:23:01.122752Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2021-09-27T00:23:01.272820Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 12e47d2b-1f29-11ec-a401-005056a31fca.

2021-09-27T00:23:01.290654Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed’ cannot be opened.

2021-09-27T00:23:01.293896Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the –initialize-insecure option.

啟動mysql

[mysql@gbase build]$ bin/mysqld_safe –user=mysql &

[1] 23685

[mysql@gbase build]$ 2021-09-27T00:30:29.012767Z mysqld_safe Logging to ‘/var/log/mariadb/mariadb.log’.

2021-09-27T00:30:29.097420Z mysqld_safe Starting mysqld daemon with databases from /soft/mysql-5.7.26/build/data

2021-09-27T00:30:30.910622Z mysqld_safe mysqld from pid file /var/run/mariadb/mariadb.pid ended

配置自動啟動MySQL

[root@gbase log]# cp /soft/mysql-5.7.26/build/support-files/mysql.server /etc/init.d/mysqld

[root@gbase log]# systemctl start mysqld

[root@gbase log]# systemctl status mysqld

鈼[0m mysqld.service – LSB: start and stop MySQL

Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)

Active: active (running) since Mon 2021-09-27 08:33:57 CST; 6s ago

Docs: man:systemd-sysv-generator(8)

Process: 24236 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)

Tasks: 28

CGroup: /system.slice/mysqld.service

鈹溾攢24252 /bin/sh /soft/mysql-5.7.26/build//bin/mysqld_safe –datadir=/soft/mysql-5.7.26/build/data –pid-file=/soft/mysql-5.7.26/build/data/mysqld.pid

鈹斺攢24542 /soft/mysql-5.7.26/build/bin/mysqld –basedir=/soft/mysql-5.7.26/build/ –datadir=/soft/mysql-5.7.26/build/data –plugin-dir=/soft/mysql-5.7.26/build//lib/plugin –user=mysql –log-error=/var/log/mariadb/mariadb.l…

Sep 27 08:33:55 gbase systemd[1]: Starting LSB: start and stop MySQL…

Sep 27 08:33:57 gbase mysqld[24236]: Starting MySQL.. SUCCESS!

Sep 27 08:33:57 gbase systemd[1]: Started LSB: start and stop MySQL.

使用 VSCode 遠端訪問程式碼

首先改設定, 左下角 管理(?) -> 設定, 搜 “remote.SSH”, 勾選 remote.SSH.showLoginTerminal
windows系統vscode遠端除錯MySQL
在 VSCode 主介面 ctrl+shift+p 選 Remote.SSH: Connect to host, 輸入 root@
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL

然後就可以從側邊欄開啟專案路徑了, 點選左邊的資源管理器,點選開啟資料夾,在下圖藍框處輸入mysql原始碼路徑(注意不是編譯後的執行程式路徑是原始碼解壓路徑,我的環境原始碼路徑為/soft/mysql-5.7.26,這裡我還向下選擇sql目錄,因為mysql的大多數核心程式碼在該目錄下,如main函式)
windows系統vscode遠端除錯MySQL
安裝一些需要的擴充套件, 如:
? C/C++ IntelliSense, debugging, and code browsing
windows系統vscode遠端除錯MySQL
使用 VSCode 和 gdbserver 遠端除錯 mysql 程式碼

在遠端 Linux 機器上執行如下程式碼:

[mysql@gbase ~]$ gdbserver localhost:2333 /soft/mysql-5.7.26/build/bin/mysqld –defaults-file=/soft/mysql-5.7.26/build/etc/my.cnf

Process /soft/mysql-5.7.26/build/bin/mysqld created; pid = 28184

Listening on port 2333

遠端 Linux 機器 IP 是 192.168.1.249 gdbserver 的埠設為 2333, 在 VSCode 機器上, 進入選單 除錯 -> 新增配置, 會生成一個配置檔案 SSH之後的專案根目錄/.vscode/launch.json, 將其修改如下:
windows系統vscode遠端除錯MySQL
windows系統vscode遠端除錯MySQL
{

// Use IntelliSense to learn about possible attributes.

// Hover to view descriptions of existing attributes.

// For more information, visit:

“version”: “0.2.0”,

“configurations”: [

{

“name”: “gdb Remote Launch”,

“type”: “cppdbg”,

“request”: “launch”,

“program”: “/soft/mysql-5.7.26/build/bin/mysqld”,

“args”: [],

“stopAtEntry”: true,

“cwd”: “${workspaceFolder}”,

“environment”: [],

“externalConsole”: false,

“MIMode”: “gdb”,

“miDebuggerPath”: “/usr/bin/gdb”,

“miDebuggerArgs”: “gdb”,

“linux”: {

“MIMode”: “gdb”,

“miDebuggerPath”: “/usr/bin/gdb”,

“miDebuggerServerAddress”: “192.168.1.249:2333”,

},

“logging”: {

“moduleLoad”: false,

“engineLogging”: true,

“trace”: false

},

“setupCommands”: [

{

“description”: “Enable pretty-printing for gdb”,

“text”: “-enable-pretty-printing”,

“ignoreFailures”: true

}

]

}

]

}

幾個注意的地方:

? 應該是 “request”: “launch”, 不是 “attach”, 此後也並不需要記錄程式ID

? 需要填對 “miDebuggerServerAddress”: “192.168.1.248:2333”, 有這個設定才會開啟 gdb 遠端除錯

? “engineLogging”: true 可以看到 gdb 自身的詳細訊息

? 必須是 “externalConsole”: false 否則報錯

? /soft/mysql-5.7.26/build/bin/mysqld 在 gdbserver 和 launch.json 裡都要填一次,即mysql編譯後的啟動程式

之後就可以使用除錯功能了, 新增斷點, 監視等等, 如圖:
windows系統vscode遠端除錯MySQL


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26015009/viewspace-2886316/,如需轉載,請註明出處,否則將追究法律責任。

相關文章