X DevAPI mysqlx::Session() over linux socket 失败并显示“CDK 错误:意外消息”
Posted
技术标签:
【中文标题】X DevAPI mysqlx::Session() over linux socket 失败并显示“CDK 错误:意外消息”【英文标题】:X DevAPI mysqlx::Session() over linux socket fails with “CDK Error: unexpected message” 【发布时间】:2021-01-16 11:06:21 【问题描述】:我已经 (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)>
【问题讨论】:
你的 mysql 是否配置为询问 root 密码? (除了sudo
。)换句话说,当你用sudo mysql --socket /var/run/mysqld/mysqld.sock -u root
连接时,mysql要求root
密码?
@Manual:感谢您的回复。不,不是的。我也安装了没有密码的mysql。
那么我会说这是您的标头/库之间的版本不匹配。 X插件也应该在服务器中激活(默认情况下)。你可以check。
试试这个:“默认的 mysqlx_socket 路径和文件名基于 MySQL 服务器的主套接字文件的默认路径和文件名,并在文件名后附加了一个 x。主套接字文件的默认路径和文件名是 /tmp/mysql.sock,因此 X Plugin 套接字文件的默认路径和文件名是 /tmp/mysqlx.sock" 试试doc.
我猜你已经在 TCP 连接上设置了 root db 用户的权限,但在套接字连接上没有设置权限。
【参考方案1】:
让我以如何让最小程序从头开始工作的方法来回答我自己的问题。数字指问题中的数字。
(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
$
【讨论】:
:-) 太棒了!以上是关于X DevAPI mysqlx::Session() over linux socket 失败并显示“CDK 错误:意外消息”的主要内容,如果未能解决你的问题,请参考以下文章
运行任意 SQL 命令 MySQL C++ (X DevAPI)?
如何设置 MySQL 连接器/Python X DevAPI 连接的选项?
如何在 Mysql X DevAPI 中查询 Null 或 Missing 字段?
使用SpringBoot和X DevApi构建真异步的API
您如何为 MySQL X DevAPI 使用 Java 连接池?
PHP MySQL X DevAPI Collection::existsInDatabase() 方法无法识别带有附加列的集合