1

Trying to connect to a database with a c++ program. I am using the mysqlcppconnector with the connection strings tcp://localhost:3306 and tcp://127.0.0.1:3306. The program works on my local machine, but when I try to run it on my remote ubuntu server after ssh'ing in, it complains:

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

I'm just trying to connect on localhost, at the default port 3306. I can also connect manually by typing in mysql -u root -p and just logging in that way.

After logging in to my database with mysql -u root -p, I can type show variables; and indeed I see skip-networking set to "OFF".

MySQL is listening on the network:

netstat -a | grep mysql
tcp        0      0 localhost:mysql         0.0.0.0:*               LISTEN
unix  2      [ ACC ]     STREAM     LISTENING     20025    /var/run/mysqld/mysqld.sock

Is there some configuration that I forgot to change? What does the "(2)" mean?

roaima
  • 107,089
  • 14
  • 139
  • 261
Taylor
  • 123
  • 6
  • @Christopher ```root@ubuntu-s-1vcpu-1gb-nyc1-01:~# netstat -a | grep mysql tcp 0 0 localhost:mysql 0.0.0.0:* LISTEN unix 2 [ ACC ] STREAM LISTENING 20025 /var/run/mysqld/mysqld.sock ``` – Taylor Oct 24 '19 at 15:39
  • Check the firewall setting too ;) – mariaczi Oct 24 '19 at 15:59
  • @mariaczi on the server? how would I bet able to ssh in then? – Taylor Oct 24 '19 at 15:59
  • Is [How to connect mySQL database using C++](https://stackoverflow.com/q/16424828/2344631) any help? – roaima Oct 24 '19 at 21:24
  • @roaima my code resembles that. But I'm reasonably sure it already works--I have successfully ran the program on my local machine. It connects to a database and writes to it. This error only pops up when I try to run it on my server. I was thinking it's either some default configuration that digital ocean has, or some path that was hardcoded that changes...I'm not sure what it could even be. – Taylor Oct 24 '19 at 21:32
  • It looks like it's trying to resolve the non-existent hostname `tcp`. are you sure that's a valid URL protocol type or connection string for your mysql library? try it without the `tcp://`, i.e. as just `127.0.0.1` or `127.0.0.1:3306`. BTW, mysql has **always** been weird about localhost connections - it takes it upon itself to silently convert tcp connections to localhost into unix domain socket connections....but I don't think that's what's happening here. – cas Oct 25 '19 at 03:10

1 Answers1

1

Thanks everyone for the suggestions. I fixed it by reinstalling mysqlcppconnector (not by changing the c++ code). I started suspecting it was something build-related, so I asked about this here.

A few notes on the differences between the two installations: instead of using 1.1.12, I reinstalled with the most recent edition; 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?" at the download page doesn't redirect you to version 1.1.12 anymore--it directs you to 1.1.13.

After this the program ran. Well, it did give me an error, but a different one, and the fix for this issue was pretty easy. For what it's worth, it was

Access Denied for User 'root'@'localhost'

I checked the permissions by typing 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

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

Hope this helps someone else--this was a strange one. Thanks everyone for the help, too.

Taylor
  • 123
  • 6