简体   繁体   English

为什么 mysql 拒绝本地连接

[英]why is mysql refusing local connections

I'm trying to use the (slightly older version of) MySQL C++ connector (see here) to open up a connection to a database and write stuff to it.我正在尝试使用(稍旧版本的)MySQL C++ 连接器(参见此处)打开与数据库的连接并向其写入内容。 My code works on my local Ubuntu 18.04 laptop.我的代码适用于我的本地 Ubuntu 18.04 笔记本电脑。 However, it doesn't work on my remote server (same OS).但是,它不适用于我的远程服务器(相同的操作系统)。

I have tried connecting to tcp://localhost:3306 , tcp://127.0.0.1:3306 , as well as several other ports.我尝试连接到tcp://localhost:3306tcp://127.0.0.1:3306以及其他几个端口。 The error I receive is我收到的错误是

terminate called after throwing an instance of 'sql::SQLException'
  what():  Unknown MySQL server host 'tcp' (2)

The server and laptop share the same OS and the same g++ compiler, almost all of the same files (except for one or two things I changed to reflect different paths), and have databases set up in the same way.服务器和笔记本电脑共享相同的操作系统和相同的g++编译器,几乎所有相同的文件(除了我更改的一两个东西以反映不同的路径),并以相同的方式设置数据库。 I was thinking that it could've been some sort of config file issue . 我在想这可能是某种配置文件问题

Is it possible I could've botched the mysqlcppconn installation?我有可能搞砸了 mysqlcppconn 的安装吗? I installed it in a pretty sketchy way--I manually moved headers into /usr/include/ and manually moved shared libraries into /usr/lib/x86_64-linux-gnu/ .我以非常粗略的方式安装了它——我手动将标头移动到/usr/include/并手动将共享库移动到/usr/lib/x86_64-linux-gnu/ When you see the files in the lib/ folder当您看到lib/文件夹中的文件时

libcrypto.so        libmysqlcppconn-static.a  libmysqlcppconn.so.7         libssl.so
libcrypto.so.1.0.0  libmysqlcppconn.so        libmysqlcppconn.so.7.1.1.12  libssl.so.1.0.0

you'll notice that there's some libcrypto and libssl stuff in there--I didn't move those in.你会注意到里面有一些libcryptolibssl的东西——我没有把它们移进去。

Also, when I tried to change ip address strings to hardcoded string literals, I remember seeing a std::bad_alloc error, and google showed me some threads that were suggesting it was something to do with varying compiler versions...此外,当我尝试将 ip 地址字符串更改为硬编码字符串文字时,我记得看到std::bad_alloc错误,谷歌向我展示了一些线程,这些线程暗示它与不同的编译器版本有关......

Anybody have an idea what's going on here?有人知道这里发生了什么吗? Here's the relevant piece of c++ code, but like I said, it works on my laptop so I'm pretty sure this isn't the problem:这是 c++ 代码的相关部分,但就像我说的,它适用于我的笔记本电脑,所以我很确定这不是问题:

MarketHistoryWriter::MarketHistoryWriter(
        const MySqlConfig& msql_config,
        unsigned num_symbols,
        const std::string& table_name,
        bool printing)
    : m_msql_config(msql_config), m_table_name(table_name), m_num_sym(num_symbols), m_printing(printing)
{

    // configure driver and connection
    m_driver = get_driver_instance();
    std::string conn_str = "tcp://"
                         + m_msql_config.host + ":"
                         + std::to_string(m_msql_config.port);
    m_conn = m_driver->connect(conn_str,
                               m_msql_config.credentials.username,
                               m_msql_config.credentials.password);
    m_conn->setSchema(m_msql_config.schema);
}

Also, if it helps, here's the traceback produced by gdb :另外,如果有帮助,这里是gdb产生的回溯:

(gdb) bt
#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:51
#1  0x00007ffff6834801 in __GI_abort () at abort.c:79
#2  0x00007ffff70a8957 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff70aeab6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff70aeaf1 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff70aed24 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff7638a4a in sql::mysql::MySQL_Connection::init (this=this@entry=0x555555818a00, 
    properties=std::map with 3 elements = {...})
    at /export/home/pb2/build/sb_0-32258110-1547655664.03/mysql-connector-c++-1.1.12/driver/mysql_connection.cpp:900
#7  0x00007ffff763b5ea in sql::mysql::MySQL_Connection::MySQL_Connection (this=0x555555818a00, 
    _driver=<optimized out>, _proxy=..., hostName=..., userName=..., password=...)
    at /export/home/pb2/build/sb_0-32258110-1547655664.03/mysql-connector-c++-1.1.12/driver/mysql_connection.cpp:146
#8  0x00007ffff763fc4f in sql::mysql::MySQL_Driver::connect (this=0x5555557fa5c0, hostName=..., 
    userName=..., password=...)
    at /export/home/pb2/build/sb_0-32258110-1547655664.03/mysql-connector-c++-1.1.12/driver/mysql_driver.cpp:132
#9  0x00005555555b8c6e in MarketHistoryWriter::MarketHistoryWriter(MySqlConfig const&, unsigned int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) ()
#10 0x000055555559fccd in TestCppClient::TestCppClient() ()
#11 0x000055555559c451 in main ()

Edit: a smaller, more reproducible example.编辑:一个更小,更可重复的例子。

I run this short program below and I get this error我在下面运行这个简短的程序,我得到了这个错误

root@ubuntu-s-1vcpu-1gb-nyc1-01:~/test_mysql_conn# ./main 
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc
Aborted (core dumped)

Here's the program这是程序

#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>

using namespace std;

int main(void)
{

try {
  sql::Driver *driver;
  sql::Connection *con;
  sql::Statement *stmt;

  driver = get_driver_instance();
  con = driver->connect("tcp://127.0.0.1:3306", "root", "secretpassword");
  //con->setSchema("ib");

  delete con;

} catch (sql::SQLException &e) {
  cout << "# ERR: SQLException in " << __FILE__;
  cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
  cout << "# ERR: " << e.what();
  cout << " (MySQL error code: " << e.getErrorCode();
  cout << ", SQLState: " << e.getSQLState() << " )" << endl;
}

cout << endl;

return EXIT_SUCCESS;
}

Here's the makefile:这是 makefile:

CXXFLAGS=-pthread -Wall -Wno-switch -std=c++11
LDFLAGS=-lmysqlcppconn
INCLUDES=-I/usr/include/cppconn
TARGET=main

$(TARGET):
        $(CXX) $(CXXFLAGS) $(INCLUDES) ./*.cpp -o$(TARGET) $(LDFLAGS)

clean:
        rm -f $(TARGET) *.o

I was correct that the code was "fine."我是正确的,代码“很好”。 I fixed this by reinstalling mysqlcppconnector.我通过重新安装 mysqlcppconnector 解决了这个问题。 Instead of using 1.1.12, I reinstalled with the most recent edition.我没有使用 1.1.12,而是重新安装了最新版本。 Also, I didn't install by manually copying files around, I installed with另外,我不是通过手动复制文件来安装的,我是用

sudo apt-get install libmysqlcppconn-dev

I don't know if this is a coincidence, but I also notice that clicking on "Looking for previous GA versions?"我不知道这是不是巧合,但我也注意到点击“寻找以前的 GA 版本?” at the download page doesn't redirect you to version 1.1.12 anymore--it directs you to 1.1.13.下载页面上的不再将您重定向到 1.1.12 版本,而是将您定向到 1.1.13。

After this the program ran.在此之后程序运行。 This issue is unrelated, but it gave me the这个问题无关紧要,但它给了我

Access Denied for User 'root'@'localhost'

error.错误。 I checked the permissions by typing SELECT user,authentication_string,plugin,host FROM mysql.user;我通过输入SELECT user,authentication_string,plugin,host FROM mysql.user; , saw that localhost didn't have mysql_native_password permissions (only auth_socket), so I changed that by doing ,看到 localhost 没有 mysql_native_password 权限(只有 auth_socket),所以我通过这样做来改变它

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'secret_password_here';

Hope this helps someone else--this was a strange one.希望这对其他人有所帮助-这是一个奇怪的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM