freedom-_-qの勉強履歴

メモ書きが主になるかと思います。勉強強制のために一日一記事目指してます。頭良くないので間違いが多々あるかと思います。

MySQLの環境構築

目次

はじめに

データベースの知識が全くないのでお勉強する。この時代でデータベースを理解できていないのは、例え製造業の技術者だとしても恥ずかしいので。
その前段階の環境構築を時系列で記録する。躓いてもそのままの流れで書く。

インストール

まずMySQLを入れます。
環境はWindows10でWSLを使ってUbuntu-20.04を動かしています。
何も考えず以下コマンドを実行します。

$ sudo apt install mysql-server mysql-client

なんかエラー出た(絶望)。

freedom@DESKTOP-6QUKUBN:~$ sudo apt install mysql-server mysql-client
...(略)...
Err:1 http://security.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client-core-8.0 amd64 8.0.21-0ubuntu0.20.04.3
  404  Not Found [IP: 91.189.88.142 80]
Err:2 http://security.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client-8.0 amd64 8.0.21-0ubuntu0.20.04.3
  404  Not Found [IP: 91.189.88.142 80]
Err:3 http://security.ubuntu.com/ubuntu focal-updates/main amd64 mysql-server-core-8.0 amd64 8.0.21-0ubuntu0.20.04.3
  404  Not Found [IP: 91.189.88.142 80]
Err:4 http://security.ubuntu.com/ubuntu focal-updates/main amd64 mysql-server-8.0 amd64 8.0.21-0ubuntu0.20.04.3
  404  Not Found [IP: 91.189.88.142 80]
Err:5 http://security.ubuntu.com/ubuntu focal-updates/main amd64 mysql-client all 8.0.21-0ubuntu0.20.04.3
  404  Not Found [IP: 91.189.88.142 80]
Err:6 http://security.ubuntu.com/ubuntu focal-updates/main amd64 mysql-server all 8.0.21-0ubuntu0.20.04.3
  404  Not Found [IP: 91.189.88.142 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-client-core-8.0_8.0.21-0ubuntu0.20.04.3_amd64.deb  404  Not Found [IP: 91.189.88.142 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-client-8.0_8.0.21-0ubuntu0.20.04.3_amd64.deb  404  Not Found [IP: 91.189.88.142 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-server-core-8.0_8.0.21-0ubuntu0.20.04.3_amd64.deb  404  Not Found [IP: 91.189.88.142 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-server-8.0_8.0.21-0ubuntu0.20.04.3_amd64.deb  404  Not Found [IP: 91.189.88.142 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-client_8.0.21-0ubuntu0.20.04.3_all.deb  404  Not Found [IP: 91.189.88.142 80]
E: Failed to fetch http://security.ubuntu.com/ubuntu/pool/main/m/mysql-8.0/mysql-server_8.0.21-0ubuntu0.20.04.3_all.deb  404  Not Found [IP: 91.189.88.142 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?

...わからん。
素直に提案されたコマンドを試してみよう。

$ sudo apt-get update
...(略)...
Fetched 19.0 MB in 7s (2877 kB/s)
Reading package lists... Done

良さげなので、再チャレンジ。

$ sudo apt install mysql-server mysql-client
...(略)...
done!
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up libhtml-parser-perl (3.72-5) ...
Setting up libhttp-message-perl (6.22-1) ...
Setting up mysql-server-8.0 (8.0.25-0ubuntu0.20.04.1) ...
invoke-rc.d: could not determine current runlevel
 * Stopping MySQL database server mysqld                                                                                                                                                                                              [ OK ]
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 1194
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
invoke-rc.d: could not determine current runlevel
Setting up libcgi-pm-perl (4.46-1) ...
Setting up libhtml-template-perl (2.97-1) ...
Setting up mysql-server (8.0.25-0ubuntu0.20.04.1) ...
Setting up libcgi-fast-perl (1:2.15-1) ...
Processing triggers for systemd (245.4-4ubuntu3.2) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9) ...

インストールできたっぽい?バージョン確認してみよう。

$ mysql --version
mysql  Ver 8.0.25-0ubuntu0.20.04.1 for Linux on x86_64 ((Ubuntu))

やればできるやん!

ユーザホームディレクトリの設定

ホームは/var/lib/mysqlに設定する。

$ sudo usermod -d /var/lib/mysql mysql

/etc/passwdをcatで確認すると、以下記述があるから成功してるっぽい。

mysql:x:112:119:MySQL Server,,,:/var/lib/mysql:/bin/false

起動

以下コマンドを実行するだけ。

$ sudo service mysql start
* Starting MySQL database server mysqld

ここで、普通の感覚では起動しているserviceの一覧が欲しくなるはず。
もちろんそんなコマンドは用意されている。

ステータス 意味
[ + ] 稼働中
[ - ] 停止中
[ ? ] 不明
$ service --status-all
 [ - ]  apparmor
 [ ? ]  apport
 [ - ]  atd
 [ - ]  console-setup.sh
 [ - ]  cron
 [ ? ]  cryptdisks
 [ ? ]  cryptdisks-early
 [ - ]  dbus
 [ ? ]  hwclock.sh
 [ + ]  irqbalance
 [ - ]  iscsid
 [ - ]  keyboard-setup.sh
 [ ? ]  kmod
 [ - ]  lvm2
 [ - ]  lvm2-lvmpolld
 [ - ]  multipath-tools
 [ - ]  mysql
 [ + ]  open-iscsi
 [ - ]  open-vm-tools
 [ ? ]  plymouth
 [ ? ]  plymouth-log
 [ - ]  procps
 [ - ]  rsync
 [ - ]  rsyslog
 [ - ]  screen-cleanup
 [ - ]  ssh
 [ + ]  udev
 [ - ]  ufw
 [ - ]  unattended-upgrades
 [ - ]  uuidd
 [ - ]  x11-common

は?稼働してないじゃん?よくわからんからとりあえず進める。

rootパスワードの設定

Ubuntuの初期設定では、rootユーザ認証方式がauth_socketなのでmysql_native_passwordに変更する。 初期設定のままでは、root以外がrootとしてログインできないらしい。

$ sudo mysql
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;

パスワードの設定をする。パスワードを強化するか聞かれるが、ローカル環境なのでしない(n)。他は全部(y)。

$ sudo mysql_secure_installation
...(略)...
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!

rootで接続

以下コマンド後にパスワードを聞かれるため、先ほど設定したものを入力する。

$ mysql -u root -p

稼働していない問題はもう疲れたので後日...