Report this

What is the reason for this report?

Hello!

I have used many databases in my my Digital Ocean droplets before. I typically use PHP but in this occasion I need to use c++. I downloaded the dev.mysql connector for c++, nothing else. Is not my plan to run this locally in my machine.

After installing and configuring it, I was finally able to compile some code. The problem is that I can’t establish a connection from the compiler. I don’t suspect any type of connectivity issue, since I can ssh to my droplet all day long.

In a nutshell, this is my code:


#include <iostream>
#include <mysqlx/xdevapi.h>

int main(){


try{

   mysqlx::SessionSettings option_list(
            SessionOption::USER, "root",
            SessionOption::PWD, "password",
            SessionOption::HOST, "123.45.6.78",
            SessionOption::PORT, 3306,
            SessionOption::DB, "test_database");

   mysqlx::session sess(option_list);

   std::cout << "yeah this works" << std::endl;


}
catch(exception e){

std::cout << e.what() << std::endl;
exit(0);

}
}

I realize the code above may seem offensively bad, but please help me.

For example, are my options right? I have played with several combinations. I even used SSL enabled or disabled, whatever that is.

How does it log in into the droplet first? I ask because if I was using myheidysql there are two logins, one for the droplet and one for mysql inside the droplet. My code doesn’t do that and in PHP, well the files were already inside the droplet. Both mysql and my droplet have identical users and passwords for simplicity.

Any pointers for me? I am using XDev c++ Dynamic option.

Thank you so much. I would also read any tutorial, but they are all outdated or assumed I am more advanced, which is not the case.

Diego.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

here is an update on an somewhat older topic

It looks like you’re on the right track with your C++ MySQL connector! You might need to adjust your approach slightly, though. Here are a few things to consider:

  1. Exception Handling: In your catch statement, you’re catching an exception by value, which can lead to object slicing and loss of data. Instead, catch by reference:
catch(const std::exception& e){
    std::cout << e.what() << std::endl;
    return 1;
}
  1. Credentials: Verify that your MySQL credentials (username, password) and connection details (host, port) are correct. If your MySQL server is not on the same machine where you’re running this program, ensure that it’s configured to accept connections from your client’s IP address. In your case, since you are using the IP address for the host, make sure that MySQL is not bound to localhost (127.0.0.1) but to 0.0.0.0 or your specific IP address.

  2. Firewall: Ensure there are no firewall rules preventing you from connecting to the MySQL port (3306 by default) on your server.

  3. Test Connection: Try to connect to your MySQL database from your command line, using mysql -h <host> -u <username> -p<password> -D <database>. This can help you determine if the issue is with your program or with your server setup.

  4. SSH Tunnel: You’ve mentioned that you log into your droplet via SSH. If your MySQL server only accepts local connections (which is common for security reasons), you’d have to create an SSH tunnel before connecting with your C++ code. You’d then connect to ‘localhost’ in your C++ code, but that connection would actually be tunneled through SSH to your remote MySQL server.

If your database is bound to localhost (for security reasons), you can’t access it directly from an external IP address. Instead, you would access it through an SSH tunnel. This is likely the reason you’re able to access it through PHP (the PHP code is running on the droplet and can access the database locally), but not from your C++ code running on a different machine.

Creating an SSH tunnel involves using the ssh command with the -L option, which forwards a local port to a port on the remote machine. This would be done from the machine where your C++ code is running.

The command might look something like this:

  1. ssh -L 3306:localhost:3306 username@droplet_ip

This command opens an SSH connection to your droplet, and also forwards any connections to port 3306 on the local machine to port 3306 on the droplet. You can then modify your C++ code to connect to ‘localhost’ instead of the IP of your droplet.

However, this requires your C++ program to have the ability to create and manage an SSH connection, which is likely beyond its current capabilities. Instead, you might consider running your C++ code on the droplet, or exploring other ways to allow external connections to your MySQL server (such as modifying its bind address or firewall rules). Always ensure that you’re following security best practices when modifying server configurations.

Remember to replace "localhost" with the IP of your MySQL server in your C++ code, and username@droplet_ip with your SSH username and the IP address of your droplet in the SSH command.

Please note that the approach of SSH tunnel is not the most suitable for a production environment, but it’s OK for testing or development. For production environments, consider a more secure approach such as a VPN, or adjusting your MySQL server to accept secure external connections.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.