Saturday, November 12, 2016

IOT: Installing MySQL and PhpMyAdmin on Raspberry Pi


Objective: The objective of this tutorial is to install MySQL and PhpMyAdmin on Raspberry Pi. 
Steps:
1. Connect to Raspberry Pi via SSH with you valid credentials
2. Once connected, let’s install mysql server on Raspberry Pi using the following command:
sudo apt-get install mysql-server

It will ask you “Do you want to continue? [Y/n]”, click Y and hit enter.
3. You will be prompted to enter a password for the root user. Enter the password and click <Ok>, which will continue with the installation again. Make sure you write this down as we will need to use this to access the MYSQL server and connect PHPMyAdmin to it.
4.  Once the installation is complete, you will be get a screen like this and the cursor waiting for the next command.
5.  Now if you want to access and start make changes to the database simply enter the following command:
mysql -u root -p
6.  It will now prompt you to enter the password we just created.
7.  Once you enter the password, you will get a mysql> prompt as shown below.
8.  You can now enter MYSQL commands to create, alter, and delete databases.
9.  Let’s create a sample database named agilerulesdb (You can use your own name for the database) and here is the command in my case:
CREATE DATABASE agilerulesdb;
10. The 'USE agilerulesdb' command tells the shell to use that database in future operations in this shell session.
USE agilerulesdb;
11. Let's create a database user and give it access to the database. I am creating a user named admin and the password as password (the one followed by IDENTIFIED BY). You can keep the user id and password which you like. This user is allowed to connect to the database from 'localhost'.
CREATE USER ‘admin’@’localhost’ IDENTIFIED BY ‘password’;
12. Initially, the new user has no privileges, so it must be granted some access rights using the 'GRANT' command. I have used 'ALL' in this example, but in real world applications it would be better to grant users more limited rights. A complete list of privilege options is available at http://dev.mysql.com/doc/refman/5.1/en/grant.html.
GRANT ALL PRIVILEGES ON agilerulesdb.* TO 'admin'@'localhost';

13. You can leave the command line by simply entering quit and relogin with the new user (admin in my case) that is created using the following command and enter the password that you had set.
mysql -u admin –p
14. Let’s now create a sample table (led_details in my case, you can create your own table) and before that we need to point to the database (agilerulesdb in my case) that was created.
use agilerulesdb;
CREATE TABLE led_details 
(
ID int NOT NULL AUTO_INCREMENT,
BlinkStatus varchar(3), 
DateTimeStamp DATETIME, 
BlinkCounter INT, 
JsonPayload varchar(255),
PRIMARY KEY (ID)
);
15. To exit from the mysql, use the command quit.
16. If you want to be able to interact with MYSQL in Python you will need to install the Python bindings as well. You can do this by entering the following command.
sudo apt-get install python-mysqldb
17. Now we are done installing the Raspberry Pi MYSQL you may want to install PHPMyAdmin so you have a nice clean interface to work with.
Installing PhpMyAdmin on Raspberry Pi (Optional)
18. If you have not installed Apache and PhpMyadmin, install them using the following command:
sudo apt-get install apache2 php5 libapache2-mod-php5
19. It will inform the memory requirement for this install and ask for your confirmation saying “Do you want to Continue? [Y/n]” and enter Y, which will continue further with the installation.
20. Now let’s install the PHPMyAdmin package, you can do this by entering the following:
sudo apt-get install phpmyadmin
21. It will inform the memory requirement for this install and ask for your confirmation saying “Do you want to Continue? [Y/n]” and enter Y, which will continue further with the installation.
22. You will be presented with a screen asking the type of web server you want it to run off. Select apache2 (by entering space bar) as this is the server we just installed and enter tab and click “OK”. It will continue further with the installation.
23. Next we will need to configure PHPMyAdmin to connect a database. To do this select yes at the next prompt.
24. It will now ask for a password, enter the one we set previously when we set up MYSQL.
Next it will ask you to set a password for PHPMyAdmin. You can keep it the same as the password to the MYSQL database or something separate. Make sure you remember it as this is the password you will need to access it.
25. The installation will continue further and finally you will see something like this once the installation is complete.
26. We will also need to setup Apache to include our PHPMyAdmin installation. To do this enter:
sudo nano /etc/apache2/apache2.conf

27. Now at the bottom of this file enter the following line:
Include /etc/phpmyadmin/apache.conf
28. Once done save & exist by pressing CTRL +X and then y.
29. Now simply restart the Apache service by entering the following command:
sudo /etc/init.d/apache2 restart
30. Now you should be able to access the PHPMyAdmin from a browser. To test go to the follow address in your browser. (Replace the IP with your IP. If you don’t have it run the command hostname –I on your Pi)
http://192.168.1.2/phpmyadmin/ 
Enter the root as the user id and password that you had set before.
31. On the Raspberry Pi we need to edit the configuration file ‘my.cnf’ in the directory /etc/mysql/. We do this with the following command;
sudo nano /etc/mysql/my.cnf
Scroll down the file a short way till we find the section [mysqld]. Here we need to edit the line that reads something similar to;
bind-address = 127.0.0.1
This line is telling MySQL to only listen to commands that come from the ‘localhost’ network (127.0.0.1). Essentially only the Raspberry Pi itself. We can change this by inserting a ‘#’ in front of the line which will turn the line into a comment instead of a configuration option. So change it to look like this;

# bind-address           = 127.0.0.1
32. Once we have saved the file, we need to restart the MySQL service with the following command;
sudo service mysql restart

No comments:

Post a Comment