连接拒绝来自 C++ 驱动程序的 mysql 数据库
Connection refused to mysql database from C++ driver
问题
我正在学习 dev.mysql 提供的 Connect/C++ 教程。复制示例并成功编译和链接后,我 运行 进入以下问题:
terminate called after throwing an instance of 'sql::SQLException'
what(): Can't connect to MySQL server on '127.0.0.1' (111)
据我所知,111
代表权限被拒绝。
设置
以下是取自 here 并稍作修改的示例代码:
#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;
}
尝试 tcp://127.0.0.1:3306
得到相同的结果。
这里是 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)
我可以轻松地 运行 mysql
使用代码中的凭据。我禁用了网络,因此它只会监听 localhost
。 运行
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
给了我0,所以我猜是端口0。
C 连接器和命令行界面工作正常。
我的 OS 是 ubuntu 16.04.
I disabled networking, so that it would only listen on localhost.
一起禁用网络 (tcp) 和指定 tcp 仅在您的环回适配器 (127.0.0.1) 上侦听是有区别的。您的 mysql 客户端几乎可以肯定是通过 Unix 套接字而不是 TCP 连接的,这就是它工作正常的原因。通过 运行 mysql --protocol tcp
验证。如果失败,那么你就知道你完全禁用了 TCP 连接,它是通过 Unix Socket 连接的。
connect("tcp://127.0.0.1:0"
如果您确实禁用了与 skip-networking
的网络连接,您需要更改 connect()
以引用您的套接字文件。参见 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).
找到你的套接字:
mysql> SHOW GLOBAL VARIABLES LIKE 'socket'
如果您想保持连接字符串不变,请删除 skip-networking
并添加
bind-address = 127.0.0.1
应该可以解决!
问题
我正在学习 dev.mysql 提供的 Connect/C++ 教程。复制示例并成功编译和链接后,我 运行 进入以下问题:
terminate called after throwing an instance of 'sql::SQLException'
what(): Can't connect to MySQL server on '127.0.0.1' (111)
据我所知,111
代表权限被拒绝。
设置
以下是取自 here 并稍作修改的示例代码:
#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;
}
尝试 tcp://127.0.0.1:3306
得到相同的结果。
这里是 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)
我可以轻松地 运行 mysql
使用代码中的凭据。我禁用了网络,因此它只会监听 localhost
。 运行
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
给了我0,所以我猜是端口0。
C 连接器和命令行界面工作正常。
我的 OS 是 ubuntu 16.04.
I disabled networking, so that it would only listen on localhost.
一起禁用网络 (tcp) 和指定 tcp 仅在您的环回适配器 (127.0.0.1) 上侦听是有区别的。您的 mysql 客户端几乎可以肯定是通过 Unix 套接字而不是 TCP 连接的,这就是它工作正常的原因。通过 运行 mysql --protocol tcp
验证。如果失败,那么你就知道你完全禁用了 TCP 连接,它是通过 Unix Socket 连接的。
connect("tcp://127.0.0.1:0"
如果您确实禁用了与 skip-networking
的网络连接,您需要更改 connect()
以引用您的套接字文件。参见 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).
找到你的套接字:
mysql> SHOW GLOBAL VARIABLES LIKE 'socket'
如果您想保持连接字符串不变,请删除 skip-networking
并添加
bind-address = 127.0.0.1
应该可以解决!