How to install MySQl for Python on Linux?

Applications are being developed on Python nowadays. Developers usually prefer the operating system platform based on their convenience. Usually, developers are used to the command line terminal, Linux is the platform for them.

Developing applications in Python has advantages over handling complexity in a simpler manner. There are scenarios where Python applications need connection with MySQL.

To establish connectivity on the Linux platform, MySQL should be installed and set up properly. 

When the application logic is built in Python and the data is stored in MySQL, Python applications need connectivity to pull and push data to and from the MySQL database.

Get MySQL Installed on Linux [Step by Step]

For the purpose of installation we have taken Ubuntu as an Linux distro. Most of the steps would be same for any Linux distro except few commands. Please comment below if you want me to write a tutorial for any specific Linux distribution.

  1. The terminal window on Linux needs to be open.
  2. Enter and execute the command – Alternatively you can download it from https://dev.mysql.com/downloads/mysql/, from operating system drop-down select Linux
sudo apt install mysql-server
  1. The process of connecting to the MySQL downloadable is initiated.
Install MySQL in Linux using console
  1. Make sure there is enough hard disk space (245 MB) as mentioned before starting the installation process. 
MySQL installation starts in terminal
  1. Type y to start connecting to the downloadable.
  2. The downloading and installation process starts.
  3. Once completed, you shall see the “done!” text.
MySQL completed in Ubuntu
  1. To check MySQL installed successfully, enter and run the below command.
mysql --version
  1. The version shall be displayed as below.
mysql --version : check MySQL version in terminal
  1. For security purposes, set the password to access MySQL. Use the following command for setting up the password.
sudo mysql_secure_installation
  1.  Press Y to continue.
Securely install MySQL through console
  1. Setup the level of password to be set. A screen asking you the level shall be shown as below.
Press y on prompt
  1. Press 0 (LOW) for easy to remember the password. 
Set the password strength
  1. The next is to enter the new password and re-enter the same password to be set.
  2. Press Y to continue.
  3. Again, press Y where it is asking the user to remove anonymous users.
  4. To remove the test database and privileges, keep pressing Y on the prompts.
Setup database privileges through terminal
  1. You are all set with MySQL installed.

Also read:

Install MySQL Connector for Python

Pip is a package manager that allows the user to install Python packages easily. The steps to install pip and then MySQL connector are stated here.

  1. Start the terminal window.
  2. Enter and run the command :
sudo apt -get install python3 -pip
  1. Make sure that the system has enough space to install pip. Press Y to proceed.
install Python through PIP
  1. Post confirmation, download of pip package manager starts.
Download pip package for python
  1. Once done, the unpacking process starts.
unpack pip package through terminal
  1. Once done, the installation process completes and the command prompt reverts with $.
complete python installation though pip in Linux
  1. The next step is to install MySQL connector through pip.
  2. Enter the command – 
pip install mysql-connector -python
  1. ENTER to execute the command. The process downloading the package starts.
install MySQL connector for Python through pip
  1. Installation starts and once completed, the user gets to see a success message.
complete the installation of Python-MySQL connector
  1. Verify that MySQL works with Python. To do so:
    • Create the test.py file in the code editor. 
    • Enter the text below in the .py file.
import mysql.connectorprint(“Connector version : {0}” .format(mysql.connector.__version__))
  1. On the terminal, run this command:
python test.py
  1. You shall see the output as below.
Connector version : 8.0.17

Takeaway

After performing all the steps above, the developer will be easily able to connect to MySQL and fetch the data in the Python application and push data to MySQL using the MySQL connector.

Scroll to Top