One place for hosting & domains

      MariaDB

      How To Store and Retrieve Data in MariaDB Using Python on Ubuntu 18.04


      The author selected the Tech Education Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      MariaDB is an open source version of the popular MySQL relational database management system (DBMS) with a SQL interface for accessing and managing data. It is highly reliable and easy to administer, which are essential qualities of a DBMS capable of serving modern applications. With Python’s growing popularity in technologies like artificial intelligence and machine learning, MariaDB makes a good option for a database server for Python.

      In this tutorial, you will connect a Python application to a database server using the MySQL connector. This module allows you to make queries on the database server from within your application. You’ll set up MariaDB for a Python environment on Ubuntu 18.04 and write a Python script that connects to and executes queries on MariaDB.

      Prerequisites

      Before you begin this guide, you will need the following:

      Step 1 — Preparing and Installing

      In this step, you’ll create a database and a table in MariaDB.

      First, open your terminal and enter the MariaDB shell from the terminal with the following command:

      Once you’re in the MariaDB shell, your terminal prompt will change. In this tutorial, you’ll write Python to connect to an example employee database named workplace and a table named employees.

      Start by creating the workplace database:

      • CREATE DATABASE workplace;

      Next, tell MariaDB to use workplace as your current database:

      You will receive the following output, which means that every query you run after this will take effect in the workplace database:

      Output

      Database changed

      Next, create the employees table:

      • CREATE TABLE employees (first_name CHAR(35), last_name CHAR(35));

      In the table schema, the parameters first_name and a last_name are specified as character strings (CHAR) with a maximum length of 35.

      Following this, exit the MariaDB shell:

      Back in the terminal, export your MariaDB authorization credentials as environment variables:

      • export username="username"
      • export password="password"

      This technique allows you to avoid adding credentials in plain text within your script.

      You’ve set up your environment for the project. Next, you’ll begin writing your script and connect to your database.

      Step 2 — Connecting to Your Database

      In this step, you will install the MySQL Connector and set up the database.

      In your terminal, run the following command to install the Connector:

      • pip3 install mysql-connector-python

      pip is the standard package manager for Python. mysql-connector-python is the database connector Python module.

      Once you’ve successfully installed the connector, create and open a new file Python file:

      In the opened file, import the os module and the mysql.connector module using the import keyword:

      database.py

      import os
      import mysql.connector as database
      

      The as keyword here means that mysql.connector will be referenced as database in the rest of the code.

      Next, initialize the authorization credentials you exported as Python variables:

      database.py

      . . .
      username = os.environ.get("username")
      password = os.environ.get("password")
      

      Follow up and establish a database connection using the connect() method provided by database. The method takes a series of named arguments specifying your client credentials:

      database.py

      . . .
      connection = database.connect(
          user=username,
          password=password,
          host=localhost,
          database="workplace")
      

      You declare a variable named connection that holds the call to the database.connect() method. Inside the method, you assign values to the user, password, host, and database arguments. For user and password, you will reference your MariaDB authorization credentials. The host will be localhost by default if you are running the database on the same system.

      Lastly, call the cursor() method on the connection to obtain the database cursor:

      database.py

      . . .
      cursor = connection.cursor()
      

      A cursor is a database object that retrieves and also updates data, one row at a time, from a set of data.

      Leave your file open for the next step.

      Now you can connect to MariaDB with your credentials; next, you will add entries to your database using your script.

      Step 3 — Adding Data

      Using the execute() method on the database cursor, you will add entries to your database in this step.

      Define a function add_data() to accept the first and last names of an employee as arguments. Inside the function, create a try/except block. Add the following code following your cursor object:

      database.py

      . . .
      def add_data(first_name, last_name):
          try:
              statement = "INSERT INTO employees (first_name,last_name) VALUES (%s, %s)"
              data = (first_name, last_name)
              cursor.execute(statement, data)
              connection.commit()
              print("Successfully added entry to database")
          except database.Error as e:
              print(f"Error adding entry to database: {e}")
      

      You use the try and except block to catch and handle exceptions (events or errors) that disrupt the normal flow of program execution.

      Under the try block, you declare statement as a variable holding your INSERT SQL statement. The statement tells MariaDB to add to the columns first_name and last_name.

      The code syntax accepts data as parameters that reduce the chances of SQL injection. Prepared statements with parameters ensure that only given parameters are securely passed to the database as intended. Parameters are generally not injectable.

      Next you declare data as a tuple with the arguments received from the add_data function. Proceed to run the execute() method on your cursor object by passing the SQL statement and the data. After calling the execute() method, you call the commit() method on the connection to permanently save the inserted data.

      Finally, you print out a success message if this succeeds.

      In the except block, which only executes when there’s an exception, you declare database.Error as e. This variable will hold information about the type of exception or what event happened when the script breaks. You then proceed to print out an error message formatted with e to end the block using an f-string.

      After adding data to the database, you’ll next want to retrieve it. The next step will take you through the process of retrieving data.

      Step 4 — Retrieving Data

      In this step, you will write a SQL query within your Python code to retrieve data from your database.

      Using the same execute() method on the database cursor, you can retrieve a database entry.

      Define a function get_data() to accept the last name of an employee as an argument, which you will call with the execute() method with the SELECT SQL query to locate the exact row:

      database.py

      . . .
      def get_data(last_name):
          try:
            statement = "SELECT first_name, last_name FROM employees WHERE last_name=%s"
            data = (last_name,)
            cursor.execute(statement, data)
            for (first_name, last_name) in cursor:
              print(f"Successfully retrieved {first_name}, {last_name}")
          except database.Error as e:
            print(f"Error retrieving entry from database: {e}")
      

      Under the try block, you declare statement as a variable holding your SELECT SQL statement. The statement tells MariaDB to retrieve the columns first_name and last_name from the employees table when a specific last name is matched.

      Again, you use parameters to reduce the chances of SQL injection.

      Smilarly to the last function, you declare data as a tuple with last_name followed by a comma. Proceed to run the execute() method on the cursor object by passing the SQL statement and the data. Using a for loop, you iterate through the returned elements in the cursor and then print out if there are any successful matches.

      In the except block, which only executes when there is an exception, declare database.Error as e. This variable will hold information about the type of exception that occurs. You then proceed to print out an error message formatted with e to end the block.

      In the final step, you will execute your script by calling the defined functions.

      Step 5 — Running Your Script

      In this step, you will write the final piece of code to make your script executable and run it from your terminal.

      Complete your script by calling add_data() and get_data() with sample data (strings) to verify that your code is working as expected.

      If you would like to add multiple entries, you can call add_data() with further sample names of your choice.

      Once you finish working with the database make sure that you close the connection to avoid wasting resources:
      connection.close():

      database.py

      import os
      import mysql.connector as database
      
      username = os.environ.get("username")
      password = os.environ.get("password")
      
      connection = database.connect(
          user=username,
          password=password,
          host=localhost,
          database="workplace")
      
      cursor = connection.cursor()
      
      def add_data(first_name, last_name):
          try:
          statement = "INSERT INTO employees (first_name,last_name) VALUES (%s, %s)"
          data = (first_name, last_name)
            cursor.execute(statement, data)
          cursor.commit()
          print("Successfully added entry to database")
          except database.Error as e:
          print(f"Error adding entry to database: {e}")
      
      def get_data(last_name):
          try:
            statement = "SELECT first_name, last_name FROM employees WHERE last_name=%s"
            data = (last_name,)
            cursor.execute(statement, data)
            for (first_name, last_name) in cursor:
              print(f"Successfully retrieved {first_name}, {last_name}")
          except database.Error as e:
            print(f"Error retrieving entry from database: {e}")
      
      add_data("Kofi", "Doe")
      get_data("Doe")
      
      connection.close()
      

      Make sure you have indented your code correctly to avoid errors.

      In the same directory, you created the database.py file, run your script with:

      You will receive the following output:

      Output

      Successfully added entry to database Successfully retrieved Kofi, Doe

      Finally, return to MariaDB to confirm you have successfully added your entries.

      Open up the MariaDB prompt from your terminal:

      Next, tell MariaDB to switch to and use the workplace database:

      After you get the success message Database changed, proceed to query for all entries in the employees table:

      You output will be similar to the following:

      Output

      +------------+-----------+ | first_name | last_name | +------------+-----------+ | Kofi | Doe | +------------+-----------+ 1 row in set (0.00 sec)

      Putting it all together, you’ve written a script that saves and retrieves information from a MariaDB database.

      You started by importing the necessary libraries. You used mysql-connector to connect to the database and os to retrieve authorization credentials from the environment. On the database connection, you retrieved the cursor to carry out queries and structured your code into add_data and get_data functions. With your functions, you inserted data into and retrieved data from the database.

      If you wish to implement deletion, you can build a similar function with the necessary declarations, statements, and calls.

      Conclusion

      You have successfully set up a database connection to MariaDB using a Python script on Ubuntu 18.04. From here, you could use similar code in any of your Python projects in which you need to store data in a database. This guide may also be helpful for other relational databases that were developed out of MySQL.

      For more on how to accomplish your projects with Python, check out other community tutorials on Python.



      Source link

      How To Reset Your MySQL or MariaDB Root Password on Ubuntu 20.04


      The author selected the COVID-19 Relief Fund to receive a donation as part of the Write for DOnations program.

      Introduction

      Forgot your database password? It happens to the best of us. If you’ve forgotten or lost the root password to your MySQL or MariaDB database, you can still gain access and reset the password if you have access to the server and a user account with sudo privileges.

      This tutorial demonstrates how to reset the root password for MySQL and MariaDB databases installed with the apt package manager on Ubuntu 20.04. The procedure for changing the root password differs depending on whether you have MySQL or MariaDB installed and the default systemd configuration that ships with the distribution or packages from other vendors. While the instructions in this tutorial may work with other system or database server versions, they have been tested with Ubuntu 20.04 and distribution-supplied packages.

      Note: On fresh Ubuntu 20.04 installations, the default MySQL or MariaDB configuration usually allows you to access the database (with full administrative privileges) without providing a password as long as you make the connection from the system’s root account. In this scenario, it may not be necessary to reset the password. Before you proceed with resetting your database root password, try to access the database with the sudo mysql command. Only if the default configuration for authentication was altered, and this results in an access denied error, follow the steps in this tutorial.

      Prerequisites

      To recover your MySQL or MariaDB root password, you will need:

      Note: Both database installation guides retain the default configuration for the database root account where a password is not needed to authenticate, as long as you can access the system’s root account. You can still follow this guide to set and verify a new password.

      Step 1 — Identifying the Database Version and Stopping the Server

      Ubuntu 20.04 runs either MySQL or MariaDB—a popular drop-in replacement that is fully compatible with MySQL. You’ll need to use different commands to recover the root password depending on which of these you have installed, so follow the steps in this section to determine which database server you’re running.

      Check your version with the following command:

      If you’re running MariaDB, you’ll see “MariaDB” preceded by the version number in the output:

      MariaDB output

      mysql Ver 15.1 Distrib 10.3.25-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

      You’ll see output like this if you’re running MySQL:

      MySQL output

      mysql Ver 8.0.22-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))

      Note the database you are running. This will determine the appropriate commands to follow in the rest of this tutorial.

      In order to change the root password, you’ll need to shut down the database server. If you’re running MariaDB, you can do so with the following command:

      • sudo systemctl stop mariadb

      For MySQL, shut down the database server by running:

      • sudo systemctl stop mysql

      With the database stopped, you can restart it in safe mode to reset the root password.

      Step 2 — Restarting the Database Server Without Permission Checks

      Running MySQL and MariaDB without permission checking allows accessing the database command line with root privileges without providing a valid password. To do this, you need to stop the database from loading the grant tables, which store user privilege information. Since this is a bit of a security risk, you may also want to disable networking to prevent other clients from connecting to the temporarily vulnerable server.

      Depending on which database server you’ve installed, the way of starting the server without loading the grant tables differs.

      Configuring MariaDB to Start Without Grant Tables

      In order to start the MariaDB server without the grant tables, we’ll use the systemd unit file to set additional parameters for the MariaDB server daemon.

      Execute the following command, which sets the MYSQLD_OPTS environment variable used by MariaDB upon startup. The --skip-grant-tables and --skip-networking options tell MariaDB to start up without loading the grant tables or networking features:

      • sudo systemctl set-environment MYSQLD_OPTS="--skip-grant-tables --skip-networking"

      Then start the MariaDB server:

      • sudo systemctl start mariadb

      This command won’t produce any output, but it will restart the database server, taking into account the new environment variable settings.

      You can ensure it started with sudo systemctl status mariadb.

      Now you should be able to connect to the database as the MariaDB root user without supplying a password:

      You’ll immediately see a database shell prompt:

      Now that you have access to the database server, you can change the root password as shown in Step 3.

      Configuring MySQL to Start Without Grant Tables

      In order to start the MySQL server without its grant tables, you’ll alter the systemd configuration for MySQL to pass additional command-line parameters to the server upon startup.

      To do this, execute the following command:

      • sudo systemctl edit mysql

      This command will open a new file in the nano editor, which you’ll use to edit MySQL’s service overrides. These change the default service parameters for MySQL.

      This file will be empty. Add the following content:

      MySQL service overrides

      [Service]
      ExecStart=
      ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
      

      The first ExecStart statement clears the default value, while the second one provides systemd with the new startup command, including parameters to disable loading the grant tables and networking capabilities.

      Press CTRL-x to exit the file, then Y to save the changes that you made, then ENTER to confirm the file name.

      Reload the systemd configuration to apply these changes:

      • sudo systemctl daemon-reload

      Now start the MySQL server:

      • sudo systemctl start mysql

      The command will show no output, but the database server will start. The grant tables and networking will not be enabled.

      Connect to the database as the root user:

      You’ll immediately see a database shell prompt:

      Now that you have access to the server, you can change the root password.

      Step 3 — Changing the Root Password

      The database server is now running in a limited mode; the grant tables are not loaded, and there’s no networking support enabled. This lets you access the server without providing a password, but it prohibits you from executing commands that alter data. To reset the root password, you must load the grant tables now that you’ve gained access to the server.

      Tell the database server to reload the grant tables by issuing the FLUSH PRIVILEGES command:

      You can now change the root password. The method you use depends on whether you are using MariaDB or MySQL.

      Changing the MariaDB Password

      If you are using MariaDB, execute the following statement to set the password for the root account, making sure to replace new_password with a strong new password that you’ll remember:

      • ALTER USER 'root'@'localhost' IDENTIFIED BY 'new_password';

      You’ll see this output indicating that the password changed:

      Output

      Query OK, 0 rows affected (0.001 sec)

      MariaDB allows using custom authentication mechanisms, so execute the following two statements to make sure MariaDB will use its default authentication mechanism for the new password you assigned to the root account:

      • UPDATE mysql.user SET authentication_string = '' WHERE user="root";
      • UPDATE mysql.user SET plugin = '' WHERE user="root";

      You’ll see the following output for each statement:

      Output

      Query OK, 0 rows affected (0.01 sec)

      The password is now changed. Type exit to exit the MariaDB console and proceed to Step 4 to restart the database server in normal mode.

      Changing the MySQL Password

      For MySQL, execute the following statement to change the root user’s password, replacing new_password with a strong password you’ll remember. MySQL allows using custom authentication mechanisms, so the following statement also makes sure that MySQL will use its default authentication mechanism to authenticate the root user using the new password:

      • ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password BY 'new_password';

      You’ll see this output indicating the password was changed successfully:

      Output

      Query OK, 0 rows affected (0.01 sec)

      The password is now changed. Exit the MySQL console by typing exit.

      Let’s restart the database in normal operational mode.

      Step 4 — Reverting Your Database Server to Normal Settings

      In order to restart the database server in its normal mode, you have to revert the changes you made so that networking is enabled and the grant tables are loaded. Again, the method you use depends on whether you used MariaDB or MySQL.

      For MariaDB, unset the MYSQLD_OPTS environment variable you set previously:

      • sudo systemctl unset-environment MYSQLD_OPTS

      Then, restart the service using systemctl:

      • sudo systemctl restart mariadb

      For MySQL, remove the modified systemd configuration:

      • sudo systemctl revert mysql

      You’ll see output similar to the following:

      Output

      Removed /etc/systemd/system/mysql.service.d/override.conf. Removed /etc/systemd/system/mysql.service.d.

      Then, reload the systemd configuration to apply the changes:

      • sudo systemctl daemon-reload

      Finally, restart the service:

      • sudo systemctl restart mysql

      The database is now restarted and is back to its normal state. Confirm that the new password works by logging in as the root user with a password:

      You’ll be prompted for a password. Enter your new password, and you’ll gain access to the database prompt as expected.

      Conclusion

      You have restored administrative access to the MySQL or MariaDB server. Make sure the new password you chose is strong and secure, and keep it in a safe place.

      For more information on user management, authentication mechanisms, or ways of resetting database passwords for other versions of MySQL or MariaDB, please refer to the official MySQL documentation or MariaDB documentation.



      Source link

      What is MariaDB?


      MariaDB is an open-source relational database management system. As with other relational databases, MariaDB stores data in tables made up of rows and columns. Users can define, manipulate, control, and query data using Structured Query Language, more commonly known as SQL.

      MariaDB is a fork of MySQL, and thus the two share many features and design choices. Interestingly, MariaDB’s lead developer is Michael Widenius — the original author of MySQL — and MariaDB is named after Widenius’s younger daughter Maria, similar to how MySQL is named after his older daughter, My.

      For more educational resources related to MariaDB, please visit:

      A complete list of our educational resources on MariaDB can be found on our MariaDB tag page.



      Source link