Lesson 3: Create MySQL Database Table for Sensor Data

Learning Objectives:

  1. Learn how to access MySQL from Jupyter Notebook to create tables and uses
  2. Prepare location to store data collected from IoT devices

Exercise 1: Create a MySQL table to hold the sensor data from our Raspberry Pi

Note: This exercise will use an existing MySQL database server to expedite the process.
You can use these notes to create the setup in your own server.

Logon using an admin account and create a table called temps3 to hold sensor data:

Table Design Schema:

Field         Description
device       -- VARCHAR, Name of the device that logged the data
datetime     -- DATETIME, Date time in ISO 8601 format YYYY-MM-DD HH:MM:SS
temp         -- FLOAT, temperature data
hum          -- FLOAT, humidity data

Load the sql notebook extension:


In [3]:
%load_ext sql

Connect to the MySQL database instance using and account that has admin access and run SQL to drop/create table:


In [6]:
%%sql mysql://admin:admin@172.20.101.81/pidata
DROP TABLE if exists temps3;

CREATE TABLE temps3 (
    device varchar(20) DEFAULT NULL, 
    datetime datetime DEFAULT NULL,
    temp float DEFAULT NULL,
    hum float DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


0 rows affected.
0 rows affected.
Out[6]:
[]

Check to see that the table was created:


In [7]:
%sql show tables;


2 rows affected.
Out[7]:
Tables_in_pidata
temps
temps3

Exercise 2: Create a MySQL user that has access to the new table:

We will create a user called piuser that will have limited access to the temps3 table on the pidata database.

We will create a user account called

'piuser'@'%'

This means the piuser account can access the MySQL server from any hostname

Login to the MySQL database using an account that has admin access, then run the code to drop/create the new user:

Note: We will create a new user called piuser3 with a password logger that will have access to the temps3


In [29]:
%%sql mysql://admin:admin@172.20.101.81

DROP USER IF EXISTS piuser3;
CREATE USER 'piuser3'@'%' IDENTIFIED BY 'logger';
GRANT SELECT, INSERT, DELETE, UPDATE ON pidata.temps3 TO 'piuser3'@'%';
FLUSH PRIVILEGES;


0 rows affected.
0 rows affected.
0 rows affected.
0 rows affected.
Out[29]:
[]

In [30]:
%sql select * from mysql.user;


12 rows affected.
Out[30]:
Host User Select_priv Insert_priv Update_priv Delete_priv Create_priv Drop_priv Reload_priv Shutdown_priv Process_priv File_priv Grant_priv References_priv Index_priv Alter_priv Show_db_priv Super_priv Create_tmp_table_priv Lock_tables_priv Execute_priv Repl_slave_priv Repl_client_priv Create_view_priv Show_view_priv Create_routine_priv Alter_routine_priv Create_user_priv Event_priv Trigger_priv Create_tablespace_priv ssl_type ssl_cipher x509_issuer x509_subject max_questions max_updates max_connections max_user_connections plugin authentication_string password_expired password_last_changed password_lifetime account_locked
localhost root Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 mysql_native_password *27DE1D24423492A015780A5ECE94A2D845E7BC34 N 2017-07-12 15:58:56 None N
localhost mysql.sys N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE N 2017-07-12 15:58:56 None Y
localhost debian-sys-maint Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 mysql_native_password *882890A0B8EC80EBBF4E2CEBD82DD54A7C276825 N 2017-07-12 15:58:56 None N
localhost rmj Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 mysql_native_password *27DE1D24423492A015780A5ECE94A2D845E7BC34 N 2017-07-12 16:05:49 None N
localhost phpmyadmin N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *27DE1D24423492A015780A5ECE94A2D845E7BC34 N 2017-07-14 09:33:50 None N
192.168.8.131 pilogger N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *9B500343BC52E2911172EB52AE5CF4847604C6E5 N 2017-07-13 23:26:20 None N
192.168.8.131 rmj Y Y Y Y Y Y Y Y Y Y N Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 mysql_native_password *27DE1D24423492A015780A5ECE94A2D845E7BC34 N 2017-07-14 09:53:31 None N
% pilogger N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *9B500343BC52E2911172EB52AE5CF4847604C6E5 N 2017-07-15 21:41:36 None N
% rmj Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 mysql_native_password *27DE1D24423492A015780A5ECE94A2D845E7BC34 N 2017-07-23 13:00:04 None N
% admin Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 mysql_native_password *4ACFE3202A5FF5CF467898FC58AAB1D615029441 N 2017-07-23 13:02:13 None N
% user1 N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *D4C0AFEB72D2383E5F7F27D1128EBF33C012A725 N 2017-07-23 17:29:14 None N
% piuser3 N N N N N N N N N N N N N N N N N N N N N N N N N N N N N 0 0 0 0 mysql_native_password *D4C0AFEB72D2383E5F7F27D1128EBF33C012A725 N 2017-07-23 20:22:59 None N

Check to be sure the new user has access to the new table:


In [32]:
%%sql mysql://piuser3:logger@172.20.101.81/pidata
select * from temps3;


0 rows affected.
Out[32]:
device datetime temp hum

Exercise 3: Add test data to new table to confirm the new piuser account can add records to the table

Note: We are still connected to the pidata database using the piuser3@'%' account


In [34]:
for x in range(10):
    %sql INSERT INTO temps3 (device,datetime,temp,hum) VALUES('pi-003',date(now()),73.2,22.0);


1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.
1 rows affected.

Now we can check to see if the data was inserted as expected:


In [35]:
%sql SELECT * from temps3;


10 rows affected.
Out[35]:
device datetime temp hum
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0
pi-003 2017-07-23 00:00:00 73.2 22.0

Next we will empty the table so it will be ready for live sensor data:


In [37]:
%sql DELETE FROM temps3;


10 rows affected.
Out[37]:
[]

In [38]:
%sql SELECT * FROM temps3;


0 rows affected.
Out[38]:
device datetime temp hum