masayuki5160's diary

名古屋でエンジニアしてます。

MySQLインストール後にDBとユーザを作成してみる

DBについてまだまだ知識不足なのでメモがてらまとめてみる。

0.MySQLインストール
 以前書いたので省略
1.rootパスワード設定
2.DB作成してみる
3.作成したDB専用の管理ユーザを作成してみる


0.MySQLインストール

 これはこちらを参照
 LAMP環境構築めも http://d.hatena.ne.jp/masayuki5160/20120218/1329573154

1.rootパスワードの設定
 インストール直後はすべてのユーザにパスワードは設定されていない。

mysql> select host,user,password from mysql.user;

                                                                                        • +
host user password
                                                                                        • +
localhost root
127.0.0.1 root
localhost
                                                                                        • +

3 rows in set (0.00 sec)

というわけでなにはともあれrootにパスワードを設定。

mysql> set password for root@localhost=PASSWORD('password');
Query OK, 0 rows affected (0.00 sec)

ためしに確認してみる。

[root@www masayuki]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.0.95 Source distribution

Copyright (c) 2000, 2011, 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>

2.DB作成してみる

ためしにSAMPLEという名前のDBをつくります。

mysql> create database sample;
Query OK, 1 row affected (0.00 sec)

mysql> show databases;

                                          • +
Database
                                          • +
information_schema
mysql
sample
test
                                          • +

4 rows in set (0.01 sec)

3.作成したDB専用の管理ユーザを作成してみる

でもこのままだとrootユーザですべてのDBを操作できちゃうわけで、なんかあぶなっかしい。というわけで専用ユーザを作成。

mysql> grant all privileges on sample.* to masayuki@localhost identified by 'masa';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)

これでsample専用のユーザmasayukiができました。
”flush privileges;”とすることでユーザテーブルをリフレッシュしているそうな。


でmasayukiでログインすると、
デフォルトで作成されているDBとsample(権限を持っているDB)しかみえない。
下記例ではtanakaというDBがmasayukiでログインしたときには見えていないことが確認できる。

[root@www masayuki]# mysql -u root -p
Enter password:

...
省略
...

mysql> show databases;

                                          • +
Database
                                          • +
information_schema
mysql
sample
tanaka
test
                                          • +

5 rows in set (0.00 sec)

mysql> quit
Bye
[root@www masayuki]# mysql -u masayuki -p
Enter password:

...
省略
...

mysql> show databases;

                                          • +
Database
                                          • +
information_schema
sample
test
                                          • +

3 rows in set (0.00 sec)

mysql> quit
Bye


参考サイト
MySQLのユーザ管理 http://linux.kororo.jp/cont/server/mysql_user.php