5 - Insertar datos y probar base de datos

How to install MySQL (MariaDB) on Android with Termux

Install MySQL on Android

Android mobile devices are not designed to be servers (although some of them are powerful), however it is always interesting to see how technology advances and day by day we can do more things with our phones.

Today we will see how to install the MySQL or MariaDB server on Android, thanks to the Termux application. For that it is enough to execute a few simple commands and voila, we will have a MySQL database server in our Android; fully functional and ready to execute queries.

If we want to put it for production, we can do it as long as we configure security. In short, we will not see more advanced topics; we will only see how to install mysql on Android.

Here we will use MariaDB and MySQL as synonyms; that is, if you see that I say MariaDB do not get confused, I mean MySQL; and vice versa.

I know that there are really more differences especially with the licenses, but that is another topic and you can search it in Google if you wish.

Requirements

If you can install Termux everything is fine, you do not need your device to be rooted. I think that this APK works from Android 6.

See how it is installed, configured and downloaded here. Once you have it, we are going to start the tutorial.

Update packages to install mysql

In your Termux application execute:

apt update

And then:

apt upgrade

In both cases, if the app asks, choose yes, that is, put the letter y. This step is very important because it will update the libraries that are necessary to install mysql on our mobile device.

Install MariaDB on Android using Termux

OK, once you have your system updated we will install the MariaDB package. To do this, execute:

pkg install mariadb

If it asks you, accept everything by writing the letter y. It may take a while, it depends on your device, so be patient.

1 - Instalar el paquete de mariadb en Android usando termux
1 – Install mysql package on Android with termux

We have installed it, here is a little note:

Required in some cases: create mysql directory

At least in my case the installation failed because there was no directory called my.cnf.d (very similar to the MySQL configuration file but that was not the case).

Then that directory has to be created. If you navigate to the location that I will tell you and it already exists, do nothing and return to $HOME.

If not, then you create it. The folder that we are going to create must be located in /data/data/com.termux.files/usr/etc (if in your case it changes, I only tell you to browse a directory above $HOME and you will see a usr folder).

Let’s navigate there with:

cd /data/data/com.termux.files/usr/etc

Then list the content:

ls

And if the folder named my.cnf.d does not appear, you have to create it with

mkdir my.cnf.d

Here is an image about the creation of the dir:

2 - Crear directorio si no existe
2 – Make dir if it does not exist

As we can see, in step 1 the content is ready and the directory does not exist, so with mkdir I created the folder.

Configure MariaDB on Android

Once we have created the directory in case it does not exist, we execute this command:

mysql_install_db

We wait for it to be completed, with that we install and create the MariaDB directories (I think that the previous step, where we created the directory, is done automatically by this installer, but we already did it).

After that, what we have to do is start the MySQL daemon (this should also be done if we restart the phone).

To do this, execute:

mysqld_safe -u root &

What we do is run mysqld_safe with the root user; the ampersand & is to run it in the background.

Run it and press Enter. This is how it looks in my case:

3 - Iniciar demonio de mysql en segundo plano
3 – Start mysql daemon on background

We are almost done.

Test MariaDB (MySQL) on Android with Termux

Now that we have started the daemon, it is time to connect from the client. To do this, execute:

mysql -u root

That must have shown you something like this:

4 - Iniciar cliente de MySQL en Android con Termux
4 – Start mysql client on Android with Termux

If not, make sure you started the demon.

Create database with table and insert data

To verify that this works we will insert some data in our server:

5 - Insertar datos y probar base de datos
5 – Insert some data and test mysql database

Stop MySQL/MariaDB process

If you want to stop the process, find the ID from whichever process that has the word “mysql” using ps with grep, and then kill them with kill -9 [ID], the -9 is to send a KILL SIGNAL.

To find the processes IDs execute:

ps aux | grep mysql

We are passing the ps output through a pipe to grep to search for those with mysql inside.

Listar procesos de MySQL MariaDB en Android
List processes of MySQL/MariaDB on Android

If you take a  look, they are two processes with 15406 and 15488 ids. The third is from grep but we omit it. Remember that it will change in your case because the process id isn’t always the same.

When you have the IDs kill them:

kill -9 15406

kill -9 15488

If we list the processes again they don’t appear anymore:

Detener servidor de MySQL
Detener servidor de MySQL

To start the server again, execute the previous steps above.

Optional but recommended: secure MySQL

The installation of MySQL is already working correctly, and if we are only going to play with it then we should not care about security.

But instead, if you want to ensure it by habit or by good practices you can do it. To do this, execute: mysql_secure_installation

Which will give you a wizard that will help you to ensure your installation of MySQL: enter a password, remove test privileges, among others.

Notes and conclusions

You already have a MySQL server installed on your Android. It’s time to try some basic commands from this database engine.

By the way, if you reboot your phone the server will stop, just rerun the daemon as we saw up there.

Click here if you want to learn more about Termux.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

9 thoughts on “How to install MySQL (MariaDB) on Android with Termux”

  1. Hi. Thank you for this. I need to import an .sql file, how do I do that? I Windows I just use source and the path to the sql file. In termux, I can’t seem to figure it out. Thank you in advance.

  2. This is very good, thanks for the post. However, just a quick pointer on a typo, the directory you referenced as under “cd /data/data/com.termux.files/usr/etc” is actually “cd /data/data/com.termux/files/usr/etc”, you used a “.” instead of “/” between “termux” and “files”.

    1. Hi, you are welcome.
      Well, in my case the dir was as my post says, but if you got another dir maybe it changed with the new version or something so your comment may help another user with that dir.
      Anyway, thank you

Leave a Comment