SIGN UP MEMBER LOGIN:    
ARTICLE

Connecting to Remote MySQL (Linux Server) using Visual C#

Posted by Camilo III Lozano Articles | Visual C# April 27, 2010
In this article we will see how to connect to a remote database server using Visual C# as client window application.
Reader Level:
Download Files:
 

I have a current project that connects to a remote database server using Visual C# as client window application. So before I started my project, I tested it if it is possible to connect and the result, it does connect. For those does who failed to solve this problem, here's my tutorial/guide.

Test Project Requirements:

  • VMware Workstation 5.5
  • CentOS 4.4
  • Visual C# Express Edition
  • MySQL Data Connector .Net v1.0.7

Scenario:

The client's OS is MS Windows XP and the server is CentOS Linux. The client will connect to the Linux server to query and process some transaction. But in the sample code, it demonstrate only how to connect remotely in the server. My server's IP address is 192.168.10.117 and a MySQL port of 3306 (default port).

Screenshots:

97__287x226_mysqlRemote1.jpg

Step 1:

Setup the Linux server, be sure you already installed MySQL server. In our case, we use VMware and a CentOS 4.4 is installed. For installation help, google it! The first thing to do is configure your IP tables by using iptables command. see illustration below;

96__400xfloat=center_iptables2.jpg

If your configuration does not allow foreign connection to your MySQL, then change that and allow it. In real scenario if your server is public, I recommend that you must have a new server which can only be access through local network. Or if cannot afford, just setup well the IP tables that only local connections are allowed. Anyway, here's my new IP tables setup in my server;

95__400xfloat=center_iptables1.jpg

In MySQL CLI, create a user that can access everywhere. To do this, see sample below;

mysql> GRANT ALL PRIVILEGES ON *.* to 'beasaw'@'%' IDENTIFIED BY 'qwerty';

Explained: the command shown above is to allow user, beasaw, to access anywhere using a password qwerty.

That's it... server setup completed.

Step 2:

Before you create a new project in VCS, test first the connection using the command;

C:\> mysql\bin\mysql --host=192.168.10.117 --port=3306 --user=beasaw --password=qwerty

See image below;

93__400xfloat=center_cmd_mysql.jpg

Step 3:

  1. Open your Visual C# and create a new project, name it to remoteMySQL.
  2. In Form1.cs, layout just like the screenshot above.
  3. Add a reference, MySql.Data.dll (assumed that you already installed the MySQL data connector .Net v1.0.7)
  4. Add mysql to the project: using MySql.Data.MySqlClient;
  5. Begin your codes, see sample code (download link below)...

98__287x226_mysqlRemote2.jpg

That's it... You can create as many applications you want with this method. Like Ticketing System, connecting 10 terminals to the servers simultaneously and etc. And of course, connection varies also to your MySQL server setup.

Login to add your contents and source code to this article
share this article :
post comment
 

i think there's no way for that issue. you really need to disable your firewall or add exception to mysql to accept outgoing and incoming requests in your computer.

Posted by Camilo III Lozano Jun 21, 2011

thanks for your offer but i cant disable to firewall there is a business place so if i disable it i'll have to look a new business :) dou you have another offer for me ? (sorry my bad english i coudnt learn good )

Posted by Ahmet COBAN Jun 20, 2011

hello Ahmet! have you check you firewall in windows server 2003? Go to Control Panel -> Windows Firewall then try disable the firewall and try add mysql in EXCEPTIONS tab to except filtering mysql broadcasting to your network. :)

Posted by Camilo III Lozano Jun 17, 2011

hi im ahmet from turkey i've ms server 2003. mysql with phpmyadmin on it i tried day by day connect with c# to my remote dbase but i cant. allowed ip or port etc. i tried eveything please help for it

Posted by Ahmet COBAN Jun 17, 2011

Thank you very much this article had help me a lot

Posted by Petrociano Vallecera Feb 08, 2011
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor