MySQL 5.5インストールとSQL文のメモ

MySQL 5.5のインストール

1.以下のURLより、rpmパッケージをダウンロード
http://dev.mysql.com/downloads/mysql/#downloads

MySQL-server-5.5.27-1.linux2.6.x86_64.rpm
MySQL-client-5.5.27-1.linux2.6.x86_64.rpm
MySQL-devel-5.5.27-1.linux2.6.x86_64.rpm
MySQL-shared-5.5.27-1.linux2.6.x86_64.rpm

2.rpmパッケージをインストール

[root@ha-01 tmp]# rpm -ivh MySQL-client-5.5.27-1.linux2.6.x86_64.rpm

[root@ha-01 tmp]# rpm -ivh MySQL-server-5.5.27-1.linux2.6.x86_64.rpm

[root@ha-01 tmp]# rpm -ivh MySQL-devel-5.5.27-1.linux2.6.x86_64.rpm

[root@ha-01 tmp]# rpm -ivh MySQL-shared-5.5.27-1.linux2.6.x86_64.rpm

3.MySQLサーバーの起動

[root@ha-01 tmp]# /etc/init.d/mysql start

4.初期設定

[root@ha-01 tmp]# 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.

Enter 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                                  ← rootユーザーのパスワード設定
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                       ← リモートからrootユーザーへのアクセスを拒否する
 ... 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              ← testデータベースの削除
 - 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!


[root@ha-01 tmp]#

5.MySQL Serverへの接続

[root@ha-01 tmp]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.27-log MySQL Community Server (GPL)

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>

6.MySQL Serverへの切断

mysql> quit

SQL文の基本操作

データベースの作成

mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)

データベースの選択

mysql> USE db1;

データベースの一覧表示

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)

テーブルの作成

mysql> CREATE TABLE list (id INT , dep INT, name CHAR(30));

フィールドの構造を表示

mysql> DESC list;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| dep   | int(11)  | YES  |     | NULL    |       |
| name  | char(30) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.04 sec)

フィールドの削除

mysql> ALTER TABLE list DROP dep;

mysql> DESC list;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(30) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

フィールドの追加

mysql> ALTER TABLE list ADD title CHAR(30) AFTER name;

mysql> DESC list;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id    | int(11)  | YES  |     | NULL    |       |
| name  | char(30) | YES  |     | NULL    |       |
| title | char(30) | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
3 rows in set (0.00 sec)

データの挿入

mysql> INSERT INTO list (id,name,title) VALUES (1, 'Discharge','Never Again');

データの検索

mysql> SELECT * FROM list;
+------+-----------+-------------+
| id   | name      | title       |
+------+-----------+-------------+
|    1 | Discharge | Never Again |
+------+-----------+-------------+
1 row in set (0.00 sec)

mysql> SELECT name,title FROM list;
+-----------+-------------+
| name      | title       |
+-----------+-------------+
| Discharge | Never Again |
+-----------+-------------+
1 row in set (0.00 sec)

データの削除

mysql> DELETE FROM list WHERE id=1;

テーブルの削除

mysql> DROP TABLE list;

AUTO INCREMENTの使用

mysql> CREATE TABLE list ( id INT PRIMARY KEY AUTO_INCREMENT , name CHAR(30) NOT NULL, title CHAR(30) NOT NULL );
Query OK, 0 rows affected (0.01 sec)

mysql> DESC list;
+-------+----------+------+-----+---------+----------------+
| Field | Type     | Null | Key | Default | Extra          |
+-------+----------+------+-----+---------+----------------+
| id    | int(11)  | NO   | PRI | NULL    | auto_increment |
| name  | char(30) | NO   |     | NULL    |                |
| title | char(30) | NO   |     | NULL    |                |
+-------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

mysql> INSERT INTO list (name,title) VALUES ('Discharge','Never Again'),('Aphex Twin','Ambient Work'),('Bjork','Debut');
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM list;
+----+------------+--------------+
| id | name       | title        |
+----+------------+--------------+
|  1 | Discharge  | Never Again  |
|  2 | Aphex Twin | Ambient Work |
|  3 | Bjork      | Debut        |
+----+------------+--------------+
3 rows in set (0.00 sec)

mysql>

管理者権限を持つユーザーの作成

mysql> GRANT ALL PRIVILEGES ON *.* TO user1@localhost IDENTIFIED BY 'xxxxxx';
mysql> GRANT ALL PRIVILEGES ON *.* TO user1@'%' IDENTIFIED BY 'xxxxxx';
mysql> FLUSH PRIVILEGES;