windows下vscode连接Linux中的MySQL(非常实用)

于初学Linux系统的小伙伴来说,由于对Linux系统及其在windows下的环境配置比较生疏,所以当使用windows下vscode连接Linux中的MySQL,并进行相关研发时,颇为头疼;下面对其环境配置做出具体介绍!!!

  1. 安装Ubuntu及Linux系统

Ubuntu安装具体流程见:https://blog.youkuaiyun.com/weixin_47156401/article/details/120941578

  1. 在Linux下安装MySQL

Linux下MySQL的安装教程网上较多,这里就不一一赘述了!

3.安装vscode及相关插件

除了需要安装C++后端开发所需要的一些插件外,还需要安装MySql相关的插件,主要有:MySQL(Jun Han)、MySQL(Weijian Chen)、MySQL Syntax,当然也可以安装一些美化的插件,如:SQL Beautify;

  1. 在Linux下建立工程文件--->mySQL_test

  1. 使用vscode连接Ubuntu,并打开mySQL_test

在mySQL_test工程下简历test01.cpp文件,以进行测试,如下图所示:

测试代码如下:

#include<iostream>
#include<mysql.h>

using namespace std;

int main(){
    cout<<"hell0 mysql"<<endl;
    return 0;
}

5.在mySQL_test工程中生成环境配置文件

环境配置文件主要有:c_cpp_properties.json、tasks.json、launch.json;其相关介绍及其生成具体流程见:https://blog.youkuaiyun.com/weixin_47156401/article/details/129467510?spm=1001.2014.3001.5501

生成结果如下图所示:

(1)c_cpp_properties.json文件

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

(2)tasks.json文件

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

(3)launch.json文件

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) 启动",
            "type": "cppdbg",
            "request": "launch",
            "program": "输入程序名称,例如 ${workspaceFolder}/a.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                },
                {
                    "description": "将反汇编风格设置为 Intel",
                    "text": "-gdb-set disassembly-flavor intel",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

6.查询Linux下MySQL的相关路径

在Linux终端输入:

mysql_config

会出现如下界面,其中红色框选部分为Linux下MySQL的相关路径;

7.将MySQL的相关路径插入到vscode的配置文件中

(1)MySQL相关路径插入到c_cpp_properties.json文件中

(2)c_cpp_properties.json文件环境配置后代码

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/mysql"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "cStandard": "c17",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "linux-gcc-x64",
            "configurationProvider": "ms-vscode.makefile-tools"
        }
    ],
    "version": 4
}

(3)MySQL相关路径插入到tasks.json文件中

(4)tasks.json文件环境配置后代码

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-I",
                "/usr/include/mysql",
                "-L",
                "/usr/lib/x86_64-linux-gnu",
                "-lmysqlclient",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

说明:“-I”表示“Include”对应路径;“-L”表示“Lib”对应路径;和VS中配置Opencv道理相通;

8.配置结果

(1)配置前

(2)配置后

(3)代码运行结果

至此windows下vscode连接Linux中的MySQL环境配置完毕,如果工程中的源文件较多,可使用Makefile对其进行编译,具体操作流程见:https://blog.youkuaiyun.com/weixin_47156401/article/details/122033897

若有帮到你,点个赞,留下你的脚印哦!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

数据库内核

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值