Home · Guides / 4.2

Guide 4.2

Setup MySQL on Ubuntu

The original MySQL page was unpublished beyond a heading. These steps complete the LEMP companion for the NGINX and PHP guides: install MySQL on Ubuntu and keep it on localhost only.

  1. Install MySQL Server from APT.

    sudo apt update
    sudo apt install mysql-server
  2. Run the bundled hardening script and follow the prompts (set a root password if asked, remove anonymous users, disallow remote root, remove the test database, reload privileges).

    sudo mysql_secure_installation
  3. Confirm MySQL is listening only on localhost.

    sudo ss -nltp | grep mysql

    You want 127.0.0.1:3306, not 0.0.0.0:3306. If needed, set this in /etc/mysql/mysql.conf.d/mysqld.cnf and restart:

    bind-address = 127.0.0.1
    sudo service mysql restart
  4. Create a database and a local user for your site. Do not create a user that can connect from %.

    sudo mysql
    CREATE DATABASE onion_site;
    CREATE USER 'onion'@'localhost' IDENTIFIED BY 'choose-a-strong-password';
    GRANT ALL PRIVILEGES ON onion_site.* TO 'onion'@'localhost';
    FLUSH PRIVILEGES;
    EXIT;
  5. Enable MySQL on boot.

    sudo systemctl enable mysql.service
Why localhost only The onion service already fronts HTTP on 127.0.0.1. MySQL should not be reachable from the network. PHP on the same host connects through localhost or the local socket.