• Home
  • LLMs
  • Docker
  • Kubernetes
  • Java
  • Maven
  • About
Install | MySQL
  1. References
  2. Installation
  3. Allow remote access
  4. Some useful commands
  5. Manage Users
  6. Manage Databases
  7. Manage Tables
  8. Export/backup a database
  9. MySQL "SHOW STATUS" command
  10. MySQL "SHOW PROCESSLIST" command
  11. Set the logs expiration days

  1. References
    See this page for more details on how to install MySQL:
    https://help.ubuntu.com/lts/serverguide/mysql.html
  2. Installation
    $ sudo apt install mysql-server

    • Verify the version of MySQL:
      $ mysql --version
      mysql  Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using  EditLine wrapper

    • Connect to MySQL as "root" (using "sudo" because "root" is configured with "auth_socket" plugin):
      $ sudo mysql -u root
      [sudo] password for mtitek:
      Welcome to the MySQL monitor.
      Commands end with ; or \g.
      Your MySQL connection id is 10
      Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)
      
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

      You need to use sudo otherwise you will get this error: "ERROR 1698 (28000): Access denied for user 'root'@'localhost'"

      MySQL in Ubuntu is using "auth_socket" plugin for the "root" user.
      Which means that the "root" user will be authorized by the system user credentials.

      mysql> USE mysql;

      mysql> SELECT user, host, plugin FROM user;
      +------------------+-----------+-----------------------+
      | user             | host      | plugin                |
      +------------------+-----------+-----------------------+
      | root             | localhost | auth_socket           |
      | mysql.session    | localhost | mysql_native_password |
      | mysql.sys        | localhost | mysql_native_password |
      | debian-sys-maint | localhost | mysql_native_password |
      +------------------+-----------+-----------------------+

    • Set up MySQL Security (root password):
      $ sudo mysql_secure_installation
      [sudo] password for mtitek:
      Securing the MySQL server deployment.
      
      Connecting to MySQL using a blank password.
      
      VALIDATE PASSWORD PLUGIN can be used to test passwords and improve security.
      It checks the strength of password and allows the users to set only those passwords which are secure enough.
      Would you like to setup VALIDATE PASSWORD plugin?
      
      Press y|Y for Yes, any other key for No: y
      
      There are three levels of password validation policy:
      LOW    Length >= 8
      MEDIUM Length >= 8, numeric, mixed case, and special characters
      STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
      Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
      
      Please set the password for root here.
      New password:
      Re-enter new password:
      Estimated strength of the password: 25
      Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
      
      By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them.
      This is intended only for testing, and to make the installation go a bit smoother.
      You should remove them before moving into a production environment.
      
      Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
      Success.
      
      Normally, root should only be allowed to connect from 'localhost'.
      This ensures that someone cannot guess at the root password from the network.
      Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
       ... skipping.
      
      By default, MySQL comes with a database named 'test' that anyone can access.
      This is also intended only for testing, and should be removed before moving into a production environment.
      Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
       ... skipping.
      
      Reloading the privilege tables will ensure that all changes made so far will take effect immediately.
      Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
      Success.
      
      All done!

    • MySQL configuration directory:
      $ ls -al /etc/mysql/
      drwxr-xr-x  root root  mysql.conf.d
          -rw-r--r-- 1  root root  mysqld.cnf
          -rw-r--r-- 1  root root  mysqld_safe_syslog.cnf
      drwxr-xr-x  root root  conf.d
          -rw-r--r-- 1  root root  mysql.cnf
          -rw-r--r-- 1  root root  mysqldump.cnf
      -rw-r--r--  root root  mysql.cnf
      lrwxrwxrwx  root root  my.cnf -> /etc/alternatives/my.cnf
      -rw-r--r--  root root  my.cnf.fallback
      -rw-------  root root  debian.cnf
      -rwxr-xr-x  root root  debian-start

    • MySQL data directory:
      $ sudo ls -al /var/lib/mysql
      -rw-r-----  mysql mysql  auto.cnf
      -rw-r--r--  root  root   debian-5.7.flag
      -rw-r-----  mysql mysql  ib_buffer_pool
      -rw-r-----  mysql mysql  ibdata1
      -rw-r-----  mysql mysql  ib_logfile0
      -rw-r-----  mysql mysql  ib_logfile1
      -rw-r-----  mysql mysql  ibtmp1
      drwxr-x---  mysql mysql  mysql
      drwxr-x---  mysql mysql  performance_schema
      drwxr-x---  mysql mysql  sy

    • MySQL log directory:
      $ ls -al /var/log/mysql/
      -rw-r-----  mysql adm  error.log
  3. Allow remote access
    • Connect to MySQL as "root" (using "sudo" because "root" is configured with "auth_socket" plugin):
      $ sudo mysql -u root
      [sudo] password for mtitek:

    • Locate and change the value of "bind-address" configuration under "[mysql]":
      $ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
      [mysql]
      #bind-address           = 127.0.0.1
      bind-address            = 0.0.0.0

    • Restart the MySQL server:
      $ sudo service mysql restart
  4. Some useful commands
    • Connect to MySQL as "root" (using "sudo" because "root" is configured with "auth_socket" plugin):
      $ sudo mysql -u root
      [sudo] password for mtitek:

    • Status:
      mysql> status;
      mysql  Ver 14.14 Distrib 5.7.27, for Linux (x86_64) using  EditLine wrapper
      
      Connection id: 10
      Current database: mysql
      Current user: root@localhost
      SSL: Not in use
      Current pager: stdout
      Using outfile: ''
      Using delimiter: ;
      Server version: 5.7.27-0ubuntu0.18.04.1 (Ubuntu)
      Connection: Localhost via UNIX socket
      Server characterset: latin1
      Db     characterset: latin1
      Client characterset: utf8
      Conn.  characterset: utf8
      UNIX socket: /var/run/mysqld/mysqld.sock

    • Select configuration variables:
      mysql> show variables like 'version';
      +---------------+--------+
      | Variable_name | Value  |
      +---------------+--------+
      | version       | 5.7.27 |
      +---------------+--------+

    • Another way to select configuration variables:
      $ echo "show variables like 'version';" | sudo mysql -u root
      [sudo] password for mtitek:
      Variable_name  Value
      version        5.7.27

    • Show current database:
      mysql> select database();
      +------------+
      | database() |
      +------------+
      | NULL       |
      +------------+

    • Show current user:
      mysql> select user();
      +----------------+
      | user()         |
      +----------------+
      | root@localhost |
      +----------------+
  5. Manage Users
    • Create a user (bind to localhost):
      mysql> CREATE USER user1@localhost IDENTIFIED BY 'user1';
      mysql> FLUSH PRIVILEGES;

      Create a user (bind to any host):
      mysql> CREATE USER user1@'%' IDENTIFIED BY 'user1';

      You might get this error: "ERROR 1819 (HY000): Your password does not satisfy the current policy requirements"
      To check the Password Validation Plugin configuration:
      mysql> SHOW VARIABLES LIKE 'validate_password%';
      +--------------------------------------+-------+
      | Variable_name                        | Value |
      +--------------------------------------+-------+
      | validate_password_check_user_name    | OFF   |
      | validate_password_dictionary_file    |       |
      | validate_password_length             | 8     |
      | validate_password_mixed_case_count   | 1     |
      | validate_password_number_count       | 1     |
      | validate_password_policy             | LOW   |
      | validate_password_special_char_count | 1     |
      +--------------------------------------+-------+

      You can change any of these settings:
      mysql> SET GLOBAL validate_password_length = 5;
      mysql> SET GLOBAL validate_password_number_count = 0;

    • Grant permissions to a user (bind to localhost):
      mysql> GRANT ALL ON mydb.* TO user1@localhost;

      Grant permissions to a user (bind to any host):
      mysql> GRANT ALL ON mydb.* TO user1@'%';

    • Drop a user (bind to localhost):
      mysql> DROP USER 'user1'@'localhost';

      Drop a user (bind to any host):
      mysql> DROP USER 'user1'@'%';

    • Select information of users:
      mysql> use mysql;

      mysql> select host, user from user;
      mysql> select * from user;

    • Connect to MySQL (using a database user configured with "mysql_native_password" plugin):
      $ mysql -h localhost -u user1 -p
      $ mysql --host=localhost --user=user1 --password
      $ mysql --host=localhost --user=user1 --password=user1
  6. Manage Databases
    • Create a database:
      mysql> help create database;

      mysql> CREATE DATABASE mydb DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;

    • Show all databases:
      mysql> show databases;
      +--------------------+
      | Database           |
      +--------------------+
      | information_schema |
      | mydb               |
      | mysql              |
      | performance_schema |
      | sys                |
      +--------------------+

    • Select information of databases:
      mysql> use mysql;

      mysql> select host, db, user from db;
      mysql> select * from db;

    • Select a database:
      mysql> use mydb;

      mysql> select database();
      +----------+
      | Database |
      +----------+
      | mydb     |
      +----------+

    • Delete a database:
      mysql> drop database mydb;
  7. Manage Tables
    • Create a table:
      mysql> create table table1 (id integer);

    • Show all tables:
      mysql> show tables;

    • Select data of a table:
      mysql> select * from table1;

    • Drop a table:
      mysql> drop table table1;
  8. Export/backup a database
    • Export/backup a database:
      mysql> mysqldump -u user1 -p -B mydb > user1-database-dump.sql
  9. MySQL "SHOW STATUS" command
    • Connect to MySQL:
      $ sudo mysql -u root
      [sudo] password for mtitek:

    • Show MySQL open database connections:
      mysql> SHOW STATUS WHERE variable_name like "Connection%";
      +-----------------------------------+-------+
      | Variable_name                     | Value |
      +-----------------------------------+-------+
      | Connection_errors_accept          | 0     |
      | Connection_errors_internal        | 0     |
      | Connection_errors_max_connections | 0     |
      | Connection_errors_peer_address    | 0     |
      | Connection_errors_select          | 0     |
      | Connection_errors_tcpwrap         | 0     |
      | Connections                       | 7     |
      +-----------------------------------+-------+
  10. MySQL "SHOW PROCESSLIST" command
    • Connect to MySQL:
      $ sudo mysql -u root
      [sudo] password for mtitek:

    • Show MySQL process list:
      mysql> SHOW processlist;
      +----+------+-----------+------+---------+------+-------+------------------+
      | Id | User | Host      | db   | Command | Time | State | Info             |
      +----+------+-----------+------+---------+------+-------+------------------+
      |  5 | root | localhost | NULL | Sleep   |  596 |       | NULL             |
      |  6 | root | localhost | NULL | Query   |    0 | init  | SHOW processlist |
      +----+------+-----------+------+---------+------+-------+------------------+

    • Kill a process (kill <process-id>):
      mysql> kill 5
  11. Set the logs expiration days
    Setup the 'expire_logs_days' system variable to expire binary log files automatically after a given number of days.

    • Show 'expire_logs_days' value:
      mysql> show variables like 'expire_logs_days';
      +------------------+-------+
      | Variable_name    | Value |
      +------------------+-------+
      | expire_logs_days | 10    |
      +------------------+-------+

    • You can setup the 'expire_logs_days' system variable:
      mysql> set global expire_logs_days = 7;

    • Or you can set the 'expire_logs_days' system variable in "my.cnf" file ("vmy.ini"):
      expire_logs_days = 7;
© 2025  mtitek