Enable Remote Access To MySQL under Linux

This is done under Ubuntu, but it almost the same for every version of Linux.

 

  1. Edit my.cnf File – you can find it with the locate command, usually under /etc/mysql/my.cnf.
  2. look for bind-address. found it? make sure this is indeed your server address on the network and not, lets say, 127.0.0.1…
  3. restart mysql (this is the exact command as well)
  4. Login to mysql from the terminal mysql -u root -p and then your password.
  5. create your db using create database command, e.g. create database test1;
  6. Now grant access to the user you want (this will create a user if not exists): GRANT ALL ON test1.* TO myuser@'192.168.0.11' IDENTIFIED BY 'PASSWORD';
  7. if you want myuser to be able to connect from any IP, you can just type: GRANT ALL ON test1.* TO myuser IDENTIFIED BY 'PASSWORD';and it will work.

Using mysql and .NET

Creating a connection String:

String connectionString = String.Format("Data Source={0};Database={1};
User ID={2};Password={3}", host, DBCatalog, username, password);

creating a connection:

mainConnection = new MySqlConnection(connectionString);

opening a connection:

mainConnection.Open();

Execute non query (one that does not return any records):

String theQuery ="select....";
DbCommand cmd = Connection.CreateCommand();
cmd.CommandText = theQuery;
cmd.ExecuteNonQuery();

Returning a simple reader:

String theQuery ="select....";
DbCommand cmd = Connection.CreateCommand();
cmd.CommandText = theQuery;   
cmd.ExecuteReader();

Returning a full-pledge record set:

DbDataAdapter dap=new MySqlDataAdapter(theQuery, mainConnection);
DataSet ds = new DataSet();
dap.Fill(ds);