MariaDB: logging in, SQL and fixing errors
MariaDB is the database (a drop-in replacement for MySQL: the mysql and mariadb commands are the same program). You can manage it with phpMyAdmin or from the terminal. The terminal is faster once you know it, and it still works when phpMyAdmin does not.
Users and permissions have their own page: MariaDB users and permissions.
Logging in
sudo mariadb # as MariaDB root, no password (always works on the Pi)
mysql -u Nypuser -p # as a normal user, asks for the password
mysql -u Nypuser -p MySensor # and open a database straight away
The prompt shows the database you are in: MariaDB [MySensor]>. Leave with exit.
Prompt survival
| You see | Meaning | Do |
|---|---|---|
-> | MariaDB is waiting for the rest of the statement | You forgot the ;. Type ; and Enter. |
'> or "> | An unclosed quote | Type the matching quote, then ; |
| A mistake halfway through | Type \c and Enter to cancel the statement | |
| Output too wide to read | End the statement with \G instead of ; (one column per line) |
Running SQL without entering MariaDB
Useful for quick checks and scripts:
mysql -u Nypuser -p MySensor -e "SELECT COUNT(*) FROM Data;"
Finding your way around
SHOW DATABASES;
USE MySensor;
SHOW TABLES;
DESCRIBE Data; -- columns, types, defaults
SHOW CREATE TABLE Data; -- the full SQL that would rebuild the table (also for tables made in phpMyAdmin)
SELECT DATABASE(), CURRENT_USER(), NOW(); -- where am I, who am I, what time does MariaDB think it is
SELECT NOW(); shows the Pi's clock as MariaDB sees it. If it is wrong, every new timestamp is wrong: set the date.
Reading your sensor data
These use the lab's Data table in MySensor:
SELECT * FROM Data ORDER BY SerialNo DESC LIMIT 10; -- newest 10 rows
SELECT COUNT(*), MIN(Temp), AVG(Temp), MAX(Temp) FROM Data; -- statistics
SELECT * FROM Data WHERE Temp > 30; -- filter
SELECT * FROM Data WHERE SensorDate >= CURDATE(); -- today's rows only
SELECT DATE(SensorDate) AS day, COUNT(*), AVG(Temp)
FROM Data GROUP BY day; -- one line per day
Changing and deleting rows
UPDATE Data SET SensorID = 25 WHERE SerialNo = 3; -- change one row
DELETE FROM Data WHERE SerialNo = 3; -- delete one row
DELETE FROM Data WHERE SensorDate < '2026-03-26'; -- rows saved while the clock was wrong
TRUNCATE TABLE Data; -- empty the table, numbering restarts at 1
:::danger Always use WHERE
UPDATE or DELETE without WHERE changes or deletes every row. There is no undo. Run the same WHERE with SELECT first to see which rows it hits.
:::
Practise without touching your lab data
CREATE DATABASE Practice;
USE Practice;
CREATE TABLE Readings (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
room VARCHAR(20),
temp FLOAT,
taken TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
INSERT INTO Readings (room, temp) VALUES ('lab', 25.3), ('home', 29.1);
SELECT * FROM Readings;
DROP DATABASE Practice; -- delete it all when done
Column types in one minute
| Type | Holds | Example |
|---|---|---|
INT | Whole numbers | SerialNo, SensorID |
FLOAT | Decimal numbers | 25.31 |
VARCHAR(20) | Text up to 20 characters | a room name |
TIMESTAMP / DATETIME | Date and time | 2026-03-26 14:55:02 |
INT(7) does not limit the number to 7 digits: the number in brackets is only a display width. AUTO_INCREMENT needs the column to be a key (PRIMARY or UNIQUE), which is why phpMyAdmin asks for an index when you tick A_I.
Backup and restore
From the Linux prompt, not inside MariaDB:
mariadb-dump -u Nypuser -p MySensor > ~/mysensor_backup.sql # backup to a file
mysql -u Nypuser -p MySensor < ~/mysensor_backup.sql # restore into MySensor
The backup is a plain text file of SQL statements. Copy it off the card (how). To restore into a fresh card, create the empty database first: CREATE DATABASE MySensor;.
Common errors
| Error | Meaning | Fix |
|---|---|---|
ERROR 1045 Access denied for user | Wrong user, password or host | Users and hosts |
ERROR 1698 Access denied for user 'root'@'localhost' | root uses unix_socket | sudo mariadb (why) |
ERROR 1044 Access denied ... to database | Logged in, but no rights on that database | GRANT (permissions) |
ERROR 1046 No database selected | You did not USE a database | USE MySensor; or write MySensor.Data |
ERROR 1049 Unknown database | Wrong name (check capitals) or not created | SHOW DATABASES; |
ERROR 1146 Table ... doesn't exist | Wrong name or wrong database | SHOW TABLES; and check capitals: Data ≠ data |
ERROR 1054 Unknown column | Column name typo | DESCRIBE Data; |
ERROR 1064 You have an error in your SQL syntax ... near '...' | Typo. The quoted part is where MariaDB got confused | Look just before the quoted text: missing comma, quote or bracket. Smart quotes (’) pasted from a PDF also cause this. |
ERROR 1062 Duplicate entry | A UNIQUE/PRIMARY column already has that value | Let AUTO_INCREMENT fill it in; do not insert SerialNo yourself |
ERROR 1364 Field ... doesn't have a default value | A NOT NULL column was left out of INSERT | Give it a value, or a default in the table |
ERROR 2002 Can't connect ... through socket | MariaDB is not running | sudo systemctl start mariadb, then why it stopped |
ERROR 2003 Can't connect to MySQL server on '...' | Not running, or not listening on the network | Same as above, or remote connections |