Installing MySQL on Ubuntu 16.04: A Comprehensive Guide
This guide outlines multiple methods for installing MySQL on Ubuntu 16.04, covering APT repository, offline DEB bundle, and binary tarball installations.
Installation Method Overview
Before installation, consider the following:
- APT Installation: Simplest method using Ubuntu's package manager. Automatically configures services and environment variables.
- DEB Bundle: Offline package installation providing more control over specific versions.
- Binary Tarball: Manual installation offfering maximum control and flexibility, suitable for producsion servers.
Method 1: APT Repository Installation
This method follows the official MySQL APT repository guide and installs latest version (5.7.18 at time of writting).
Step 1: Configure MySQL APT Repository (Optional)
Download and configure the repository setup tool:
sudo dpkg -i mysql-apt-config_0.8.6-1_all.deb
Update package lists:
sudo apt-get update
Step 2: Install MySQL Server
sudo apt-get install mysql-server
If dependency issues occur, resolve them with:
sudo apt-get install -f
Step 3: Installation Configuration
During installation, you'll be prompted to set the root password.
Step 4: Post-Installation Verification
Check MySQL service status and connectivity:
# Check if MySQL is listening on default port
sudo netstat -anp | grep mysql
# Service management commands
sudo service mysql start
sudo service mysql stop
sudo service mysql status
# Connect to MySQL instance
mysql -h localhost -P 3306 -uroot -p
# Verify installation
SHOW DATABASES;
Step 5: Uninstallation
# Remove MySQL server
sudo apt-get remove mysql-server
# Clean up dependencies
sudo apt-get autoremove
# List installed MySQL packages
dpkg -l | grep mysql | grep ii
Method 2: Offline DEB Bundle Installation
Step 1: Download Bundle
Download the appropriate DEB bundle from MySQL's official download page.
Step 2: Extract Bundle
tar xvf mysql-server_5.7.18-1ubuntu16.04_amd64.deb-bundle.tar
Step 3: Install Dependencies and Packages
# Install required library
sudo apt-get install libaio1
# Configure server package
sudo dpkg-preconfigure mysql-community-server_*.deb
# Install packages in specific order
sudo dpkg -i mysql-common_*.deb
sudo dpkg -i mysql-community-client_*.deb
sudo dpkg -i mysql-client_*.deb
sudo dpkg -i mysql-community-server_*.deb
sudo dpkg -i mysql-server_*.deb
# Fix any dependency issues
sudo apt-get -f install
Method 3: Binary Tarball Installation
Step 1: Download and Extract
tar zxvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
sudo mv mysql-5.7.18-linux-glibc2.5-x86_64 /usr/local
sudo ln -s /usr/local/mysql-5.7.18-linux-glibc2.5-x86_64/ /usr/local/mysql
Step 2: Install Dependencies and Configure
# Install required library
sudo apt-get install libaio1
# Create MySQL user and group
sudo groupadd mysql
sudo useradd -r -g mysql -s /bin/false mysql
# Navigate to installation directory
cd /usr/local/mysql
# Create necessary directories
sudo mkdir mysql-files
sudo chmod 750 mysql-files
# Set ownership
sudo chown -R mysql .
sudo chgrp -R mysql .
# Initialize database (note the temporary root password in output)
sudo bin/mysqld --initialize --user=mysql
# Generate SSL certificates
sudo bin/mysql_ssl_rsa_setup
# Reset ownership for security
sudo chown -R root .
sudo chown -R mysql data mysql-files
Step 3: Start MySQL Service
sudo bin/mysqld_safe --user=mysql &
Step 4: Secure Initial Login and Configure Remote Access
# Connect using temporary password
/usr/local/mysql/bin/mysql -uroot -p
# Change root password and configure access
UPDATE mysql.user SET authentication_string=PASSWORD('NewSecurePass123!') WHERE User='root';
FLUSH PRIVILEGES;
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'NewSecurePass123!' WITH GRANT OPTION;
FLUSH PRIVILEGES;
Step 5: Configure Automatic Startup
# Copy service script
sudo cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
# Install service manager
sudo apt-get install sysv-rc-conf
# Configure startup
sudo sysv-rc-conf
Service management commands:
sudo service mysql.server status
sudo service mysql.server start
sudo service mysql.server stop
Step 6: Set Environment Variables
Add MySQL binaries to PATH in ~/.profile:
export PATH="$PATH:/usr/local/mysql/bin"
Reload profile:
source ~/.profile