User Tools

Site Tools


mysql

MySQL

Install and configure a MySQL server

$ sudo apt install mysql-server
$ sudo systemctl status mysql
[sudo] password for user_name:
● mysql.service - MySQL Community Server
     Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
     Active: active (running) since Thu 2025-02-20 09:14:29 JST; 6min ago
   Main PID: 77669 (mysqld)
     Status: "Server is operational"
      Tasks: 37 (limit: 19031)
     Memory: 363.0M (peak: 377.9M)
        CPU: 2.396s
     CGroup: /system.slice/mysql.service
             └─77669 /usr/sbin/mysqld
 
 220 09:14:28 localhost systemd[1]: Starting mysql.service - MySQL Community Server...
 220 09:14:29 localhost systemd[1]: Started mysql.service - MySQL Community Server.
$ sudo ss -tap | grep -Ei 'mysql|state'
State      Recv-Q Send-Q                            Local Address:Port                       Peer Address:Port Process                                                                                                                                                                                                                                                  
LISTEN     0      70                                    127.0.0.1:33060                           0.0.0.0:*     users:(("mysqld",pid=77669,fd=21))                                                                                                                                                                                                                      
LISTEN     0      151                                   127.0.0.1:mysql                           0.0.0.0:*     users:(("mysqld",pid=77669,fd=23))                                                                       
$ sudo ufw allow from 192.168.1.0/24 to any port 33060 proto tcp comment 'Allow mysql from local'
$ sudo journalctl -u mysql
 220 09:14:28 localhost systemd[1]: Starting mysql.service - MySQL Community Server...
 220 09:14:29 localhost systemd[1]: Started mysql.service - MySQL Community Server.

https://ubuntu.com/server/docs/install-and-configure-a-mysql-server#database-engines

Configuring MySQL

$ sudo mysql

Then run the following ALTER USER command to change the root user’s authentication method to one that uses a password. The following example changes the authentication method to mysql_native_password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
  ex)
mysql> alter user 'root'@'localhost' identified with mysql_native_password by '******';

:!: It will fail with some special charactors.(like !, *)

mysql> exit
$ sudo mysql_secure_installation
$ sudo mysql_secure_installation
Securing the MySQL server deployment.
 
Enter password for user root:
 
VALIDATE PASSWORD COMPONENT 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 component?
 
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: 2
Using existing password for root.
 
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
 
 ... skipping.
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) : no
 
 ... 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) : y
 - Dropping test database...
Success.
 
 - Removing privileges on test database...
Success.
 
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!

login as root with password

$ sudo mysql --user=root --password
Enter password:******
  or
$ sudo mysql -u root -p
mysql> alter user 'root'@'localhost' identified with auth_socket;
Query OK, 0 rows affected (0.01 sec)

You can once again connect to MySQL as your root user using:

$ sudo mysql

Creating a Dedicated MySQL User and Granting Privileges

mysql> create user 'user_name'@'host' identified with authentication_plugin by 'password';

:!: Wrapping both the user_name and host in single quotes isn’t always necessary, but doing so can help to prevent errors.

create a user that authenticates with caching_sha2_password:

mysql> create user 'user_name'@'localhost' identified by 'password';

There is a known issue with some versions of PHP that causes problems with caching_sha2_password

If you plan to use this database with a PHP application — phpMyAdmin, for example — you may want to create a user that will authenticate with the older, though still secure, mysql_native_password plugin instead:

mysql> create user 'user_name'@'localhost' identified with mysql_native_password by 'password';

You can alter it later on with this command:

mysql> alter user 'user_name'@'localhost' identified with mysql_native_password by 'password';

The general syntax for granting user privileges is as follows:

mysql> grant privilege on database.table to 'user_name'@'host';
mysql> create user 'user_name'@'localhost' identified with auth_socket;
  or
mysql> create user 'user_name'@'localhost' identified with mysql_native_password;
mysql> alter user 'user_name'@'localhost' identified with auth_socket;
mysql> create user 'user_name'@'localhost' identified with auth_socket;
Query OK, 0 rows affected (0.01 sec)
 
mysql> select user,host from mysql.user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
| wweb             | localhost |
+------------------+-----------+
6 rows in set (0.00 sec)

Granting Privileges

The general syntax for granting user privileges is as follows:

mysql> grant privilege on database.table to 'user_name'@'host';
  ex)
mysql> grant create, alter, drop, insert, update, index, delete, select, references, reload on *.* to 'wweb'@'localhost' with grant option;

sql commands

mysql> SELECT user FROM mysql.user;
+------------------+
| user             |
+------------------+
| debian-sys-maint |
| mysql.infoschema |
| mysql.session    |
| mysql.sys        |
| root             |
+------------------+
5 rows in set (0.00 sec)
mysql> SHOW databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.09 sec)
mysql> use mysql;
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
 
Database changed
mysql> select user,host from user;
mysql> select user,host from user;
+------------------+-----------+
| user             | host      |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.infoschema | localhost |
| mysql.session    | localhost |
| mysql.sys        | localhost |
| root             | localhost |
+------------------+-----------+
5 rows in set (0.00 sec)

declare a variable

mysql> set @start = 1, @finish = 10;
Query OK, 0 rows affected (0.00 sec)
 
mysql> select @start, @finish;
+--------+---------+
| @start | @finish |
+--------+---------+
|      1 |      10 |
+--------+---------+
1 row in set (0.00 sec)

Syntax:

mysql> SHOW {DATABASES | SCHEMAS}  [LIKE 'pattern' | WHERE expr];

create databases

mysql> CREATE database student;

Show/List all Databases

mysql> SHOW databases;
mysql> SHOW schemas;

Show/List Databases using LIKE

Show/List all the databases starting with the letter 's'.

mysql> SHOW databases LIKE 's%';
mysql> SHOW schemas LIKE 's%';
mysql> SELECT schema_name 
mysql> FROM information_schema.SCHEMATA 
mysql> WHERE schema_name LIKE 's%';

MySQL has four default system databases:

information_schemametadata about the MySQL server, including information about databases, tables, columns, data types, character sets, and privileges.
mysqlsystem-related data such as user account details, user privileges, and metadata information about administration. It includes tables like user, db, tables_priv, columns_priv, and host.
performance_schemaaggregates performance-related data about the MySQL server’s operations and resource usage.
sysa group of stored procedures, functions, and views that are not only critical in providing insights into the MySQL server's performance but also provide a source of information as far as the server's configuration is concerned.

password change

mysql> UPDATE mysql.user SET authentication_string=PASSWORD("password") WHERE User="root";
 
mysql> UPDATE mysql.user SET Password=PASSWORD("password") WHERE User="root";
 
mysql> mysqladmin -u root password [newpassword]
mysql.txt · Last modified: 2025/02/26 03:55 by jianwu