Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

c++ - X DevAPI mysqlx::Session() over linux socket fails with “CDK Error: unexpected message”

I’ve (3) freshly installed mysql, (4) freshly installed Connector/C++ 8.0 X DevAPI, (5) did minimal configuration in my.cnf, (1) wrote a minimal C++ program, (2) build it, and after executing it gave the following output:

$ sudo bin/sql
[sudo] password for xxxxxx: 
CDK Error: unexpected message

But when I use mysql it connects properly:

$ sudo mysql --socket /var/run/mysqld/mysqld.sock -u root
<…>
Server version: 8.0.22 MySQL Community Server – GPL
<…>
mysql>

In my imagination, my minimal C++ program is doing the same the mysql client application is doing, but apparently not. What am I doing wrong here? Below are some details which have been referred to in my opening sentence.

(1) The minimal C++ program:

#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) Build with:

g++ -std=gnu++17 -I/usr/include/mysql-cppconn-8 sql.cpp -lstdc++fs  -lmysqlcppconn8 -o bin/sql
chmod u+x bin/sql

(3) I’ve installed 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) Installed Connector/C++ 8.0 X DevAPI (when I recall correctly)

<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) Configured my.cnf and added [client] and 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)>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Let me answer my own question in the form of a recipe how to get the minimal program working from scratch. The numbers refer to the numbers in the question.

(3) Install 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) Install Connector/C++ 8.0 X DevAPI (in my case 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) Configured my.cnf and added [client] and socket

(5) Do not change my.cnf, it is not needed

(+) Create a user with all privileges and check the socket path

$ 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) Write minimal C++ program (sql.cpp). Replace in above mysqlx_socket all '/' by '%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) Build with:

g++ -std=gnu++17 -I/usr/include/mysql-cppconn-8 sql.cpp -lstdc++fs  -lmysqlcppconn8 -o sql
chmod u+x sql

(+) Executing it should lead to no exceptions:

$ ./sql
$

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...