Ubuntu - Installing MySQL & Python
Ubuntu - Installing Python and MySQL
Section titled “Ubuntu - Installing Python and MySQL”Python is a versatile programming language, and MySQL is a popular relational database management system. Both are commonly used on Linux systems for development and server applications. Here’s how to install and set them up on Ubuntu.
Installing and Managing Python
Section titled “Installing and Managing Python”Modern Ubuntu versions come with Python 3 pre-installed. It’s the recommended version for new projects.
Step 1: Check Python 3 version. Open a terminal (Ctrl+Alt+T) and type:
python3 -VThis will display the installed Python 3 version (e.g., Python 3.10.x).
Note: The command python -V might point to Python 2 (if it’s still installed for legacy reasons) or not be found. Always use python3 for Python 3.
Step 2: Install Pip (Python Package Installer) and venv (Virtual Environment tool). While Python 3 is pre-installed, pip (for managing Python packages) and venv (for creating isolated Python environments) might not be. It’s best practice to install them:
sudo apt updatesudo apt install python3-pip python3-venvStep 3: Using Python Virtual Environments (Highly Recommended). Virtual environments allow you to manage dependencies for different projects separately, preventing conflicts.
To create a virtual environment for a project:
# Navigate to your project directorycd my_python_project
# Create a virtual environment named 'env' (or any name you prefer)python3 -m venv env
# Activate the virtual environment# On Linux/macOS:source env/bin/activate
# On Windows (Git Bash or WSL):# source env/Scripts/activate
# Your terminal prompt will now show (env) indicating it's active.# Now, when you use 'pip install <package>', it installs into this environment.
# To deactivate the environment when done:deactivateApplication Scenario: Developing a web application with Django. You’d create a virtual environment, activate it, then pip install django to get the Django framework specifically for that project.
Installing MySQL Server
Section titled “Installing MySQL Server”MySQL is a robust database server. Alternatively, MariaDB, a community-developed fork of MySQL, is a popular drop-in replacement and often available in Ubuntu repositories.
Step 1: Update package lists.
sudo apt updateStep 2: Install the MySQL server package.
sudo apt install mysql-serverDuring the installation, you might be prompted to set a root password for MySQL, or it might configure authentication using the auth_socket plugin by default for the root user (meaning system root can access MySQL root without a password).
Step 3: Secure your MySQL installation. This is a crucial step to improve security.
sudo mysql_secure_installationThis script will guide you through several security-related options:
- VALIDATE PASSWORD COMPONENT: Helps enforce strong passwords. You can choose to enable it or not.
- Change the root password? If you haven’t set one or want to change it. If
auth_socketis used for root, you might manage root access differently (e.g.,sudo mysql). - Remove anonymous users? Yes, for security.
- Disallow root login remotely? Yes, highly recommended. Root should only connect from localhost.
- Remove test database and access to it? Yes, it’s not needed for production.
- Reload privilege tables now? Yes, to apply changes immediately.
Answer these prompts according to your security needs. The defaults are generally good for a more secure setup.
Step 4: Check MySQL service status.
sudo systemctl status mysqlYou should see that the service is active (running). You can also use sudo systemctl start mysql, sudo systemctl stop mysql, sudo systemctl enable mysql (to start on boot).
Step 5: Connecting to MySQL. To connect as the MySQL root user:
# If auth_socket is enabled for root (common default):sudo mysql
# If you set a password for root and auth_socket is not primary:mysql -u root -pOnce connected, you can execute SQL commands. Type exit or quit to leave the MySQL prompt.
Common Learning Obstacle: MySQL root user authentication. Understanding the auth_socket plugin versus traditional password authentication can be confusing. For auth_socket, system root user can connect to MySQL as root without a password via sudo mysql. If you need password authentication for root, you might need to alter the root user’s authentication method within MySQL: ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password'; FLUSH PRIVILEGES;
Additional Resources: Official MySQL documentation (dev.mysql.com/doc/) and Python documentation (docs.python.org/3/) are invaluable.