windows系统vscode远程调试MySQL
这里介绍如何使用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插件
在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
在 VSCode 主界面 ctrl+shift+p 选 Remote.SSH: Connect to host, 输入 root@<ip>
然后就可以从侧边栏打开项目路径了, 点击左边的资源管理器,点击打开文件夹,在下图蓝框处输入mysql源码路径(注意不是编译后的运行程序路径是源码解压路径,我的环境源码路径为/soft/mysql-5.7.26,这里我还向下选择sql目录,因为mysql的大多数核心代码在该目录下,如main函数)
安装一些需要的扩展, 如:
• C/C++ IntelliSense, debugging, and code browsing
使用 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, 将其修改如下:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"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编译后的启动程序
之后就可以使用调试功能了, 添加断点, 监视等等, 如图:
![](https://res.hc-cdn.com/ecology/9.3.130/v2_resources\images\global\blog-dialog.png)
华为开发者空间发布
让每位开发者拥有一台云主机
- 点赞
- 收藏
- 关注作者
评论(0)