さくらVPSにMariaDBをインストールする手順やエラーへの対処

coding

Djangoアプリケーションの開発で本番環境にMariaDBをインストールした時の覚書です。

途中、詰まった部分もありましたが手順を紹介していきます。

さくらVPSにMariaDBをインストールする

前提条件として、CentOS7で構築しています。

ローカルからさくらVPSにログインし下記コマンドを実行。

sudo yum -y install mariadb mariadb-server

インストールが開始し、終了したら下記コマンド。設定ファイルの編集となります。

sudo vi /etc/my.cnf

mysqldの箇所に下記を追加。

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
character-set-server=utf8←これを追加

Macはctrl + c入力し、:wqで保存→終了できます。

MariaDBを起動する

sudo systemctl start mariadb

ここで以下のエラーが発生しました。

Job for mariadb.service failed because the control process exited with error code. See "systemctl status mariadb.service" and "journalctl -xe" for details.

何かをミスしてる。よくよく見ると先ほど追加したcharacter-set-server=utf8の箇所で頭のcが抜けていました。上記エラーが発生した人はまずはスペルミスを確認してみてください。

sudo systemctl enable mariadb

自動起動も有効にしておきます。

MariaDBの初期設定をする

最後にMariaDBの初期設定をしていきます。

sudo mysql_secure_installation

すると以下のメッセージが出てきてrootユーザーのパスワード入力の項目があります。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

あれ、パスワードなんて設定した覚えないぞ・・・

と思って適当に入力したら当たり前ですがエラーが返ってきました。

よく説明を読んでいませんでした。

If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

ざっくり訳すると「インストールしたばかりなら、まだrootパスワードを設定していなから空で入力してね。」

というわけで、パスワードは何も入力せず空の状態でエンターキーを押します。

この後、色々とメッセージが出てきますがy(yes)を入力していけば初期設定は完了していきます。この時にrootパスワードも設定できます。

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

無事にMariaDBをインストールすることができました。

最後にポートの開放と反映を行います。

$ sudo firewall-cmd  --add-service=mysql --permanent
[sudo] user のパスワード:
success
$ sudo firewall-cmd --reload
success

DBの用意をする

無事にインストールもしました。最後にアプリケーションで使うためのDBやユーザーを用意します。

ちょっと長いですが、要はローカルで開発しているものと同じ環境をリモートに構築します。

まずは ログインします。

mysql -u root -p

-pオプションを付けることでパスワードの入力を求められます。先ほどの初期設定の時にrootユーザーのパスワードを設定しなかった場合は必要ありません。

 

データベースを作成します。

create database your_db_name;

your_db_nameの箇所は好きなDB名を入力してください。

 

ユーザーを作成します。ここではパスワード付きのユーザーを作成しています。

create user 'your_name'@'host_name' identified by 'your_password';

your_nameやyour_passwordの箇所に好きなものを入れます。

host_nameの箇所はlocalhostがほとんどだと思います。

 

create userでユーザーを作成した場合、初期設定では権限が付与されていません。最後にgrant文を使って作成したユーザーがどのデータベースに対して権限を持つのかを設定します。

grant all on your_db.* to 'user_name'@'host_name' identified by 'your_password';

grant以下の流れはこんな感じ。

grant [権限] on [対象DB].[対象テーブル] to ‘ユーザー名’@’ホスト名’ identified by ‘パスワード’;

全てを許可するのでgrant allとしました。また、対象テーブルは全てなので*で全てを対象にします。

↑これでエラーが出る場合は、こちらで。

grant all on your_db.* to ‘user_name’@’host_name’;

 

最後にflush~~を実行することで権限の変更をデータベースに反映します。

flush privileges;

ちなみに、privilegesには特権・権限・権利などの意味があります。

CentOSでmysqlclientをインストールする

Djangoでmysqlを使用するには、mysqlclientをインストールする必要があります。

が、ローカルでは問題なくインストールできても、さくらVPSのCentOS上でなかなかインストール成功せず。

yum install python3-devel mysql-devel
pip install mysqlclient

色々とパッケージのインストールが必要なようで、上記でインストール可能になります。



カテゴリー