linux:centos:centos7_mysql_install

CentOS 7 に MySQL 5.7 をインストールする

$ sudo rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
$ sudo yum install -y mysql-community-server
$ sudo cat /var/log/mysqld.log | grep 'temporary password'
2018-03-01T15:45:33.932304Z 1 [Note] A temporary password is generated for root@localhost: 2rGWtisl*JYC

自動起動設定はされているのでしなくて良い。

以下のコマンドを実行して MySQL の root パスワードを変更する。

$ mysql_secure_installation
~省略~
Enter password for user root: ******** <- 初期パスワードを入力
~省略~
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y を入力
New password: ******** <- root のパスワードを入力
Re-enter new password: ******** <- root の確認パスワードを入力
~省略~
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y を入力
Success.
~省略~
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y を入力
Success.
~省略~
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <- Enter を入力

 ... skipping.
~省略~
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y を入力
Success.

root にてログインできることを確認する。

$ mysql -u root -p
Enter password: ******** <- root ユーザーのパスワードを入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.21 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

UTF-8を標準とするように設定する。

$ sudo vi /etc/my.cnf
[mysqld]
character-set-server=utf8

設定を反映するために MySQL を再起動する。

$ sudo systemctl restart mysqld

UTF-8 設定確認

$ mysql -u root -p
mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.01 sec)

MySQL 5.6まで

mysql> UPDATE mysql.user SET Password=PASSWORD('パスワード') WHERE User='root';
mysql> FLUSH PRIVILEGES;

MySQL 5.7以降

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'パスワード';
mysql> GRANT ALL ON *.* TO username IDENTIFIED BY 'password';
$ sudo vi /etc/my.cnf
[mysqld]
sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
  • linux/centos/centos7_mysql_install.txt
  • 最終更新: 2024/02/04 13:42
  • by ともやん