Connect to remote MySQL database

Created at 03 Oct 2019, 10:40
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
BE

BenjaminR

Joined 12.06.2019

Connect to remote MySQL database
03 Oct 2019, 10:40


Hi Everybody,

I would like to use remote MySQL database from cAlgo. I have found this cBot but does not works for me.

It returns with "Cannot connect to server" error. I thought the remote access is not allowed to the MySQL, but the owner said it is allowed. So I have no idea why can't connect. 

Could someone help me or try it? 

Thanks.

using System;
using System.Linq;
using cAlgo.API;
using cAlgo.API.Indicators;
using cAlgo.API.Internals;
using cAlgo.Indicators;
using System.Collections.Generic;

using System.Diagnostics; // run browser
using MySql.Data.MySqlClient; //Add MySql Library and mysql connector(download and install) to references
using MySql.Web;

namespace cAlgo
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.FullAccess)]
    public class Robot1 : Robot
    {
        [Parameter(DefaultValue = 1)]
        public int TimerSeconds { get; set; }

        private MySqlConnection connection;
        private string server;
        private string database;
        private string uid;
        private string password;

        protected override void OnStart()
        {
            Timer.Start(TimerSeconds);
            server = "server IP address";
            database = "database name";
            uid = "username";
            password = "password";
            string connectionString;
            connectionString = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
            connection = new MySqlConnection(connectionString);
        }
        protected override void OnTimer()
        {
            try
            {
                connection.Open();
            } catch (MySqlException ex)
            {
                switch (ex.Number)
                {
                    case 0:
                        Print("Cannot connect to server.  Contact administrator");
                        break;

                    case 1045:
                        Print("Invalid username/password, please try again");
                        break;
                    default:
                        Print("Connected");
                        break;
                }
            }   
            connection.Close();
        }
}}

 


@BenjaminR
Replies

PanagiotisCharalampous
03 Oct 2019, 10:43

Hi Be Rich

Do you change the following values to the actual ones?

            server = "server IP address";
            database = "database name";
            uid = "username";
            password = "password";

Best Regards,

Panagiotis


@PanagiotisCharalampous

BenjaminR
03 Oct 2019, 10:45

Yes. Of course. I do not want to share the real login creditentals by security reason.

I  have tried both server = "http://123.123.123.123"; and  server = "123.123.123.123"; 

where 123.123.123.123 the server IP address 


@BenjaminR