Does digital ocean prohibit tcp connections to localhost on a mysql server?

October 24, 2019 97 views
MySQL

I am 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 digital ocean 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 have tried many ports, including the default of 3306. I can also connect to the database outside of c++, manually on the command line, by typing in mysql -u root -p. After that, I can type show variables; and indeed I see skip-networking set to “OFF”, which is what it should be. Finally, MySQL appears to be 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?

1 Answer

DigitalOcean does not prohibit tcp connections to localhost.

The netstat output you provided does look like it is bound to localhost and listening on the “mysql” port - which is the default 3306. To see a little clearer info, you can run netstat -an which will use IP addresses and port numbers instead of domains / services.

Just to confirm you can access mysql via CLI to localhost, let’s try running this command:

mysql -h localhost -u root -p

I usually use the IP address, 127.0.0.1, but it’s easy to check both for whatever sservice it is you’re testing:

mysql -h 127.0.0.1 -u root -p

If both of those work, you might try comparing the versions of mysqlcppconnector that you are using locally and on your Droplet. I found some hints here:

https://unix.stackexchange.com/a/548725

That answer links to another question with more info, too.

Hope this helps! Let us know if you need additional help or advice.

  • Thanks for the help. Turns out it was an issue with the build. I reinstalled a library (third time’s the charm), and everything started working as expected.

Have another answer? Share your knowledge.