LoginSignup
3
2

More than 1 year has passed since last update.

Ubuntu20.04にMySQLを導入

Posted at

ゼミの研究にあたり、UbuntuにMySQLを導入するのに少しつまづいたため備忘録として記事を書きます。

環境

  • Ubuntu 20.04.4 LTS
  • MySQL Ver 8.0.30-0ubuntu0.20.04.2 for Linux on x86_64 ((Ubuntu))
  • Windows10 Home

環境構築

それではここから環境構築の手順を記述していきます。

ライブラリのインストール

$ sudo apt update
$ sudo apt install mysql-server

Socketファイルのパス指定

MySQLを一旦止めます。

$ sudo service mysql stop

Socketファイルのパスを取得します。

$ mysql_config --socket

そして、my.cnfに取得したSocketファイルのパスを指定します。

$ sudo vi /etc/my.cnf
my.cnf
[mysqld]
socket=socketファイルのパス

サーバーを再起動します。

$ sudo service mysql start

rootユーザーのパスワード設定

$ sudo mysql -u root

パスワードポリシーの設定を下げます。

set global validate_password.policy = "LOW";

rootユーザーのパスワードを設定します。

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by 'パスワード';

mysql_secure_installationの実行

最後にMySQLの初期設定を行います。

$ sudo mysql_secure_installation

# 先ほど設定したパスワードを入力
Enter password for user root:

# 何も入力せずEnterを押す
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

 ... 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) : 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のサーバーはUbuntuを閉じてしまうと停止するため、Ubuntu起動時に自動でMySQLのサーバーを起動するように設定しましょう。

$ sudo update-rc.d mysql defaults

以上で設定は完了です。

mysql-serverの環境を完全削除

途中で予期せぬエラー等が発生した場合、以下のコマンドを打ってmysql-serverの環境をリセットしてください。

$ sudo apt-get remove --purge mysql-server* mysql-common 
$ sudo apt-get autoremove --purge 
$ sudo rm -r /etc/mysql
$ sudo rm -r /var/lib/mysql

いかがだったでしょうか?

なんだかんだでDockerでMySQLの環境構築を行うことが多かったため少し手こずりました。

なにはともあれ無事環境構築できてよかったです。

ご不明店頭あればコメントにてご質問ください。

Thank you for reading

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2