1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS7にMySQL5.7を構築する方法

Last updated at Posted at 2017-01-24

インストール

  • MariaDBライブラリを削除。

    terminal
     yum remove mariadb-libs
    
  • yumレポジトリ追加

    terminal
     rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
     sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/mysql-community.repo
     sed -i -e "s/enabled *= *1/enabled=0/g" /etc/yum.repos.d/mysql-community-source.repo
    
  • MySQLインストール

    terminal
     yum -y install mysql-community-server --enablerepo=mysql57-community
    

設定&起動

  • 一度サービスを起動し、rootパスワードを取得する

    terminal
     service mysqld start
    
  • /var/log/mysqld.logに書かれた初期パスワードを取得

    terminal
     cat /var/log/mysqld.log | grep 'temporary password' | awk -F ': ' '{print $NF}'
    
  • フォルダ作成

    terminal
     mkdir -p /var/log/mysqld && chown -R mysql. /var/log/mysqld
     mkdir -p /etc/mysql      && chown -R mysql. /etc/mysql
    
  • cnfファイルを配置

    terminal
     cat << \EOS > /etc/mysql/my.cnf
     [mysqld]
     
     # 文字コード設定
     character-set-server=utf8mb4
     skip-character-set-client-handshake
     
     # リンクテーブル作成を許容する
     symbolic-links=0
     
     # ログ出力設定
     slow_query_log=0
     slow_query_log_file=/var/log/mysql/slow.log
     long_query_time=1
    
     # パスワードポリシー設定
     validate_password_policy=LOW
     
     # メモリサイズ拡張
     max_allowed_packet=128MB
     
     # テーブル、カラム名を小文字に統一
     lower_case_table_names=1
     
     # リンクテーブル有効化
     federated = 1
     
     # InnoDB設定
     innodb_buffer_pool_size = 512M
     #innodb_log_file_size = 256M
    
     # TIMESTAMPデフォルト値
     explicit_defaults_for_timestamp = true
     
     # indexサイズ拡張対応
     innodb_large_prefix=1
     innodb_file_format=Barracuda
     innodb_file_format_max=Barracuda
     EOS
    
  • サービス再起動

    terminal
     service mysqld restart
    
  • 接続確認

    terminal
     mysql -u root -p
     
     Enter password: [初期パスワード]
     Welcome to the MySQL monitor.  Commands end with ; or \g.
     Your MySQL connection id is 8
     Server version: 5.7.17 MySQL Community Server (GPL)
     
     Copyright (c) 2000, 2016, 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>
    
    terminal
     # MariaDBコンソールを抜ける
     mysql> exit
    

セキュリティ設定

  • mysql_secure_installationを使う場合。

    terminal
     mysql_secure_installation
    
  • 同等のことを、Chef/Ansibleなどでやる場合は以下を参考に。。。

    この方法、5.7ではデフォルトパスワードではインタラクティブに
    パスワードを受け付けてもらえず、使えないみたいだ。

    terminal
     mysql -u root -p << \EOS
     /* rootユーザー作成 & パスワード設定 */
     GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost'	IDENTIFIED BY '[password]' WITH GRANT OPTION;
     GRANT ALL PRIVILEGES ON *.* TO 'root'@'%'			IDENTIFIED BY '[password]' WITH GRANT OPTION;
     /* anonymasユーザー削除 */
     DELETE FROM mysql.user WHERE User = '';
     /* testデータベース削除 */
     DROP DATABASE IF EXISTS test;
     /* 権限反映 */
     FLUSH PRIVILEGES;
     EOS
    

完了

  • 以上
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?