MySQL8.0のインストール【AlmaLinux 8】
MySQL8.0をインストールします。
1. インストール
# dnf --disablerepo=appstream --enablerepo=mysql80-community install mysql-community-server
2. MySQLの設定
(1) DBの初期化
DBの初期化を行います。
# mysqld --initialize --user=mysql --datadir=/var/lib/mysql
※5.6までは「mysql_install_db」コマンドで行いましたが、5.7からは変更になりました。
このコマンドで初期化を行うと、初期ユーザとして「root@localhost」が作成されます。
また、初期パスワードが「/var/log/mysqld.log」に書き込まれます。
ちなみに「mysqld --initialize-insecure」とすると、パスワードは空となります。
(2) /etc/my.cnfの設定
MySQLの設定ファイルは、「/etc/my.cnf」ですが、カスタム設定は「/etc/my.cnf.d」フォルダに「*.cnf」ファイルを作成することで設定を追加できます。
# vi /etc/my.cnf.d/server.cnf
[mysqld]
innodb_data_home_dir = /var/lib/mysql/
innodb_log_group_home_dir = /var/lib/mysql/
character_set_server=utf8mb4
collation-server=utf8mb4_0900_ai_ci
expire_logs_days = 30
# Error log
log_error="mysqld.log"
log_warnings=1
# Query log
general_log = 0
general_log_file="sql.log"
# Slow Query log
slow_query_log = 0
slow_query_log_file="slow_query.log"
log_queries_not_using_indexes
log_slow_admin_statements
long_query_time=5
[client]
default-character-set=utf8mb4
general_log:「0」:ログを出力しない、「1」:ログを出力する
slow_query_log:「0」:ログを出力しない、「1」:ログを出力する
※「Query log」や「Slow Query log」を有効にすると、ファイルサイズがかなり大きくなる可能性がありますので、必要時のみ有効にして下さい。
■collation-server
utf8mb4_0900_ai_ci |
(MySQL 8.0のデフォルト)。アクセントの違い(「は」と「ぱ」など)および大文字・小文字を区別しない。 |
utf8mb4_bin |
大文字・小文字を含めて、すべて区別。 |
utf8mb4_general_ci |
大文字・小文字は区別しない。他は全て区別。 |
utf8mb4_unicode_ci |
大文字・小文字および全角・半角を区別しない。 |
(3) MySQLの起動
設定が完了したら、MySQLのサービスを起動します。
# systemctl start mysqld.service
(4) 自動起動設定
サーバを再起動した場合に自動的にサービスが起動するようにします。
# systemctl enable mysqld.service
(5) mysql_secure_installationの実行
MySQLのセキュリティを向上させるために「mysql_secure_installation」を実行します。
# /usr/bin/mysql_secure_installation
MySQLのセキュリティを向上させるために「mysql_secure_installation」を実行します。
# /usr/bin/mysql_secure_installation
■現在のrootパスワードの入力
現在のrootのパスワードを入力します。
Enter current password for root (enter for none):
インストール時に「/var/log/mysqld.log」に記載されている初期パスワードを入力します。
■新しいパスワードの登録
初期パスワードから新しいパスワードに変更します。
The existing password for the user account root has expired. Please set a new password.
New password:
Re-enter new password:
「New password:」と「Re-enter new password:」に任意のパスワードを入力します。
■VALIDATE 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:
プラグインをインストールしないので「No」を入力します。
■rootパスワードの変更
rootパスワードを変更するかどうかの質問です。
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
ここでは変更しないので「N」を入力します。
■匿名アカウントの削除
パスワードのない匿名アカウントを削除するかどうかの質問です。
Remove anonymous users? (Press y|Y for Yes, any other key for No) :
削除するので「Y」を入力します。
■rootアカウントのリモートログインの不許可
Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
rootアカウントのリモートログインは許可しないので、「Y」と入力します。
■testデータベースの削除
どのユーザからでもアクセスできるtestデータベースを削除するかどうかの質問です。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
削除するので「Y」を入力します。
■特権テーブルのリロード
すぐに特権テーブルをリロードして反映させるかどうかの質問です。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
すぐにリロードするので「Y」を入力します。
3. rootパスワードの変更
インストール直後は、「root」ユーザにテンポラリーパスワードが設定されているので、パスワードを変更します。
※前述の「mysql_secure_installation」を実行した場合には不要です。
MySQLにログインします。
# mysql -u root -p
Enter password:
パスワードを変更します。
mysql> SET PASSWORD FOR root@localhost='xxxxxx';
mysql> exit;
※従来の「SET PASSWORD FOR root@localhost=PASSWORD('xxxxxx');」だとSyntaxエラーが発生します。
変更したパスワードでログインできることを確認します。
# mysql -u root -p
Enter password:
ログインできればOKです。
4. その他のコマンド
(1) ユーザ作成
CREATE USER '(ユーザID)'@'localhost' IDENTIFIED WITH mysql_native_password BY '(パスワード)';
GRANT ALL PRIVILEGES ON (テーブル名).* TO '(ユーザID)'@'localhost';
※※【注意事項】※※
「my.cnf」で「skip-name-resolve」を指定している場合には、ホストの名前解決ができないため「localhost」ではエラーとなる。その場合には「127.0.0.1」と記述すること。
5. cronでの実行の場合
定期バックアップなどでコマンドラインからMySQLを実行する場合に、パスワードをコマンドラインに含めると警告が発生しますし、またセキュリティ上問題があります。
そこで、設定ファイルを作成して、コマンドラインから読込みます。
(1) 設定ファイルの作成
「/etc/.mysql_config」という設定ファイルを作成します。
内容は以下の通りです。
[client]
user = root
password = (パスワード)
host = localhost
「root」以外読めないようにします。
# chmod 400 /root/.mysql_config
(2) コマンドラインでの実行
「--defaults-extra-file」オプションで設定ファイルを読み込みます。
【例】
for DBNAME in `ls -p /var/lib/mysql | grep / | tr -d /`
do
# mysqldump
if [ "$DBNAME" = '#innodb_temp' ]
then
continue;
fi
if [ "$DBNAME" = 'performance_schema' ]
then
/usr/bin/mysqldump --defaults-extra-file=/root/.mysql_config --skip-lock-tables --skip-extended-insert $DBNAME > $DBNAME.dump
else
/usr/bin/mysqldump --defaults-extra-file=/root/.mysql_config --events --skip-lock-tables --skip-extended-insert $DBNAME > $DBNAME.dump
fi
done
6. MySQLのコマンド
(1) mysqldump
■データベースを出力
mysqldump --defaults-extra-file=/root/.mysql_config -c -skip-extended-insert (DB名) > (出力ファイル名)
■テーブルを指定して出力
mysqldump --defaults-extra-file=/root/.mysql_config -c -skip-extended-insert (DB名) (テーブル名) > (出力ファイル名)
(2) mysql
■ダンプファイルをインポート
mysql --defaults-extra-file=/root/.mysql_config (DB名) < (ダンプファイル名)
最終更新:2021年07月03日 09:45