GNU/Linux >> Linux の 問題 >  >> Panels >> Panels

Ubuntu20.04にMySQLをインストールして使用する方法

このチュートリアルでは、Ubuntu 20.04にMySQLをインストールする方法と、基本的なMySQLコマンドでMySQLを使用する方法を示します。 MySQLはオープンソースのリレーショナルデータベース管理システムであり、その人気により、データを保存するためのさまざまなシステムで広く使用されています。

この投稿では、rootユーザーの有無にかかわらず、MySQLへのロギング、データベースの作成、ユーザーの作成、特権の付与、データベースへの外部アクセス、データベースのインポート、データベースのダンプの作成などについて詳しく学びます。始めましょう!

1。システムを更新する

OSとしてUbuntu20.04が新規インストールされていることを前提としているため、インストールを開始する前にシステムを更新します。

sudo apt update -y && sudo apt upgrade -y

2。 MySQLサーバーをインストールする

次のコマンドを使用してMySQLデータベースサーバーをインストールします。

sudo apt install mysql-server -y

このコマンドが実行されると、MySQL8.0のインストールプロセスが開始されます。 MySQL 8.0は、デフォルトでUbuntu20.04リポジトリに含まれています。インストールが完了したら、サービスが稼働しているかどうかを確認できます。

sudo systemctl status mysql

次の出力が得られるはずです:

root@vps:~# systemctl status mysql
● mysql.service - MySQL Community Server
     Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-11-04 17:07:12 UTC; 39s ago
   Main PID: 98088 (mysqld)
     Status: "Server is operational"
      Tasks: 38 (limit: 4617)
     Memory: 355.4M
     CGroup: /system.slice/mysql.service
             └─98088 /usr/sbin/mysqld
>

3。 MySQLサーバーを保護する

MySQLの使用を開始する前に、次のコマンドを使用して複数の手順でMySQLを保護する必要があります。

sudo mysql_secure_installation

このコマンドは、MySQLのセキュリティを構成したり、強力なルートパスワードを設定したりするのに役立つ複数の手順を実行します。以下の出力から、太字のオプションを使用する必要があります。

root@vps:~# sudo mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

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
Please set the password for root here.

New password: YourStrongRootPassword

Re-enter new password:YourStrongRootPassword

Estimated strength of the password: 100
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) : Y
Success.

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!

これでMySQLのインストールが保護され、次の手順でいくつかの便利なコマンドを実行できます

4。ルートパスワードの有無にかかわらずMySQLサーバーにログインします

MySQLを新規インストールした後、「 mysql 」と入力するだけで、MySQLプロンプトにログインできます。 MySQLを使用せずにサーバーのコマンドラインで「」コマンドを実行して、前の手順で設定したrootパスワードを要求します。

mysql」と入力した後 」MySQLプロンプトにログインします。

root@vps:~# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.27-0ubuntu0.20.04.1 (Ubuntu)

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

次のコマンドを実行し、パスワードを入力せずにEnterキーを押した場合も同様です。

mysql -u root -p

ルートパスワードを要求するようにMySQLを設定するには、MySQLプロンプトで次のコマンドを実行します。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongRootPassword';

実行後、次の入力が表示されます

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourStrongRootPassword';
Query OK, 1 rows affected (0.01 sec)

mysql

ここで、「 mysql」でログインしようとすると 」コマンドを実行すると、以下のメッセージが表示されます:

root@vps:~# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

以前に設定したrootパスワードを使用して、以下のコマンドを使用する必要があります。

mysql -u root -p

rootログインにパスワードを使用するかどうかを決定するのはあなたの選択です。パスワードなしでアクセスしたい場合は、この手順を無視して先に進んでください。

5。データベース、ユーザーの作成、および特権の付与

コマンド「showdatabases」を使用してMySQLプロンプトにデータベースをリストすると、MySQLからデフォルトのデータベースが取得されます。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

データベースを作成するには、次のコマンドを実行します。

Create database 'admin';

作成したら、データベースを再度一覧表示できます。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| admin              |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>

「adminuser」という名前のユーザーを作成し、「admin」データベースにパスワードを使用して権限を付与しましょう。

CREATE USER 'adminuser'@'localhost' IDENTIFIED by 'YourStrongPassword';
 GRANT ALL PRIVILEGES ON admin* TO 'adminuser'@'localhost';
 FLUSH PRIVILEGES;
 EXIT;

一度、データベースユーザーは「管理者」への権限で作成されます 」データベースでは、すべてが正しく設定されているかどうかを簡単に確認できます。

mysql -u adminuser -p

admin」のみが表示されます 」とデフォルトの「information_schema 」データベース:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| admin              |
| information_schema |
+--------------------+
2 rows in set (0.01 sec)

mysql>

6。ユーザーへの外部アクセス

ユーザー「adminuser」にリモートアクセスを許可するには 」と既存の「admin 」データベースは次のコマンドを実行します:

GRANT ALL ON admin.* TO adminuser@'remote_ip_address' IDENTIFIED BY 'YourStrongPassword';

ユーザーが任意のIPアドレスから接続できるようにする場合は、を使用します :

GRANT ALL ON admin.* TO adminuser@'%' IDENTIFIED BY 'YourStrongPassword';

7。データベースのダンプを作成する

データベースのダンプを作成するために、MySQLプロンプトにログインする必要はありません。これは、ユーザー名、データベース、およびパスワードを使用して、サーバーのコマンドライン端末から実行できます。 「管理者」のダンプを作成します rootユーザーのデータベース:

mysqldump -u root -p admin > dump.sql

ダンプが完了したら、リストしてすべてが正常かどうかを確認できます

root@vps:/# mysqldump -u root -p admin > dump.sql
Enter password:
root@vps:/# ls -al
total 12
drwxr-xr-x  2 root root 4096 Nov  4 21:38 .
drwxr-xr-x 19 root root 4096 Oct 18 14:09 ..
-rw-r--r--  1 root root 1266 Nov  4 21:40 dump.sql

8。データベースダンプのインポート

データベースのダンプを「admin」にインポートするには ” rootユーザーのデータベースでは、次のコマンドを使用する必要があります:

mysql -u root -p admin < dump.sql

これは、データベースをダンプおよびインポートするための構文です。

mysqldump -u "database user" -p "database name" > "name of dump file".sql

mysql -u "database user" -p "database name" < "name of dumped database".sql

データベースユーザーは、ダンプする必要のあるデータベースへの特権を持っている必要があることに注意してください。 MySQLのrootユーザーには、すべてのデータベースをダンプおよびインポートする権限があります。

おめでとうございます。MySQLサーバーに最も重要なコマンドをインストールして使用することができました

MySQLコマンドの詳細については、公式ドキュメントをご覧ください。もちろん、難しい場合は、24時間年中無休のテクニカルサポートに連絡してください。管理者がUbuntu20.04にMySQLサーバーをインストールして構成します。

Ubuntu 20.04にMySQLをインストールして使用する方法に関するこの投稿が気に入った場合は、左側のボタンを使用してソーシャルネットワーク上の友達と共有するか、下に返信を残してください。ありがとう。


Panels
  1. Ubuntu18.04にMySQLをインストールする方法

  2. Ubuntu20.04にMySQLデータベースをインストールする方法

  3. Ubuntu 22.04 に MySQL をインストールする方法

  1. Ubuntu20.04にMySQLをインストールする方法

  2. Ubuntu20.04にWineをインストールして使用する方法

  3. Ubuntu18.04にFFmpegをインストールして使用する方法

  1. Ubuntu18.04にWineをインストールして使用する方法

  2. Ubuntu18.04にCurlをインストールして使用する方法

  3. Ubuntu18.04にMySQLWorkbenchをインストールして使用する方法