LoginSignup
0
0

More than 1 year has passed since last update.

Raspberry PiでMySQLの保存先を変更する方法

Posted at

概要

Raspberry PiでMySQLを使用する際にデータベースの保存先を外付けSSDにしたかったが、configのdatadir=を変更しただけではMySQLが起動しなくなった。

$ systemctl status mysql.service
mysql.service: Failed with result 'exit-code'.
Failed to start MySQL Community Server.

$ journalctl -xe
systemd[1]: mysql.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- The unit mysql.service has entered the 'failed' state with result 'exit-code'.
systemd[1]: Failed to start MySQL Community Server.
-- Subject: A start job for unit mysql.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- A start job for unit mysql.service has finished with a failure.

そんなエラーログを吐かれてもさっぱりである。

どうやら追加設定が必要な模様。

実行環境

  • マシン:Raspberry Pi B+
  • OS:Ubuntu Server 20.04.4
  • MySQL:Ver 8.0.28-0

外付けSSDのフォーマット・マウント

パーティション作成
$ sudo parted /dev/sda
(parted) mklabel gpt
(parted) mkpart
Partition name?  []?
File system type?  [ext2]? ext4
Start?0%
End? 100%
Warning: You requested a partition from 0.00B to 128GB (sectors 0..250069679).
The closest location we can manage is 17.4kB to 1048kB (sectors 34..2047).
Is this still acceptable to you?
Yes/No?Yes
(parted)q

フォーマット
$ sudo mkfs.ext4 /dev/sdb1

マウント
$ sudo mkdir /mnt/mysql

$ sudo nano /etc/fstab
~~追記~~
/dev/sda2               /mnt/mysql      ext4    nofail          0       0

$ sudo mount -all
$ sudo chown mysql:mysql /mnt/mysql -R

MySQLの設定

$ sudo systemctl stop  mysql.service

$ sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
~~[mysqld]以下 追記、又は編集~~
datadir = /mnt/mysql

AppArmorの設定

$ sudo nano /etc/apparmor.d/tunables/alias
~~追記~~
alias /var/lib/mysql/ -> /mnt/mysql/,

$ sudo systemctl restart apparmor.service

MySQLのデータを移動・再起動

スラッシュの付け方を間違えると、階層が変わるので注意

$ sudo rsync -avuz /var/lib/mysql.bak/ /mnt/mysql

$ sudo systemctl start mysql.service

参考文献

straightweeds.hatenablog.com 様
RaspberryPiにmysqlをインストール

DigitalOcean Community 様
How To Move a MySQL Data Directory to a New Location on Ubuntu 16.04

0
0
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
0
0