Creating your own backend MySQL server

If you’d like to create your own MySQL database back end server for your android app and are wondering where to begin this overview should help you get started.

Often times it is preferable to control every aspect of your application as opposed to relying on some external API as those may become outdated and unreliable with time. In order to ensure that your application performs harmoniously with your database it is wise to create and maintain the database yourself.

Some advantages include: Personalized security measures, query optimizations specific to your application, and personalized push notifications, cheaper.

Some disadvantages include: Server maintenance can be arduous and time consuming, full responsibility for security leaks, and paying for a domain space.

Firs off you need to have access to a MySQL server. To start you can download and install MySQL on your computer and run the server form local host. To set it up with Ubuntu refer here: https://help.ubuntu.com/lts/serverguide/mysql.html

Second have an http server to store your PHP files to make sql queries from your phone. For this we need apache and php

For ubuntu from terminal:
sudo apt-get update
sudo apt-get install apache2
sudo libapache2-mod-auth-mysql php5-mysql
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

From this point you can add custom php files to /var/www/folder/ so that you can run them from your server

Now that the server is set up you can call the http server to sql queries from your app. To make network calls from inside the app you must use an Asynch task:

public class NewTask extends AsyncTask<Void,Void,Document>{

@Override
protected Document doInBackground(Void… arg0) {

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://serverip/folder/" + "function.php");

try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair(“col1″, Integer.toString(UserID)));
nameValuePairs.add(new BasicNameValuePair(“col2″, Integer.toString(User2)));

httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(response.getEntity().getContent());

return doc;

}

}

At this point you should be ready to implement a service containing your mysql queries.