How to install MySQL database on raspberry pi

What you will learn here about MYSQL database installation on raspberry pi

  • How to install MYSQL database on raspberry pi
  • How to connect MySQL database using Java on raspberry

How to install MYSQL database on raspberry pi

Please follow the following steps to know how to install MYSQL server on raspberry pi or to know how to install MariaDB server on raspberry pi.
1)Please open the raspberry pi terminal.

2)Execute the following command to update the existing packages.

sudo apt-get update

3)Now execute the following to install MySQL server which is shown below. While installing if it is asking do you want to continue then please enter y and hit enter

sudo apt-get install mysql-server

install mysql server on raspberry pi




4)Now please execute the following command for secure installation which is shown below.

sudo mysql_secure_installation

mysql server on raspberry pi
5)Please hit Enter for current root password.

6)Now please Enter y and hit Enter for setting a new password which is shown below
Set password for root user
7)Now Please enter New password which is shown below
Enter new password
8)Now please enter y to remove anonymous user and hit Enter.
remove anonymous user
9)Now please enter y to disallow remote login which is shown below
disallow remote login mysql server
10)Please enter y to remove test databases which is shown below
remove test databases
11)Please enter y to reload privileges tables which is shown below
reload previleges
12)Now please execute the following command to login into the database and Enter the password which you have entered in step 7.

sudo mysql -u root -p

login to mysql database
13)Please execute the following command to see databases present in the mysql database.

show databases;

show all databases in mysql database
14)Execute the following to create Demo database in mysql server which is shown below.

CREATE DATABASE Demo;

Create database in mysql server
15)Now please execute the following to go in Demo database

USE Demo;

use database
16)Please execute the following command to create database user

CREATE USER ‘admin’@’localhost’ IDENTIFIED BY ‘admin’;

Create user in mysql database
17)Execute the following command to grant all previleges

GRANT ALL PRIVILEGES ON Demo.* TO ‘admin’@’localhost’;

Grant all provileges
18)Now execute the following command save all the changes

FLUSH PRIVILEGES;

Flush privileges
19)Now please execute the following command to come out of database.

quit

Exit mysql server
20)Execute the following command to restart the MYSQL server

sudo service mysql restart

restart mysql server on raspberry pi



How to insert and fetch data from MySQL database

Please follow the following steps to insert and fetch from the MySQL database.
1)Open the raspberry pi terminal

2)Execute the following command to login to the database and enter the password which is shown below.

sudo mysql -u root -p

Login to mysql database
3)Execute the following command to use Demo database which is shown above.

USE Demo;

4)Execute the following command to create login table which has two coloums i.e is username and password which is shown above.

create table login(username varchar(25), password varchar(25));

5)Execute the following command to insert data into login table which is shown below.

insert into login values(‘admin’,’admin123′);

Insert data into mysql server
6)To see the inserted values please execute the following command which is shown below

select * from login;

Fetch data from mysql database

How to connect MySQL server using Java on raspberry

Please download the following java project code to connect with MySQL database using java on raspberry pi.

IMPORTANT NOTE: The following code will only work if you have followed all the above steps successfully.

Download mysql connect using java code (8 downloads)

You may also like...

Leave a Reply