今回はCentOS 7上にMySQLをインストールする方法を紹介します。MySQLは公式でyumレポジトリが公開されていますでyumでのインストール方法と基本的な設定方法を説明します。
この記事の環境
OS、ミドルウェア | バージョン等 |
---|---|
CentOS | 7.2 |
MySQL Community | 5.7 |
MySQLデータディレクトリ | /data/mysql |
インストールおよび初期設定
インストール
まずはMySQLのyumレポジトリをインストールします。MySQLのページからyumレポジトリのRPMファイルのURLを取得してインストールを行います。。
yumレポジトリのRPMファイルのURLは以下の方法で取得できます。
-
MySQLのページ上部にあるダウンロードリンクをクリックします。
-
ダウンロードページ上部にあるYum Repositoryのリンクをクリックします。
-
MySQL公式サイト(英語)のYum Repositoryダウンロードページへ遷移しますので、画面中部にある環境に合ったバージョン(今回はRed Hat Enterprise Linux 7 / Oracle Linux 7)のDownloadボタンをクリックします。
-
ログインまたはサインインを求められますが、必要がなければ下部にある「No thanks, just start my download.」というリンクのURLをコピーします。
コピーしたURLを使用してyumレポジトリをインストールします。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
$ sudo yum -y install http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm 読み込んだプラグイン:fastestmirror /var/tmp/yum-root-nmzbq_/mysql57-community-release-el7-9.noarch.rpm を調べています: mysql57-community-release-el7-9.noarch /var/tmp/yum-root-nmzbq_/mysql57-community-release-el7-9.noarch.rpm をインストール済みとして設定しています 依存性の解決をしています --> トランザクションの確認を実行しています。 ---> パッケージ mysql57-community-release.noarch 0:el7-9 を インストール --> 依存性解決を終了しました。 依存性を解決しました ================================================================================ Package アーキテクチャー バージョン リポジトリー 容量 ================================================================================ インストール中: mysql57-community-release noarch el7-9 /mysql57-community-release-el7-9.noarch 8.6 k トランザクションの要約 ================================================================================ インストール 1 パッケージ 合計容量: 8.6 k インストール容量: 8.6 k Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction インストール中 : mysql57-community-release-el7-9.noarch 1/1 検証中 : mysql57-community-release-el7-9.noarch 1/1 インストール: mysql57-community-release.noarch 0:el7-9 完了しました! $ |
yumレポジトリがインストールできたらMySQL本体のインストールを行います。
今回はライセンスの問題が少ないCommunityバージョンでインストールを行います。他のバージョンでも同様の手順でインストールは可能です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
$ sudo yum -y install mysql-community-devel mysql-community-server み込んだプラグイン:fastestmirror Loading mirror speeds from cached hostfile * base: ftp.iij.ad.jp * extras: ftp.iij.ad.jp * updates: ftp.iij.ad.jp 依存性の解決をしています --> トランザクションの確認を実行しています。 ---> パッケージ mysql-community-devel.x86_64 0:5.7.17-1.el7 を インストール --> 依存性の処理をしています: mysql-community-libs(x86-64) >= 5.7.9 のパッケージ: mysql-community-devel-5.7.17-1.el7.x86_64 --> 依存性の処理をしています: libmysqlclient.so.20()(64bit) のパッケージ: mysql-community-devel-5.7.17-1.el7.x86_64 ---> パッケージ mysql-community-server.x86_64 0:5.7.17-1.el7 を インストール --> 依存性の処理をしています: mysql-community-common(x86-64) = 5.7.17-1.el7 のパッケージ: mysql-community-server-5.7.17-1.el7.x86_64 --> 依存性の処理をしています: mysql-community-client(x86-64) >= 5.7.9 のパッケージ: mysql-community-server-5.7.17-1.el7.x86_64 --> 依存性の処理をしています: net-tools のパッケージ: mysql-community-server-5.7.17-1.el7.x86_64 --> トランザクションの確認を実行しています。 ---> パッケージ mariadb-libs.x86_64 1:5.5.52-1.el7 を 不要 --> 依存性の処理をしています: libmysqlclient.so.18()(64bit) のパッケージ: 2:postfix-2.10.1-6.el7.x86_64 --> 依存性の処理をしています: libmysqlclient.so.18(libmysqlclient_18)(64bit) のパッケージ: 2:postfix-2.10.1-6.el7.x86_64 ---> パッケージ mysql-community-client.x86_64 0:5.7.17-1.el7 を インストール ---> パッケージ mysql-community-common.x86_64 0:5.7.17-1.el7 を インストール ---> パッケージ mysql-community-libs.x86_64 0:5.7.17-1.el7 を 非推奨 ---> パッケージ net-tools.x86_64 0:2.0-0.17.20131004git.el7 を インストール --> トランザクションの確認を実行しています。 ---> パッケージ mysql-community-libs-compat.x86_64 0:5.7.17-1.el7 を 非推奨 --> 依存性解決を終了しました。 依存性を解決しました ================================================================================ Package アーキテクチャー バージョン リポジトリー 容量 ================================================================================ インストール中: mysql-community-devel x86_64 5.7.17-1.el7 mysql57-community 3.6 M mysql-community-libs x86_64 5.7.17-1.el7 mysql57-community 2.1 M mariadb-libs.x86_64 1:5.5.52-1.el7 を入れ替えます mysql-community-libs-compat x86_64 5.7.17-1.el7 mysql57-community 2.0 M mariadb-libs.x86_64 1:5.5.52-1.el7 を入れ替えます mysql-community-server x86_64 5.7.17-1.el7 mysql57-community 162 M 依存性関連でのインストールをします: mysql-community-client x86_64 5.7.17-1.el7 mysql57-community 24 M mysql-community-common x86_64 5.7.17-1.el7 mysql57-community 271 k net-tools x86_64 2.0-0.17.20131004git.el7 base 304 k トランザクションの要約 ================================================================================ インストール 4 パッケージ (+3 個の依存関係のパッケージ) 合計容量: 194 M 総ダウンロード容量: 162 M Downloading packages: Delta RPMs disabled because /usr/bin/applydeltarpm not installed. 警告: /var/cache/yum/x86_64/7/mysql57-community/packages/mysql-community-server-5.7.17-1.el7.x86_64.rpm: ヘッダー V3 DSA/SHA1 Signature、鍵 ID 5072e1f5: NOKEY mysql-community-server-5.7.17-1.el7.x86_64.rpm の公開鍵がインストールされていません file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql から鍵を取得中です。 Importing GPG key 0x5072E1F5: Userid : "MySQL Release Engineering <mysql-build@oss.oracle.com>" Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5 Package : mysql57-community-release-el7-9.noarch (installed) From : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql Running transaction check Running transaction test Transaction test succeeded Running transaction インストール中 : mysql-community-common-5.7.17-1.el7.x86_64 1/8 インストール中 : mysql-community-libs-5.7.17-1.el7.x86_64 2/8 インストール中 : mysql-community-client-5.7.17-1.el7.x86_64 3/8 インストール中 : net-tools-2.0-0.17.20131004git.el7.x86_64 4/8 インストール中 : mysql-community-server-5.7.17-1.el7.x86_64 5/8 インストール中 : mysql-community-libs-compat-5.7.17-1.el7.x86_ 6/8 インストール中 : mysql-community-devel-5.7.17-1.el7.x86_64 7/8 削除中 : 1:mariadb-libs-5.5.52-1.el7.x86_64 8/8 検証中 : mysql-community-common-5.7.17-1.el7.x86_64 1/8 検証中 : mysql-community-server-5.7.17-1.el7.x86_64 2/8 検証中 : net-tools-2.0-0.17.20131004git.el7.x86_64 3/8 検証中 : mysql-community-libs-5.7.17-1.el7.x86_64 4/8 検証中 : mysql-community-libs-compat-5.7.17-1.el7.x86_ 5/8 検証中 : mysql-community-client-5.7.17-1.el7.x86_64 6/8 検証中 : mysql-community-devel-5.7.17-1.el7.x86_64 7/8 検証中 : 1:mariadb-libs-5.5.52-1.el7.x86_64 8/8 インストール: mysql-community-devel.x86_64 0:5.7.17-1.el7 mysql-community-libs.x86_64 0:5.7.17-1.el7 mysql-community-libs-compat.x86_64 0:5.7.17-1.el7 mysql-community-server.x86_64 0:5.7.17-1.el7 依存性関連をインストールしました: mysql-community-client.x86_64 0:5.7.17-1.el7 mysql-community-common.x86_64 0:5.7.17-1.el7 net-tools.x86_64 0:2.0-0.17.20131004git.el7 置換: mariadb-libs.x86_64 1:5.5.52-1.el7 完了しました! $ |
以上でMySQL Community ServerおよびMySQL Community Client のインストールは終了です。
MySQLのデータディレクトリの変更
MySQLのデータベースの保存領域であるデータディレクトリを作成します。保存したい領域にディレクトリを作成し、デフォルトの領域(/var/lib/mysql)にシンボリックリンクを作成するだけの作業になります。
1 2 3 4 5 6 |
$ sudo mkdir /data/mysql $ sudo chown mysql: /data/mysql $ sudo chmod 751 /data/mysql $ sudo rm -rf /var/lib/mysql $ sudo ln -s /data/mysql /var/lib/mysql $ |
MySQLの初期設定
MySQLの初期設定を行う前に、スーパーユーザーのパスワードを確認する必要があります。MySQLでは初回起動時にスーパーユーザーのパスワードがランダムで生成されるようになっています。まずはそのパスワードを確認する必要があります。
パスワードは/var/log/mysqld.logに記載されますので、mysqldを起動してログファイルを確認します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
$ sudo systemctl start mysqld $ cat /var/log/mysqld.log 2016-12-18T13:52:35.824815Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-12-18T13:52:36.670193Z 0 [Warning] InnoDB: New log files created, LSN=45790 2016-12-18T13:52:36.858988Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2016-12-18T13:52:36.926192Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 5bd0gcb3-c509-14e6-b243-08001710aae4. 2016-12-18T13:52:36.938231Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2016-12-18T13:52:36.939060Z 1 [Note] A temporary password is generated for root@localhost: yVrhdOK;N6<C 2016-12-18T13:52:39.762367Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2016-12-18T13:52:39.763267Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.17) starting as process 1342 ... 2016-12-18T13:52:39.765632Z 0 [Note] InnoDB: PUNCH HOLE support available 2016-12-18T13:52:39.765655Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins 2016-12-18T13:52:39.765659Z 0 [Note] InnoDB: Uses event mutexes 2016-12-18T13:52:39.765663Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier 2016-12-18T13:52:39.765667Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3 2016-12-18T13:52:39.765671Z 0 [Note] InnoDB: Using Linux native AIO 2016-12-18T13:52:39.765849Z 0 [Note] InnoDB: Number of pools: 1 2016-12-18T13:52:39.765918Z 0 [Note] InnoDB: Using CPU crc32 instructions 2016-12-18T13:52:39.766778Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2016-12-18T13:52:39.775408Z 0 [Note] InnoDB: Completed initialization of buffer pool 2016-12-18T13:52:39.778993Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority(). 2016-12-18T13:52:39.791729Z 0 [Note] InnoDB: Highest supported file format is Barracuda. 2016-12-18T13:52:39.810333Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables 2016-12-18T13:52:39.810400Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ... 2016-12-18T13:52:39.917626Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB. 2016-12-18T13:52:39.919257Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active. 2016-12-18T13:52:39.919281Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active. 2016-12-18T13:52:39.922093Z 0 [Note] InnoDB: Waiting for purge to start 2016-12-18T13:52:39.972412Z 0 [Note] InnoDB: 5.7.17 started; log sequence number 2534707 2016-12-18T13:52:39.972811Z 0 [Note] Plugin 'FEDERATED' is disabled. 2016-12-18T13:52:39.988093Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them. 2016-12-18T13:52:39.988502Z 0 [Warning] CA certificate ca.pem is self signed. 2016-12-18T13:52:39.990610Z 0 [Note] Server hostname (bind-address): '*'; port: 3306 2016-12-18T13:52:39.990645Z 0 [Note] IPv6 is available. 2016-12-18T13:52:39.990656Z 0 [Note] - '::' resolves to '::'; 2016-12-18T13:52:39.990673Z 0 [Note] Server socket created on IP: '::'. 2016-12-18T13:52:39.990961Z 0 [Note] InnoDB: Loading buffer pool(s) from /data/mysql/ib_buffer_pool 2016-12-18T13:52:39.993498Z 0 [Note] InnoDB: Buffer pool(s) load completed at 161218 22:52:39 2016-12-18T13:52:40.014218Z 0 [Note] Event Scheduler: Loaded 0 events 2016-12-18T13:52:40.014299Z 0 [Note] Executing 'SELECT * FROM INFORMATION_SCHEMA.TABLES;' to get a list of tables using the deprecated partition engine. You may use the startup option '--disable-partition-engine-check' to skip this check. 2016-12-18T13:52:40.014306Z 0 [Note] Beginning of list of non-natively partitioned tables 2016-12-18T13:52:40.025593Z 0 [Note] End of list of non-natively partitioned tables 2016-12-18T13:52:40.025664Z 0 [Note] /usr/sbin/mysqld: ready for connections. Version: '5.7.17' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server (GPL) $ |
A temporary password is generated for root@localhost:
という部分にパスワードが記載されています。
パスワードが確認できたら、そのパスワードを忘れないように保存しておき、初期化用のコマンドを実行します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
$ mysql_secure_installation Securing the MySQL server deployment. Enter password for user root: The 'validate_password' plugin is installed on the server. The subsequent steps will run with the existing configuration of the plugin. Using existing password for root. Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : y New password: Re-enter new password: Estimated strength of the password: 100 Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. 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! $ |
mysql_secure_installation
コマンドを実行すると対話式でいくつかの質問と入力を聞かれます。
- コマンドを実行後パスワードを聞かれますので、先程ログで確認したパスワードを入力します。
- rootユーザーのパスワード変更を行うかどうか確認されますので、[y]を入力します。
- 新しいパスワードおよび確認用のパスワードを入力します。英数字記号の含まれた複雑なパスワードでないとエラーとなります。
- anonymousユーザーの削除するか確認されますので、[y]を入力します。
- rootユーザーへのリモートログインを禁止するか確認されますので、[y]を入力します。
- testデータベースを削除するか確認されますので、[y]を入力します。
- 今すぐprivilegeテーブルを再読込するか確認されますので、[y]を入力します。
次に設定ファイルを変更し、データベースの文字コードをUTF-8に変更します。
設定ファイルは/etc/my.confにあります。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# For advice on how to change settings please see # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html [mysqld] # # Remove leading # and set to the amount of RAM for the most important data # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%. # innodb_buffer_pool_size = 128M # # Remove leading # to turn on a very important data integrity option: logging # changes to the binary log between backups. # log_bin # # Remove leading # to set options mainly useful for reporting servers. # The server defaults are faster for transactions and fast SELECTs. # Adjust sizes as needed, experiment to find the optimal values. # join_buffer_size = 128M # sort_buffer_size = 2M # read_rnd_buffer_size = 2M datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid character-set-server=utf8 skip-character-set-client-handshake [mysql] default-character-set=utf8 |
ユーザー設定
データベースを操作するユーザーを作成します。rootユーザーはスーパーユーザーであり、このユーザーでも操作することについて何の問題もありませんが、逆に何でもできてしまうため誤ってデータやデータベースを削除してしまったり、悪意のあるユーザーに乗っ取られた場合には被害が大きくなってしまいます。よって、スーパーユーザーよりも権限の低いユーザーを使用して操作を行います。
1 2 3 4 |
mysql> CREATE USER '(ユーザー名)'@'localhost' IDENTIFIED BY '(パスワード)'; Query OK, 0 rows affected (0.00 sec) mysql> |
データベースの作成およびユーザーの所有権の紐付け
データベースの初期化と、ユーザーの作成ができたらデータベースを作成します。
データベースの作成
1 2 3 4 5 6 |
mysql> CREATE DATABASE (データベース名); Query OK, 1 row affected (0.00 sec) mysql> use (データベース名); Database changed mysql> |
useを使用することで、データベースを切り替えることができます。
データベースとユーザーの紐付け
データベースが作成できたら、先程作成したユーザーにこのデータベースの操作権限を与えます。今回与える権限は、先程作成したデータベースに対しての全権限を与えます。
1 2 3 4 |
mysql> GRANT ALL PRIVILEGES ON (データベース名).* TO '(ユーザー名)'@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> |
アクセス制限
外部接続制限
ユーザーの作成時およびデータベースとのヒモ付を行う際に、@以降をプライベートIPアドレスにすることで、外部からの接続を許可することができます。
JavaやPHPなどからデータベースへ接続する際にもこの設定がされていないと接続できない場合があります。
作成したユーザーでデータベースを操作する
以上の操作を行うことで、データベースとユーザーの組み合わせで操作することができるようになります。
データベース名 | sample_db |
ユーザー名 | sample |
テーブル名 | tbl_sample |
以下のようなテーブル(sample)を作成します。
カラム名 | 型 | 制約 |
---|---|---|
id | integer | primary key, not null |
name | text | not null |
job | text |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
mysql> CREATE TABLE tbl_sample (id integer primary key, name text not null, job text); Query OK, 0 rows affected (0.01 sec) mysql> desc tbl_sample; +-------+---------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id | int(11) | NO | PRI | NULL | | | name | text | NO | | NULL | | | job | text | YES | | NULL | | +-------+---------+------+-----+---------+-------+ 3 rows in set (0.01 sec) mysql> |
以上で、CentOS 7上にMySQL 5.7のインストールおよび初期設定は完了です。