How to set up MySQL database to connect to from another computer? - c#

I am have tremendous trouble with this.
What I want to do is: I want to create a system (in C#) that will (amongst other things) allow users to send simple messages to each other. So I want to create a MySQL database which will rest on a server. Other computers running the system will then have to connect remotely to this database and read a certain table to see if they have any new messages.
I want the client computer to connect via the internet to the database on the server.
But how do I do this?
How do I create a database that can be connected to through the internet?
How should my connection string look like on the client computer?
What configurations do I need to do on both client and server computers?
Any assistance is deeply appreciated, and if you could suggest how I can go about achieving my objective. I am a quite competent programmer, but HATE these network things!
I am developing in C# using WPF to access the database. So it's a desktop app!

Here's how you do what you're asking to do.
Use the MySQL Connector/NET ADO.NET connector. It is here.
http://dev.mysql.com/downloads/connector/net/5.1.html
Follow the directions for setting it up. It works very well.
Put your MySQL server on a public IP address.
Get your client software assembly to connect to that public IP through the ADO.NET connector.
But, PLEASE!
Ask yourself whether this is the right thing to do. It probably isn't, because MySQL servers (and all table servers) work much more safely and predictably when they are behind firewalls and accept connections from a limited number of client packages.
Consider doing what Mike Christensen suggested. Use ASP.NET and WCF, or whatever stack you like, to build a server-resident interface to your MySQL database. That server-resident interface can then be accessed by the client software you push out to your end users.
That interface can have just the methods you need. WCF or any other stack for building server components can do this easily and robustly. For example.
Client 1: This is moe. Here's a message for curly: "Look at the grouse, look at the grouse".
Server: OK
Client 2: This is curly: any new messages?
Server: moe says "Look at the grouse, look at the grouse".
Client 1: This is moe. When did curly last collect my messages?
Server: ten minutes ago.
Client 2: This curly. any new messages?
Server: NO
This is far safer and more scalable than just making the MySQL interface available on the internet, and it will perform much better.
By the way, it looks like you want an instant messaging protocol. You may want to look into using the open-source system at http://jabber.org . This stuff can be tricky to get right if you build it all yourself.

Related

Proper way for a client desktop application to query SQL database

Say I have an application that wants to return information from an online database, it's a public application which anyone has free access to. What is a safe approach for these clients to communicate with the database?
I am interested in how this is done in professional environments. When I tried to do research all I could find were examples of clients connecting directly to the database themselves, leaving their connection strings in the code, these examples I'm sure were for server sided/private applications. But in my scenario I assume there has to be a proxy in between.
Do I set up a web server with PHP scripts that returns results or do I program a server that listens for TCP/IP connections? I feel there is a better way but can't lay my finger on it.
What my question boils down to is essentially..
What technique is used for this 'proxy' and client to communicate? What does the setup look like? Even a diagram would be great. Assume I am working in C#.

Making two programs talk with each other

My group in the university are working on a project, which includes us making a C# program. We have a vision of making a server side console program that is constantly calculating data and then make a client side program that can fetch the data from our server side program. The client side program will then be able to display the data and the user is able to navigate forth and back around the data. Both of these programs will be run on the same computer solely for exercise purpose.
I am wondering how I can get data from the server side program and be able to display it on the client side program.
If both client and server are always on the same machine, there is no need at all to use WCF or Sockets. You can use named pipes for the interprocess communication.
If you're trying to do a more serious client/server application, I recommend you to try Redis which provides a lot of remote features like pub/sub and caching, so you'll avoid reinventing the wheel.
As mentioned in the comments that sounds like you would like to create a "server" like application which you could do with WCF.
http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication
There are also some workarounds if you wish to do so, using a Database or use a folder containing Text Files,that would include a folder watcher, which you than read, but they are not that elegant.
You can get data to the client program by configuring it by using web.api self hosting. Here is a great link. The server app can be configured to use the same thing. They can both listen and answer one another.
http://www.c-sharpcorner.com/UploadFile/b1df45/Asp-Net-web-api-self-hosting/
Why don't you give ASP.NET Web API a chance?
You'll be creating a REST API that not only a C# program can talk to (any language/implementation - even another server) can talk to your server as long as the end point is correct (in your case would be your IP address and port number) & of course, you don't need both programs to be on the same computer as well :)

How to load balance a chat server?

i have a dedicated server that run a chat server made by me. But the server is almost overloaded and i want add another server to balance the users.
The servers are both a Linux Ubuntu 12.04 server and the chat program made by me was developed with c# and .NET with Mono.
The problem is that if i open another chat server on the other computer how can i make that the users can communicate between the two servers?
I will try to explain better:
User A is connected to Server A.
User B is connected to Server B.
If the user A send a message to user B that is connected to server B how i can redirect the message to the other server? What is the best solution?
Thank you in advance.
Firstly, whenever you have a performance problem, you should first try to measure where exactly it lies. Maybe you can improve the performance of your system dramatically in some way by looking into why and where it is slow. This way you might not need to scale your system at all. You could also try to scale up instead of out, which is generally easier.
If that fails I would suggest to switch to an existing chat system like XMPP or IRC. There are great XMPP libraries for C# so this would be the best solution I think.
Lastly, when you really want to scale out your own chat system in the way you described, I would suggest to just send messages as JSON over TCP. You can use fancy RPC stuff or REST APIs but for this use case I think you should keep it as simple and lightweight as possible.
It depends which part exactly is missing. There a many parts to a load balanced system.
My guess is that you are missing the backing part, which might not be so obvious.
IPAddress -> Load Balancer <= x-Servers => Backplane
This is simliar to what we used in our case for the back part:
http://www.tugberkugurlu.com/archive/scaling-out-signalr-with-a-redis-backplane-and-testing-it-with-iis-express
I hope it points you in the right direction.

Best practice: How to detect if a SQL Server is available and failover if not

I am looking for a way to detect if a SQL Server fails to respond (timeout/exceptions etc) so that if our C#/.net program trying to access the server asks a server that is down for maintenance, it will jump and try another automatically.
I would like a solution where we do the SQL connection and THEN get the timeout. We could build a WCF service and ask that one, but that is a bit of an overkill in my opinion.
The sync between multiple server locations is not the issue here.
Our development platform at the moment is SQL2008express as its plenty at the moment, but later on we might switch to real SQL2008 servers (or whatever is latest when the time comes).
Clients will connect to "first known" in a "last known dynamic list" asking a "rootserver" or hardcoded configs for first lookup.
When clients loses connections, they will automatically have to try to reconnect to other nodes in the clusters and use whatever returns a reply first. The nodes will individualle connect and share data through other services which we also distribute in the cloud.
We know that mirroring and clustering might be available through large licenses, but our setup demands a more dynamically "linking" and we believe this approach suits our needs better.
So... to be specific:
we need to detect when a SQL-server goes offline, when its not available anymore. Eg. in the middle of a transaction or when we try to connect.
Is the best approach to do a "try-catch" exception handling or is there better tricks when looking over WAN's and using C#/.net =
EDIT
I've received a lot of good ideas to use failover servers, but I would like a more programatical approach, so whats the best way to query the server if its available?
Situation:
4 different SQL servers running on seperate WAN/IP's, each will maintain a list of "where the others are" (peer-to-peer). They will automatically be moving data from each other (much like a RAID-setup where data is spread out on multiple drives)
A client retries the list from an entry-point-server and asks the first available.
If the client asks a server that is "down for maintance" or the data has moved to one of the other servers, it must automatically ask the next in the list.
What we are looking for..
is the best way from within C#/.net to detect that the server is currently unavailble.
We could have a service we connect to and when we loose this, the server is off
We could make a "dbConnectionSqlServer3.open()" and wait for the time out.
We could invest in "real cluster servers", and pay a lot + bind ourselfs to 1 SQL-server type (The SQL platform might change in future, so this is not a real option)
So whats your vote here: 1 or 2?
or can you come up with a number 4 that beats the h**k out of ideas? :o)
I would probably take a different approach.
I'd designate a server (with a failover partner) to be the holder of the server list. It should monitor all of the other servers for availability.
When the client initially wants to connect, it should contact the list server and ask it what to use. From that point forward the client should stick with it, unless a failure is detected. At which point it should contact the list server to get a new one.
The list server would be able to implement any type of load balancing you wanted just by telling the clients which one to connect to. Further, deploying new servers would be easy as the only "list" would be maintained by this primary server.
This completely ignores any sort of server synchronization issues you might have.
One major benefit is that by having a central server doing the monitoring your clients won't have to fall through 3, 5, or 10 servers just to find one that's up. which would increase responsiveness.
UPDATE
#BerggreenDK: The best, and probably only assured way, to detect if a server has failed is to connect to it and run a simple query. All other mechanisms such as pinging, a simple check if the port is open, etc. might give a false positive. Even having a process running on that server itself may give a false reading if the server itself is up but SQL Server is down (e.g: the database is offline).
Ultimately it sounds like your client will have to connect to one of the gateway servers and get a short list of sql servers to attempt to connect to. On failure, it will have to rotate through that short list. If all are down it will have to go back to the gateway and ask for a new list to start the process over.
In the ConnectionString you can specify a failover partner like
Data Source=<MySQLServer>;Failover Partner=<MyAlternateSQLServer>;Initial Catalog=<MyDB>;Integrated Security=True
On http://www.connectionstrings.com there is a section talking of DataBase mirroring.

How are server side applications created, how is client - server communication done?

I would like to have a client-server application written in .NET which would do following:
server is running Linux
on the server there is SQL database (mySQL) containing document URLs
What we want:
- server side would regularly crawl all URLs and create a full text index for them
- client side would be able to perform a query into this index using GUI
The client application is written in .NET using C#. Besides of searching in documents it will be able to do a lot of other things which are not described here and which are done client-side very well.
We would like to use C# for the server side as well, but we have no experience in this area. How are things like this usually done?
Clarifying question now based on some answers:
The thing which is most unclear to me is how client-server communication is usually handled. Is client and server usually using sockets, caring about details like IP addresses, ports or NAT traversal? Or are there some common frameworks and patters, which would make this transparent, and make client-server messaging or procedure calling easy? Any examples or good starting points for this? Are there some common techniques how to handle the fact a single server is required to server multiple clients at the same time?
To use c# on Linux you will need to use Mono. This is an open source implementation of the CLR specification.
Next you need to decide on how to communicate between server and client, from the lowest level of just opening a TCP/IP socket and sending bits up and down, to .Net remoting, to WCF, to exposing webservices on the server. I do not know how compleat WCF implementation is on mono, also I think you may have issue with binary remoting between mono and MS .Net .
I would suggest RPC style WebServices offer a very good solution. WebServices also have the advantage of alowing clients from other platforms to connect easily.
EDIT
In response to the clarification of the question.
I would suggest using mono/ASP.NET/WebServices on the server, if you wish to use c# on both server and client.
One assumption I have made is that you can do a client pull model, where every message is initiated by the client. Using another approach could allow the server to push events to the client. Given the client has the ability to pole the server regularly I don't consider this much of a draw back but it may be depending on the type of application you are developing.
Mono allow execution of c# (compiled to IL) on a Linux box. Mono ASP.NET allows you to use the standard ASP.NET and integrate into Apache see http://www.mono-project.com/ASP.NET and finally WebServices allow you to communicate robustly in a strongly typed manner between you client and your server.
Using this approach negates most of the issues raised in your clarification and makes them someone else's problem.
Sockets/SSL - is taken care of by standard .Net runtime on the client and Apache on the server.
IPAddress/ports/NAT traversal - Is all taken care of. DNS look up will get the servers IP. Open socket will allow the server to respond through any firewall and NAT setup.
Multiple Clients - Apache is built to handle multiple clients processing at the same time as is ASP.NET, so you should not encounter any problems there.
As many have already mentioned there are a number of thing that you have mentioned which are going to cause you pain. I'm not going to go into those, instead I will answer your original question about communication.
The current popular choice in this kind of communication is web services. These allow you to make remote calls using the HTTP protocol, and encoding the requests and responses in XML. While this method has its critics I have found it incredibly simple to get up and running, and works fine for nearly all applications.
The .NET framework has built in support for web services which can definitely be called by your client. A brief look at the mono website indicates that it has support for web services also, so writing your server in C# and running it under mono should be fine. Googling for "C# Web Service Tutorial" shows many sites which have information about how to get started, here is a random pick from those results:
http://www.codeguru.com/Csharp/Csharp/cs_webservices/tutorials/article.php/c5477
have a look at Grasshopper:
"With Grasshopper, you can use your favorite development environment from Microsoft® to deploy applications on Java-enabled platforms such as Linux"
Or see here
The ideea is to convert your app to Java and then run it on Tomcat or JBoss.
Another approach: use the Mod_AspDotNet module for Apache, as described here.
This Basic Client/Server Chat Application in C# looks like a kind of example which might be a starting point for me. Relevant .NET classes are TcpClient and TcpListener

Categories