添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

// Tutorial //

How To Install MySQL on Ubuntu 20.04

Published on April 23, 2020 · Updated on July 11, 2022
English
How To Install MySQL on Ubuntu 20.04
Not using Ubuntu 20.04 ? Choose a different version or distribution.
Ubuntu 20.04

Introduction

MySQL is an open-source database management system, commonly installed as part of the popular LAMP (Linux, Apache, MySQL, PHP/Python/Perl) stack. It implements the relational model and uses Structured Query Language (better known as SQL) to manage its data.

This tutorial will go over how to install MySQL version 8.0 on an Ubuntu 20.04 server. By completing it, you will have a working relational database that you can use to build your next website or application.

Create a MySQL database quickly using DigitalOcean Managed Databases . Let DigitalOcean focus on maintaining, upgrading, and backups.

Prerequisites

  • initial server setup guide for Ubuntu 20.04 .
  • Step 1 — Installing MySQL

        1. Step 2 — Configuring MySQL

                  1. Step 3 — Creating a Dedicated MySQL User and Granting Privileges

                        1. The MySQL documentation recommends this plugin for users who want to log in with a password due to its strong security features.

                          Run the following command to create a user that authenticates with caching_sha2_password. Be sure to change sammy to your preferred username and password to a strong password of your choosing:

                                1. the official MySQL documentation.

                                  Run this GRANT statement, replacing sammy with your own MySQL user’s name, to grant these privileges to your user:

                                        1. Step 4 — Testing MySQL

                                            1. Conclusion

                                            2. Set up a LAMP stack or a LEMP stack
                                            3. Practice running queries with SQL
                                            4. Manage your MySQL installation with phpMyAdmin

    About the authors
    Default avatar

    staff technical writer

    hi! i write do.co/docs now, but i used to be the senior tech editor publishing tutorials here in the community.


    Default avatar

    Manager, Developer Education

    Technical Writer @ DigitalOcean


    Still looking for an answer?

    Ask a question Search for more help

    Was this helpful?
    10 Comments
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    melvinpg7 August 10, 2021

    Hello Mark, Thanks for the share of this tutorial, it helped me a lot, First time I do this without any issues and very clear all your steps on this article. Appreciate it. Thank You.

    Hello,

    How I can install MySQL 5? This is very important to me for using Magento 1 version.

    Thank you.

    Do anybody else have this issue upon running sud apt install mysql-server on a fresh droplet?

    Setting up mysql-server-8.0 (8.0.32-0ubuntu0.22.10.2) ...
    update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
    Renaming removed key_buffer and myisam-recover options (if present)
    mysqld will log errors to /var/log/mysql/error.log
    2023-03-17T17:42:33.732349Z 0 [ERROR] [MY-011065] [Server] Unable to determine if daemon is running: Invalid argument (rc=0).
    2023-03-17T17:42:33.733195Z 0 [ERROR] [MY-010946] [Server] Failed to start mysqld daemon. Check mysqld error log.
    Warning: Unable to start the server.
    Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
    Job for mysql.service failed.
    See "systemctl status mysql.service" and "journalctl -xeu mysql.service" for details.
    invoke-rc.d: initscript mysql, action "start" failed.
    ● mysql.service - MySQL Community Server
         Loaded: loaded (/lib/systemd/system/mysql.service; enabled; preset: enabled)
         Active: activating (auto-restart) (Result: oom-kill) since Fri 2023-03-17 17:42:36 UTC; 52ms ago
        Process: 2883 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
        Process: 2891 ExecStart=/usr/sbin/mysqld (code=killed, signal=KILL)
       Main PID: 2891 (code=killed, signal=KILL)
         Status: "Server startup in progress"
            CPU: 624ms
    dpkg: error processing package mysql-server-8.0 (--configure):
     installed mysql-server-8.0 package post-installation script subprocess returned error exit status 1
    Setting up libcgi-pm-perl (4.54-1) ...
    Setting up libhtml-template-perl (2.97-2) ...
    dpkg: dependency problems prevent configuration of mysql-server:
     mysql-server depends on mysql-server-8.0; however:
      Package mysql-server-8.0 is not configured yet.
    dpkg: error processing package mysql-server (--configure):
     dependency problems - leaving unconfigured
    Setting up libcgi-fast-perl (1:2.15-1) ...
    Processing triggers for man-db (2.10.2-2) ...
    No apport report written because the error message indicates its a followup error from a previous failure.
                  Processing triggers for libc-bin (2.36-0ubuntu4) ...
    Errors were encountered while processing:
     mysql-server-8.0
     mysql-server
    needrestart is being skipped since dpkg has failed
    E: Sub-process /usr/bin/dpkg returned an error code (1)
    

    I don’t see where I went wrong in copying-pasting 2 commands

    Note, this will not work on the $4 instance but works fine on the $6 ones. If you have a $4 and upgrade to $6 it will work as well.

    I recently ran into the issue of not being able to log in after Step 2- mysql_secure_installation in WSL2

    sudo mysql
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
    sudo mysql_secure_installation 
    # various prompts within this step
    mysql -u root -p
    mysql -u root -p
    Enter password: 
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (13)
    

    What worked was using TCP connection to connect rather than a socket connection

    mysql -u root -p --protocol=tcp
    

    OR use sudo

    sudo mysql -u root -p
    [sudo] password for vivek:  <---- the sudo user password
    Enter password: <---- mysql password
    

    “sudo mysql_secure_installation” on a brand-new Ubuntu 20.04 provided by Digital Ocean gave the following error after attempting to set the root password:

    “Failed! Error: SET PASSWORD has no significance for user ‘root’@‘localhost’ as the authentication method used doesn’t store authentication data in the MySQL server. Please consider using ALTER USER instead if you want to change authentication parameters.”

    So to workaround this, use CTRL-C to cancel the mysql_secure_installation script and do the following:

    sudo mysql
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '<rootpassword>';
    

    Then you can run “sudo mysql_secure_installation” again and this time, decline the option to change the root password so you can perform the other steps of the script.

    Note that once this is set, you’ll access mysql with the password, like so, instead of just “sudo mysql”

    mysql -u root -p
    
    Ganesh Dhumal January 19, 2022

    Great Article it worked for me

    This comment has been deleted

    hi

    I followed your tutorial on ubuntu 16.4 and it seems to be working all fine, now I have another question, my software instruction asks: the table engine must be “MyISAM”. With new MySQL versionit’s always InnoDB. Set “default-storage-engine” option to “MyISAM” in “/etc/mysql/my.cnf”