Skip to main content

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 seeMeaningDo
->MariaDB is waiting for the rest of the statementYou forgot the ;. Type ; and Enter.
'> or ">An unclosed quoteType the matching quote, then ;
A mistake halfway throughType \c and Enter to cancel the statement
Output too wide to readEnd 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​

TypeHoldsExample
INTWhole numbersSerialNo, SensorID
FLOATDecimal numbers25.31
VARCHAR(20)Text up to 20 charactersa room name
TIMESTAMP / DATETIMEDate and time2026-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​

ErrorMeaningFix
ERROR 1045 Access denied for userWrong user, password or hostUsers and hosts
ERROR 1698 Access denied for user 'root'@'localhost'root uses unix_socketsudo mariadb (why)
ERROR 1044 Access denied ... to databaseLogged in, but no rights on that databaseGRANT (permissions)
ERROR 1046 No database selectedYou did not USE a databaseUSE MySensor; or write MySensor.Data
ERROR 1049 Unknown databaseWrong name (check capitals) or not createdSHOW DATABASES;
ERROR 1146 Table ... doesn't existWrong name or wrong databaseSHOW TABLES; and check capitals: Data ≠ data
ERROR 1054 Unknown columnColumn name typoDESCRIBE Data;
ERROR 1064 You have an error in your SQL syntax ... near '...'Typo. The quoted part is where MariaDB got confusedLook just before the quoted text: missing comma, quote or bracket. Smart quotes (’) pasted from a PDF also cause this.
ERROR 1062 Duplicate entryA UNIQUE/PRIMARY column already has that valueLet AUTO_INCREMENT fill it in; do not insert SerialNo yourself
ERROR 1364 Field ... doesn't have a default valueA NOT NULL column was left out of INSERTGive it a value, or a default in the table
ERROR 2002 Can't connect ... through socketMariaDB is not runningsudo systemctl start mariadb, then why it stopped
ERROR 2003 Can't connect to MySQL server on '...'Not running, or not listening on the networkSame as above, or remote connections