How to Change a MariaDB/MySQL Data Directory to a New Location on Linux

Step 1 — Moving the MariaDB Data Directory mysql -u root -p select @@datadir; Output: +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set (0.00 sec) exit sudo systemctl stop mariadb sudo systemctl status mariadb Output: mysql systemd[1]: Stopped MariaDB database server. sudo rsync -av /var/lib/mysql /mnt/my-volume-01 sudo mv /var/lib/mysql /var/lib/mysql.bak Step 2 — Pointing to the New Data Location sudo vi /etc/my.cnf [mysqld] . . ....

March 7, 2023 · 1 min · 122 words · Andrew

How to count the amount of rows in MariaDB fast

If you need to find the fastest way to count the number of rows in a massive MariaDB, or MySQL table, then you can do the following instead of performing a select count() query: show table status like '<TABLE_NAME>' This will provide you with a table of information about the table statistics, including the amount of rows.

February 11, 2023 · 1 min · 57 words · Andrew

How to Join Multiple MySQL Tables in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. Presenting the data let’s take two (2) tables as a demonstration for the code below. Users – Table 1 { id: 1, name: 'Carl', fav: 254}, { id: 2, name: 'Emma', fav: 254}, { id: 3, name: 'John', fav: 255}, { id: 4, name: 'Hayley', fav:}, { id: 5, name: 'Andrew', fav:} Products – Table 2...

August 9, 2022 · 2 min · 316 words · Andrew

How to Limit a MySQL Query in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Limit the Result Returned from MySQL in Python import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers LIMIT 10") myresult = mycursor.fetchall() for x in myresult: print(x) How to Start From Another Position in MySQL from Python import mysql....

August 8, 2022 · 1 min · 117 words · Andrew

How to Update a MySQL Table in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Update a MySQL Table in Python import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() sql = "UPDATE customers SET address = 'Stoneleigh Place' WHERE address = 'Abbey Road'" mycursor.execute(sql) mydb.commit() print(mycursor....

August 7, 2022 · 1 min · 127 words · Andrew

How to Drop a MySQL Table in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Delete/Drop a MySQL Table in Python import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() sql = "DROP TABLE customers" mycursor.execute(sql) How to Delete/Drop Only if MySQL Table Exists in Python import mysql....

August 6, 2022 · 1 min · 99 words · Andrew

How to Delete MySQL Records in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Delete MySQL Records in Python import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() sql = "DELETE FROM customers WHERE address = 'The Rockies'" mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, "record(s) deleted") Prevent SQL Injection in MySQL queries through Python Specify the injected variable as the second argument to the execute command as below....

August 5, 2022 · 1 min · 131 words · Andrew

How to ORDER BY a MySQL Query in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Sort the Result of a MySQL Query in Python import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() sql = "SELECT * FROM customers ORDER BY name" mycursor.execute(sql) myresult = mycursor.fetchall() for x in myresult: print(x) How to Change Direction of MySQL Order in Python You can change the order of the sort by simply setting the order direction....

August 4, 2022 · 1 min · 170 words · Andrew

How to Filter WHERE MySQL Queries in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Select from MySQL with a Filter in Python You simply specify the WHERE clause in your SQL statement as follows: import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() sql = "SELECT * FROM customers WHERE address ='London Road'" mycursor....

August 3, 2022 · 2 min · 262 words · Andrew

How to Select From MySQL in Python

First, you will need the mysql.connector. If you are unsure of how to get this setup, refer to How to Install MySQL Driver in Python. How to Select From a MySQL Table in Python import mysql.connector mydb = mysql.connector.connect( host = "localhost", user = "username", password = "YoUrPaSsWoRd", database = "your_database" ) mycursor = mydb.cursor() mycursor.execute("SELECT * FROM customers") myresult = mycursor.fetchall() for x in myresult: print(x) How to Select Columns From a MySQL Table in Python import mysql....

August 2, 2022 · 1 min · 151 words · Andrew