SQL_


はじめに

MySQLのインストール

  • インストールしたパッケージ(debian)
mysql-server 
mysql-client 
mysql-doc 
  • mysqlのルートユーザパスワードを設定(普段のrootユーザではありません)
# /usr/bin/mysqladmin -u root password 'new-password' 
  • 毎回パスワードを入力するに疲れるとかスクリプトから自動にしたい場合
#vi /root/.my.cnf
参考:/usr/share/doc/mysql-doc,www.mysql.com(MySQLマニュアル) 
注意:このファイルは"password"行を含むときはいつでも"user"行を含みます。
Debianメンテナンススクリプトは/etc/mysql/debian.cnfを使い、
ユーザ名"debian-sys-maint"を使います。
しかしパスワードはルートの .my.cnfにあります。
 /root/.my.cnfに対する変更はmysqlのcronスクリプトにも影響があることにも注意し
てください。 
  • パスワード設定(hogehogehogeに設定の場合)
$ mysqladmin -u root password hogehogehog
  • 確認作業
    $ mysql -u root -p
    Enter password: ********
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 6 to server version: 3.23.49-log
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
    mysql> quit
    Bye

ユーザ作成

DB作成Sledgeで使用するデータベース www1とユーザ wwwdataを作成します。 
wwwdataにはwww1というデータベースにのみアクセス権限を与えます。 
$ mysql -u root mysql -p
Enter password: ********
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7 to server version: 3.23.49-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create database www1;
Query OK, 1 row affected (0.00 sec) 
mysql> grant usage on *.* to wwwdata@localhost identified by '3edc4rfv';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on www1.* to wwwdata@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
  • 権限の設定(補足)
最初に「grant usage on *.* …」としてグローバルな権限を無しに設定します。
次に「grant all on www1.* …」としてデータベースwww1に限って全ての
権限を与えます。 
  • wwwdataユーザでwww1データベースに接続し、テーブルを作成できることを確認します。
$ mysql -u wwwdata -p www1
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8 to server version: 3.23.49-log
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> create table dummy (name varchar(20), memo varchar(255));
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_www1 |
+----------------+
| dummy          |
+----------------+
1 row in set (0.00 sec)
mysql> drop table dummy;
Query OK, 0 rows affected (0.00 sec)
mysql> show tables;
Empty set (0.00 sec)
mysql> quit
Bye

データベース操作

データベース作成

  • ■mysqlユーティリティ
mysqlユーティリティはMySQLへのコマンドライン・インターフェースです。起動する
ためには、Linuxのシェル上で以下のコマンドを入力してください。
shell>mysql -u root -pxxxxx
上はユーザがrootでそのパスワードがxxxxxであることを意味します。このコマンド
の結果、次のようなmysqlのコマンドラインに入ります。
mysql>
  • ■データベースの作成
    • 次のコマンドで新規のデータベースtest1を作成します。
mysql>CREATE DATABASE test1;
mysql>GRANT ALL ON bbsdb.* TO test1@localhost identified by "wwwdata";
このGRANTコマンドは、データベースbbsdbの全てのテーブルに対して、全権利(ALL)
を持つユーザwwwdataをパスワード3edc4rfvで作成します。
mysql>FLUSH PRIVILEGES;
FLUSHコマンドを実行した時点でtest1のwwwdataへのアクセスが有効になります。これ
以降データベースtest1へのアクセスはユーザwwwdataで行います。
  • ■mysqlユーティリティから抜ける
コマンド \q でmysqlを終わり、シェルへ戻ります。
mysql>\q
SHELL>
  • ■データベース操作の説明

・データベースの表示(show databases;)  ここではデータベースの削除の前後に2度表示しています。

・データベースの削除(drop database bbsdb;)  ここではデータベースbbsdbを削除しています。

・テーブルの表示(show tables;)  ここではテーブル削除の前後に2度表示しています。

・テーブルの削除(drop table bbs_table;)  ここではテーブルbbs_tableを削除しています。


テーブルへのデータの追加と削除

  • テーブルへのデータ追加
    mysql> create table テーブル名 (フィールド名1 データ型1, フィールド名2 カラム名2 データ型2, ...);
  • レコードの変更
    mysql> update テーブル名 set フィールド名1 = 値1 , フィールド名2 = 値2, ... where 条件;
  • レコードの削除
    delete from テーブル名 where 条件式;
  • レコードからデータの検索
    mysql> select 検索したいフィールド名 from 検索対象であるテーブル名;
  • appleをfruitのnameから検索する
    select * from fruit where name = 'apple';

よく使うコマンドリスト

FAQ

リンク

てきとうなリンク

最新の7件

2015-05-27 2009-06-06 2007-05-15 2007-02-17 2006-09-22 2005-02-04 2004-11-29

Counte

  • counter: 1512
  • today: 1
  • yesterday: 0
  • online: 1