りんちゃんの日記

日常を書き留めていきます。

mySQLインストール

1. mySQLのインストール

 1.1. yumでインストール
    # yum -y install mysql-server

 1.2. 自動起動設定
    # chkconfig mysqld on

 1.3. 自動起動確認
    # chkconfig --list mysqld

 

 mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 ※3がOKであればOK 


 2. UTF8の設定

 2.1. config編集(赤文字の箇所追加する)
    # vi /etc/my.cnf

  [mysqld]
  datadir=/var/lib/mysql
  socket=/var/lib/mysql/mysql.sock
  user=mysql
  # Disabling symbolic-links is recommended to prevent assorted security risks
  symbolic-links=0

  default-character-set=utf8
  character-set-server=utf8
  skip-character-set-client-handshake

  [client] ※クライアントからの接続の文字コードの設定
  default-character-set=utf8

  [mysqld_safe]
  log-error=/var/log/mysqld.log
  pid-file=/var/run/mysqld/mysqld.pid


 3. サービス起動

 3.1. サービスを起動
    # service mysqld start
         mysqld (pid 4563) を実行中...

    または
    # ps aux|grep mysqld
  返答があれば起動中

 3.2. サービスのステータスを確認

     # service mysqld status
  mysqld (pid 4563) を実行中...


 4. パスワードの設定

  最初と次でEnter
  MySQLのrootパスワードを設定
  <パスワード>=hogehoge

 

 4.1. セキュア設定
    # mysql_secure_installation

  NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
  SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!


  In order to log into MySQL to secure it, we'll need the current
  password for the root user. If you've just installed MySQL, and
  you haven't set the root password yet, the password will be blank,
  so you should just press enter here.

  current password for root (enter for none): <-- [Enter]
  OK, successfully used password, moving on...

  Setting the root password ensures that nobody can log into the MySQL
  root user without the proper authorisation.

  Set root password? [Y/n] y <-- [y]
  New password: <-- [パスワード入力]
  Re-enter new password: <-- [再パスワード入力]
  Password updated successfully!
  Reloading privilege tables..
  ... Success!


  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? [Y/n] <-- [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? [Y/n] <-- [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? [Y/n] <-- [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? [Y/n] <-- [y]
  ... Success!

  Cleaning up...

  All done! If you've completed all of the above steps, your MySQL
  installation should now be secure.

  Thanks for using MySQL!

 


 5. データベースとテーブル作成

    <DB名>hogedb

 5.1. 接続
    # mysql --user=root --password=hogehoge

 5.2. DB作成
    mysql>create database hogedb;

 5.3. DBを指定
    mysql>use hogedb;

   5.4. テーブル作成
   mysql>create table test(id int PRIMARY KEY, name varchar(20));

   5.5. テーブル確認
   mysql>show tables;


 6. ユーザ作成

   6.1. mysql管理者ユーザ作成(mysqlのrootユーザではない)

 <例>ユーザ名:system  パスワード:manager
     全データベースの管理者:アクセス可
 
 mysql>grant all privileges on *.* to system@'172.16.80.%' identified by 'manager' with grant option;
  mysql>grant all privileges on *.* to system@'localhost' identified by 'manager' with grant option;

 

 6.2. mysql一般ユーザ(選択、挿入、更新、削除OK)

 <例>ユーザ名:scott  パスワード:tiger
     指定データベース:アクセス可
  mysql>grant select,insert,update,delete on jusmine.* to scott@'172.16.80.%' identified by 'tiger';

  mysql>grant select,insert,update,delete on jusmine.* to scott@'localhost' identified by 'tiger'; 

 

 6.3. mysql一般ユーザ(参照のみ)

 <例>ユーザ名:adams パスワード:tiger
     指定データベース:アクセス可
  mysql>grant select,insert,update,delete on jusmine.* to adams@'172.16.80.%' identified by 'adams';

  mysql>grant select,insert,update,delete on jusmine.* to adams@'localhost' identified by 'adams'; 

 

 6.4. rootユーザがクライアントPCからログインする場合(通常作らない)

 <例>ユーザ名:root パスワード:hogehoge
     指定データベース:アクセス可
  mysql>#grant all privileges on *.* to root@'172.16.80.%' identified by 'hogehoge' with grant option;

 

 6.5. ユーザ一覧を表示

    mysql> use mysql
 mysql> select host,user from user; 
  または
 mysql> select host,user from mysql.user;

  +-------------+--------+
  | host | user |
  +-------------+--------+
  | 127.0.0.1 | root |
  | 172.16.80.% | adams | <--追加(クライアント接続用)
  | 172.16.80.% | scott | <--追加(クライアント接続用)
  | 172.16.80.% | system | <--追加(クライアント接続用)
  | localhost | adams | <--追加(サーバ接続用)
  | localhost | root |
  | localhost | scott | <--追加(サーバ接続用)

 

 6.6. ユーザ一覧を表示(特定のDBのみ接続できるユーザ)
 
全データベースの権限もっているものは表示されない

    mysql> select Host,Db,User from db;

 +-------------+---------+-------+
 | Host | Db | User |
 +-------------+---------+-------+
 | 127.0.0.1 | jusmine | scott |
 | 172.16.80.% | testdb | adams |
 | 172.16.80.% | testdb | scott |
 | 172.16.80.% | jusmine | adams |
 | 172.16.80.% | jusmine | scott |
 | localhost | jusmine | adams |
 | localhost | jusmine | scott |
 +-------------+---------+-------+


 7. 接続テスト(クライアント/サーバ間での接続テスト)

クライアント/サーバ間での接続テスト(workbench経由とaccess経由)を行う。
ODBC→システムDSN→【MySQL ODBC 5.x ANSI Driver】を選択
※接続できなかったら調べる

※参考URL
http://dev.mysql.com/doc/refman/5.1/ja/connection-access.html