1

Problem

I'm following tutorial on Connect/C++ provided by dev.mysql. After copying the example and successfully compiling and linking it, I run into following issue:

terminate called after throwing an instance of 'sql::SQLException'

what(): Can't connect to MySQL server on '127.0.0.1' (111)

From what I know 111 stands for Permission Denied.

Setup

Here is the example code taken from here and slightly modified:

#include <mysql_driver.h>

int main()
{
    auto driver = sql::mysql::get_mysql_driver_instance();
    auto con = driver->connect("tcp://127.0.0.1:0", "root", "123");

    delete con;
}

Trying with tcp://127.0.0.1:3306 give the same result.

Here is the CMakeLists.txt:

cmake_minimum_required(VERSION 3.9)
project(mysql_try)

set(CMAKE_CXX_STANDARD 14)

set(SOURCE_FILES main.cpp)
add_executable(mysql_try ${SOURCE_FILES})

target_include_directories(mysql_try PUBLIC /usr/local/include/mysql++)
target_link_libraries(mysql_try mysqlclient /usr/local/lib/libmysqlcppconn-static.a pthread dl)

I can easily run mysql with the credentials in the code. I disabled networking, so that it would only listen on localhost. Running

mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';

gave me 0, so I guess it is port 0.

C Connector and command line interfaces work just fine.

My OS is ubuntu 16.04.

12
  • Is the mysql server running?
    – Galik
    Commented Sep 8, 2017 at 17:49
  • @Galik, yes. As I mentioned, I can easily run mysql and perform queries. Commented Sep 8, 2017 at 17:49
  • Port 0? Are you sure that's correct?
    – Galik
    Commented Sep 8, 2017 at 17:51
  • @Galik, the query gave me that, so I guess it is correct. It is depicted in the ASCII art table that the query gave me. 0 is written to the right of port. Commented Sep 8, 2017 at 17:51
  • I would check your server configurtion to see what port mysql is running on. But if the commandline works it is probably the default one
    – Galik
    Commented Sep 8, 2017 at 17:53

1 Answer 1

3

I disabled networking, so that it would only listen on localhost.

There's a difference between disabling networking all together (tcp) and specifying tcp to listen ONLY on your loopback adapter (127.0.0.1). Your mysql client is almost certainly connecting via Unix Socket, not TCP, which is why that works fine. Verify that by running mysql --protocol tcp. If that fails, then you know you disabled TCP connections all together, and it's connecting via Unix Socket.

connect("tcp://127.0.0.1:0"

If you did disable networking with skip-networking you need to change your connect() to reference your socket file instead. See the docs

unix://path_name

This URL format enables use of Unix domain socket files for connections to the local host on Unix and Unix-like systems. The path_name value is the socket file path name, just as for the --socket option of MySQL clients such as mysql and mysqladmin running on Unix (see Connecting to the MySQL Server).

Find your socket with:

mysql> SHOW GLOBAL VARIABLES LIKE 'socket'

If you want to keep your connect string the same, remove skip-networking and instead add
bind-address = 127.0.0.1

That should fix it!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.