I just went through all this on a new install of pi OS for a Computer Science class I teach - setting up to show the students how to make python scripts to read from a database via an apache2 server on a pi 4 - old school, no new frameworks.
From start to end, these are the commands I ran. (All were run with sudo).
From https://pimylifeup.com/raspberry-pi-mysql/
apt install mariadb-server
mysql_secure_installation
(Follow the prompts to setup passwords)
Then, prepare for adding the mariadb package to python
From https://mariadb.com/docs/server/connect/programming-languages/c/install/
- apt install libmariadb3 libmariadb-dev
Finally add mariadb to python
- python -m pip install mariadb --break-system-packages
It warned me I shouldn't do this as root and should use
a virtual env instead (https://pip.pypa.io/warnings/venv)
This all worked fine. The sample code I used for testing was to make a simple try.py script as follows (made sure the script was executable) and it worked without any trouble:
#!/usr/bin/python import mariadb conn = mariadb.connect( db='dbname', user='username', passwd='******', host='localhost') c = conn.cursor() c.execute("SELECT * FROM sometable") for row in c.fetchall(): print(row)
Specs: This is on pi OS 6.1.0-rpi7-rpi-v8 #1 SMP PREEMPT Debian 1:6.1.63-1+rpt1 (2023-11-24) aarch64, with Python 3.11.2, and mariadb 10.11.4-MariaDB-1~deb12u1 Debian 12