Introduction
MySQL is a very popular open source database. In this project, we will use it to store the measured temperature values, which can then be retrieved by a web page.
Installing MySQL
Before we start, if you have not updated opkg in a while, you should probably do that first:
opkg update
Now on to the task at hand. Installing MySQL. There is already an opkg package, so we’ll install that:
opkg install mysql5
This will download and install MySQL. Unfortunately, if we try and fire up MySQL:
/etc/init.d/mysqld start
We will see an error:
/etc/init.d/mysqld: line 3: /etc/default/rcS: No such file or directory
The fix to this issue is fairly easy. We need to comment out that line 3 in the script:
vi /etc/init.d/mysqld
Now when we try and fire up MySQL:
/etc/init.d/mysqld start
We are successful and we can log into MySQL:
Warning
At this point it might seem like time for celebration. We have MySQL installed and are able to run it and log in. Unfortunately, if we restart then when we log in again we will get an error stating:
Cannot make/remove an entry for the specified session.
This is really annoying since it won’t let you log in and fix the issue. I had to resort to reimaging my SD card and trying again. (The steps are here, in case you have to do it as well.)
MySQL Configuration
To fix that issue, we need to make some more configuration changes. First we need to remove some unnecessary links:
rm /etc/rc*/*mysqld
Now this prevents MySQL from starting at boot. Since we want MySQL to start at boot, let’s see if we can fix that. To do this, we need to create a new configuration file:
vi /lib/systemd/system/mysql.service
And put the following text in that file:
[Unit]
Description=MySQL database server
After=syslog.target
After=network.target
[Service]
ExecStart=/usr/bin/mysqld_safe
ExecStop=/bin/kill -15 $MAINPID
PIDFile=/var/lib/mysql/mysql.pid
Restart=always
[Install]
WantedBy=multi-user.target
Now we can enable the MySQL service to start at boot and then start the MySQL service:
systemctl enable mysql.service
systemctl start mysql.service
Then we should be able to check the status of the service and make sure that it is running:
systemctl status mysql.service
Now you should be able to restart without getting the log in error and MySQL should be running!
Installing MySQL Client Library
The last thing that we need to do is install the MySQL C programming client library:
opkg install libmysqlclient-dev
Now, let’s test to make sure that it installed correctly and works. To do this, create small test script:
#include <mysql.h>
#include <stdio.h>
int main(void)
{
printf(“MySQL client version: %s\n”, mysql_get_client_info());
return 0;
}
This will print out the version information for the MySQL client. To build it type:
g++ test.cpp -o test -I/usr/include/mysql –lmysqlclient
And then to run it, type:
./test
Now MySQL should be installed on your BeagleBone and ready to use!
Video
Next Article
In the next article, we will create a C program that will measure the temperature of the room. It will then write the current temperature into the MySQL database. Finally, we will create a web interface to retrieve and plot the measured temperatures of the room.