gpt4 book ai didi

c++ - X DevAPI mysqlx::Session() over linux socket 失败并显示 “CDK Error: unexpected message”

转载 作者:行者123 更新时间:2023-12-04 19:07:20 26 4
gpt4 key购买 nike

我已经 (3) 新安装了 mysql,(4) 新安装了 Connector/C++ 8.0 X DevAPI,(5) 在 my.cnf 中进行了最小配置,(1) 编写了一个最小 C++ 程序,(2) 构建它,然后执行它给出了以下输出:

$ sudo bin/sql
[sudo] password for xxxxxx:
CDK Error: unexpected message
但是当我使用 mysql 时,它可以正确连接:
$ sudo mysql --socket /var/run/mysqld/mysqld.sock -u root
<…>
Server version: 8.0.22 MySQL Community Server – GPL
<…>
mysql>
在我的想象中,我的最小 C++ 程序与 mysql 客户端应用程序正在做同样的事情,但显然不是。我在这里做错了什么?
以下是我在开场白中提到的一些细节。
(1) 最小的 C++ 程序:
#include <iostream>
#include <stdexcept>
#include <mysqlx/xdevapi.h>

int main()
{
try
{
mysqlx::Session sess("mysqlx://root@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock");
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
(2) 构建:
g++ -std=gnu++17 -I/usr/include/mysql-cppconn-8 sql.cpp -lstdc++fs  -lmysqlcppconn8 -o bin/sql
chmod u+x bin/sql
(3) 我已经安装了mysql:
<download from  https://dev.mysql.com/downloads/repo/apt/>
$ sudo dpkg -i ~/Downloads/mysql-apt-config_0.8.16-1_all.deb
$ sudo apt-get update
$ sudo apt-get install mysql-server
<leave pwd blank>
$ systemctl status mysql
<active (running)>
$ mysql -V
mysql Ver 8.0.22 for Linux on x86_64 (MySQL Community Server - GPL)
(4)安装了Connector/C++ 8.0 X DevAPI(当我没记错的时候)
<download from  https://dev.mysql.com/downloads/connector/cpp/>
<chose Ubuntu Linux>
<Chose Ubuntu Linux 20.04 (x86, 64-bit)
<downloaded libmysqlcppconn8-2_8.0.22-1ubuntu20.04_amd64.deb>
$ sudo dpkg -i ~/Downloads/libmysqlcppconn8-2_8.0.22-1ubuntu20.04_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libmysqlcppconn8-2
<downloaded libmysqlcppconn8-2-dbgsym_8.0.22-1ubuntu20.04_amd64.deb>
$ sudo dpkg -i ~/Downloads/libmysqlcppconn8-2-dbgsym_8.0.22-1ubuntu20.04_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libmysqlcppconn8-2-dbgsym
<downloaded libmysqlcppconn-dev_8.0.22-1ubuntu20.04_amd64.deb>
$ sudo dpkg -i ~/Downloads/libmysqlcppconn-dev_8.0.22-1ubuntu20.04_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libmysqlcppconn-dev
<downloaded libmysqlcppconn7_8.0.22-1ubuntu20.04_amd64.deb>
$ sudo dpkg -i ~/Downloads/libmysqlcppconn7_8.0.22-1ubuntu20.04_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libmysqlcppconn7
<downloaded libmysqlcppconn7-dbgsym_8.0.22-1ubuntu20.04_amd64.deb>
$ sudo dpkg -i ~/Downloads/libmysqlcppconn7-dbgsym_8.0.22-1ubuntu20.04_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libmysqlcppconn7-dbgsym
(5) 配置my.cnf并添加【client】和socket
$ sudo vi /etc/mysql/my.cnf
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
<…>
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/

[client]
socket = /var/run/mysqld/mysqld.sock
<save>
$ systemctl stop mysql
$ systemctl start mysql
$ systemctl status mysql
<active (running)>

最佳答案

让我以如何让最小程序从头开始工作的方法来回答我自己的问题。数字是指问题中的数字。
(3)安装mysql:

<download from  https://dev.mysql.com/downloads/repo/apt/>
$ sudo dpkg -i ~/Downloads/mysql-apt-config_0.8.16-1_all.deb
$ sudo apt-get update
$ sudo apt-get install mysql-server
<leave pwd blank>
$ systemctl status mysql
<active (running)>
$ mysql -V
mysql Ver 8.0.22 for Linux on x86_64 (MySQL Community Server - GPL)
(4) 安装 Connector/C++ 8.0 X DevAPI (在我的例子中是 Ubuntu Linux 20.04)
<download from  https://dev.mysql.com/downloads/connector/cpp/>
<chose Ubuntu Linux>
<Chose Ubuntu Linux 20.04 (x86, 64-bit)
<downloaded libmysqlcppconn8-2_8.0.22-1ubuntu20.04_amd64.deb>
<downloaded libmysqlcppconn8-2-dbgsym_8.0.22-1ubuntu20.04_amd64.deb>
<downloaded libmysqlcppconn-dev_8.0.22-1ubuntu20.04_amd64.deb>
<downloaded libmysqlcppconn7_8.0.22-1ubuntu20.04_amd64.deb>
<downloaded libmysqlcppconn7-dbgsym_8.0.22-1ubuntu20.04_amd64.deb>
$ sudo dpkg -i ~/Downloads/libmysqlcppconn8-2_8.0.22-1ubuntu20.04_amd64.deb
$ sudo dpkg -i ~/Downloads/libmysqlcppconn8-2-dbgsym_8.0.22-1ubuntu20.04_amd64.deb
$ sudo dpkg -i ~/Downloads/libmysqlcppconn-dev_8.0.22-1ubuntu20.04_amd64.deb
$ sudo dpkg -i ~/Downloads/libmysqlcppconn7_8.0.22-1ubuntu20.04_amd64.deb
$ sudo dpkg -i ~/Downloads/libmysqlcppconn7-dbgsym_8.0.22-1ubuntu20.04_amd64.deb
$ sudo apt-get update
$ sudo apt-get install libmysqlcppconn8-2
$ sudo apt-get install libmysqlcppconn8-2-dbgsym
$ sudo apt-get install libmysqlcppconn-dev
$ sudo apt-get install libmysqlcppconn7
$ sudo apt-get install libmysqlcppconn7-dbgsym
(5) 配置my.cnf并添加【client】和socket
(5)不要更改my.cnf,不需要
(+) 创建具有所有权限的用户并检查套接字路径
$ sudo mysql -u root
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON * . * TO 'user'@'localhost';
mysql> FLUSH PRIVILEGES;
mysql> show variables like 'mysqlx_socket';
+---------------+-----------------------------+
| Variable_name | Value |
+---------------+-----------------------------+
| mysqlx_socket | /var/run/mysqld/mysqlx.sock |
+---------------+-----------------------------+
1 row in set (0.00 sec)
mysql> exit
<check:>
$ sudo mysql -u user -p
<first password is sudo password>
<second pass is sql password for user>
mysql> exit
(1) 编写最小的 C++ 程序 (sql.cpp)。将上面 mysqlx_socket 中的所有 '/' 替换为 '%2F':
#include <iostream>
#include <stdexcept>
#include <mysqlx/xdevapi.h>

int main()
{
try
{
// wrong: mysqlx::Session sess("mysqlx://root@%2Fvar%2Frun%2Fmysqld%2Fmysqld.sock"); replace d by x
mysqlx::Session sess("mysqlx://user:password@%2Fvar%2Frun%2Fmysqld%2Fmysqlx.sock");
}
catch(std::exception& e)
{
std::cerr << e.what() << std::endl;
}
}
(2) 构建:
g++ -std=gnu++17 -I/usr/include/mysql-cppconn-8 sql.cpp -lstdc++fs  -lmysqlcppconn8 -o sql
chmod u+x sql
(+) 执行它应该不会导致任何异常:
$ ./sql
$

关于c++ - X DevAPI mysqlx::Session() over linux socket 失败并显示 “CDK Error: unexpected message”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65748942/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com