リポジトリの追加
MySQLはyumコマンドでインストールします。
CentOS 7では公式のyumリポジトリにMySQLがないので、まずはMySQLの公式が用意しているリポジトリを追加します。
※https://dev.mysql.com/downloads/repo/yum/
rpmファイルをインストール
公式が用意しているrpmファイルをインストールすれば、yumのリポジトリもいい感じに追加してくれるみたいです。yumコマンドの localinstall
オプションを使うことでリモートにあるrpmファイルをインストールすることができます。
rpmコマンドを使ってのインストールも可能ですが、依存関係とかを考慮したインストールまではしてくれないので、yumでインストールしましょう。
[vagrant@localhost ~]$ sudo yum localinstall http://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Loaded plugins: fastestmirror mysql80-community-release-el7-3.noarch.rpm | 25 kB 00:00:00 Examining /var/tmp/yum-root-nv4FuK/mysql80-community-release-el7-3.noarch.rpm: mysql80-community-release-el7-3.noarch Marking /var/tmp/yum-root-nv4FuK/mysql80-community-release-el7-3.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mysql80-community-release.noarch 0:el7-3 will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================== Package Arch Version Repository Size =============================================================================================== Installing: mysql80-community-release noarch el7-3 /mysql80-community-release-el7-3.noarch 31 k Transaction Summary =============================================================================================== Install 1 Package Total size: 31 k Installed size: 31 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mysql80-community-release-el7-3.noarch 1/1 Verifying : mysql80-community-release-el7-3.noarch 1/1 Installed: mysql80-community-release.noarch 0:el7-3 Complete!
上のURLはリモートにあるrpmファイルをインストールするためのもので、公式サイトの、下の画像の赤枠部分を、http://dev.mysql.com/get/
の後に付け加えて完成です。
(使用するOSに合わせたものを選択)
リポジトリの確認
/etc/yum.repos.d
配下に、 mysql-community-source.repo
とmysql-community.repo
というリポジトリが作成されてるはずです。
[vagrant@localhost ~]$ cd /etc/yum.repos.d/ [vagrant@localhost yum.repos.d]$ ls -l | grep mysql -rw-r--r--. 1 root root 2108 4月 24 17:35 mysql-community-source.repo -rw-r--r--. 1 root root 2076 4月 24 17:35 mysql-community.repo
このリポジトリが作成されて入れば、リポジトリの追加の確認は完了です。
yumインストール
まずは、リポジトリの反映の確認。
[vagrant@localhost ~]$ sudo yum info mysql-community-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.fairway.ne.jp * extras: mirror.fairway.ne.jp * updates: ftp.yz.yamagata-u.ac.jp base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 mysql-connectors-community | 2.5 kB 00:00:00 mysql-tools-community | 2.5 kB 00:00:00 mysql80-community | 2.5 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/4): mysql-connectors-community/x86_64/primary_db | 41 kB 00:00:01 (2/4): mysql-tools-community/x86_64/primary_db | 58 kB 00:00:02 (3/4): mysql80-community/x86_64/primary_db | 70 kB 00:00:03 (4/4): extras/7/x86_64/primary_db | 205 kB 00:00:05 Available Packages Name : mysql-community-server Arch : x86_64 Version : 8.0.16 Release : 2.el7 Size : 403 M Repo : mysql80-community/x86_64 Summary : A very fast and reliable SQL database server URL : http://www.mysql.com/ License : Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. : Under GPLv2 license as shown in the Description field. Description : The MySQL(TM) software delivers a very fast, multi-threaded, multi-user, (以下省略)
こんな感じでリポジトリ情報が出てくれていれば、確認OK
あとは、インストールなのですが、今回は5.7をインストールしたいので、変更します。
まずは確認。
[vagrant@localhost ~]$ yum repolist all | grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 108 mysql-connectors-community-source MySQL Connectors Community - disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 90 mysql-tools-community-source MySQL Tools Community - Sourc disabled mysql-tools-preview/x86_64 MySQL Tools Preview disabled mysql-tools-preview-source MySQL Tools Preview - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - disabled mysql56-community/x86_64 MySQL 5.6 Community Server disabled mysql56-community-source MySQL 5.6 Community Server - disabled mysql57-community/x86_64 MySQL 5.7 Community Server disabled mysql57-community-source MySQL 5.7 Community Server - disabled mysql80-community/x86_64 MySQL 8.0 Community Server enabled: 113 mysql80-community-source MySQL 8.0 Community Server - disabled
8.0を無効にします。
[vagrant@localhost ~]$ sudo yum-config-manager --disable mysql80-community Loaded plugins: fastestmirror =================================== repo: mysql80-community =================================== [mysql80-community] async = True bandwidth = 0 base_persistdir = /var/lib/yum/repos/x86_64/7 baseurl = http://repo.mysql.com/yum/mysql-8.0-community/el/7/x86_64/ cache = 0 (途中省略) sslcacert = sslclientcert = sslclientkey = sslverify = True throttle = 0 timeout = 30.0 ui_id = mysql80-community/x86_64 ui_repoid_vars = releasever, basearch username =
引き続き5.7を有効にします。
[vagrant@localhost ~]$ sudo yum-config-manager --enable mysql57-community Loaded plugins: fastestmirror =================================== repo: mysql57-community =================================== [mysql57-community] async = True bandwidth = 0 base_persistdir = /var/lib/yum/repos/x86_64/7 baseurl = http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/ cache = 0 (途中省略) sslcacert = sslclientcert = sslclientkey = sslverify = True throttle = 0 timeout = 30.0 ui_id = mysql57-community/x86_64 ui_repoid_vars = releasever, basearch username =
再度確認。
[vagrant@localhost ~]$ yum repolist all | grep mysql mysql-cluster-7.5-community/x86_64 MySQL Cluster 7.5 Community disabled mysql-cluster-7.5-community-source MySQL Cluster 7.5 Community - disabled mysql-cluster-7.6-community/x86_64 MySQL Cluster 7.6 Community disabled mysql-cluster-7.6-community-source MySQL Cluster 7.6 Community - disabled mysql-cluster-8.0-community/x86_64 MySQL Cluster 8.0 Community disabled mysql-cluster-8.0-community-source MySQL Cluster 8.0 Community - disabled mysql-connectors-community/x86_64 MySQL Connectors Community enabled: 108 mysql-connectors-community-source MySQL Connectors Community - disabled mysql-tools-community/x86_64 MySQL Tools Community enabled: 90 mysql-tools-community-source MySQL Tools Community - Sourc disabled mysql-tools-preview/x86_64 MySQL Tools Preview disabled mysql-tools-preview-source MySQL Tools Preview - Source disabled mysql55-community/x86_64 MySQL 5.5 Community Server disabled mysql55-community-source MySQL 5.5 Community Server - disabled mysql56-community/x86_64 MySQL 5.6 Community Server disabled mysql56-community-source MySQL 5.6 Community Server - disabled mysql57-community/x86_64 MySQL 5.7 Community Server enabled: 347 mysql57-community-source MySQL 5.7 Community Server - disabled mysql80-community/x86_64 MySQL 8.0 Community Server disabled mysql80-community-source MySQL 8.0 Community Server - disabled
これからインストールです。
[vagrant@localhost ~]$ sudo yum install -y mysql-community-server Loaded plugins: fastestmirror Loading mirror speeds from cached hostfile * base: mirror.fairway.ne.jp * extras: mirror.fairway.ne.jp * updates: ftp.yz.yamagata-u.ac.jp mysql-connectors-community | 2.5 kB 00:00:00 mysql-tools-community | 2.5 kB 00:00:00 mysql57-community | 2.5 kB 00:00:00 mysql57-community/x86_64/primary_db | 177 kB 00:00:00 Resolving Dependencies (途中省略) Installed: mysql-community-libs.x86_64 0:5.7.26-1.el7 mysql-community-libs-compat.x86_64 0:5.7.26-1.el7 mysql-community-server.x86_64 0:5.7.26-1.el7 Dependency Installed: libaio.x86_64 0:0.3.109-13.el7 mysql-community-client.x86_64 0:5.7.26-1.el7 mysql-community-common.x86_64 0:5.7.26-1.el7 net-tools.x86_64 0:2.0-0.24.20131004git.el7 Replaced: mariadb-libs.x86_64 1:5.5.60-1.el7_5 Complete!
MySQLのデーモンの起動と停止
確認
しっかりMySQLが入ってるかの確認
[vagrant@localhost ~]$ mysqld --version mysqld Ver 5.7.26 for Linux on x86_64 (MySQL Community Server (GPL))
起動
[vagrant@localhost ~]$ sudo systemctl start mysqld
起動の確認
[vagrant@localhost ~]$ systemctl status mysqld ● mysqld.service - MySQL Server Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled) Active: active (running) since 木 2019-06-20 10:39:38 UTC; 19s ago Docs: man:mysqld(8) http://dev.mysql.com/doc/refman/en/using-systemd.html Process: 11726 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid $MYSQLD_OPTS (code=exited, status=0/SUCCESS) Process: 11653 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS) Main PID: 11729 (mysqld) CGroup: /system.slice/mysqld.service └─11729 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
Active: active (running)
が表示されていればOKです。
停止
[vagrant@localhost ~]$ sudo systemctl stop mysqld
自動起動の設定
これは、CentOSの起動・再起動の際に一緒にmysqldも起動するようにする設定です。 以下のコマンドでOKです。
[vagrant@localhost ~]$ sudo systemctl enable mysqld
MySQLの初期設定
rootユーザーでログイン
MySQLは初期状態だと、ユーザーがrootしかいないので、一旦rootで入らなくてはならないです。 しかし、
[root@localhost vagrant]# mysql -u root ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
このように、パスワードなしでアクセスしようとすると怒られます。
※ここらはrootユーザへ変更しました。
初期パスワードの確認
当然、rootでアクセスするにはパスワードが必要なのですが、初期パスワードが何かわからないので、確認する必要があります。
初期パスワードは、mysqlのログの中で確認できます。ログの中身をroot
でgrepした時の一番最初の行にちらっと記載されています。ログファイルは /var/log/mysqld.log
です。
[root@localhost vagrant]# cat /var/log/mysqld.log | grep root 2019-06-20T10:39:35.392305Z 1 [Note] A temporary password is generated for root@localhost: [ここにパスワードの文字が記載されているはず] [root@localhost vagrant]# mysql -u root -p Enter password: [上記のパスワードでログイン] Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.26 (以下省略)
パスワードの変更
rootでログインできたら、自分の分かりやすいパスワードに変更します。
変更の仕方は、以下です。
※8文字以上かつ英大文字・小文字・数字・記号を含めないとエラーで返されます。
mysql> set password for root@localhost=password('新パスワード'); Query OK, 0 rows affected, 1 warning (0.00 sec)
これで変更完了です。
文字コードの確認と変更
設定されている文字コードの確認は以下の通りです。
mysql> show variables like "chara%"; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | latin1 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | latin1 | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec)
ちなみに上記の内容は、
character_set_client : クライアント側で発行したsql文はこの文字コードになる character_set_connection : クライアントから受け取った文字をこの文字コードへ変換する character_set_database : 現在参照しているDBの文字コード character_set_results : クライアントへ送信する検索結果はこの文字コードになる character_set_server : DB作成時のデフォルトの文字コード character_set_system : システムの使用する文字セットで常にutf8が使用されている
これを全部UTF-8にします。
mysqlの設定ファイルは /etc/my.cnf
です。
一回mysqlからログアウトして、設定しましょう。
[root@localhost vagrant]# vi /etc/my.cnf //以下をファイルの末尾に追記 character_set_server=utf8 skip-character-set-client-handshake [root@localhost vagrant]# systemctl restart mysqld
変更を確認します。
mysql> show variables like "chara%"; +--------------------------+----------------------------+ | 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)
全部UTF-8になっています。
これで初期設定終わり