MacのMySQL操作
準備
MySQLサーバーを立ち上げる
$ sudo mysql.server start
エラーが起きた場合
$ sudo mysql.server start . ERROR! The server quit without updating PID file (/usr/local/var/mysql/xxxxxxxxxxxxx.local.pid).
権限を変えてやる必要がある。
$ sudo chown -R _mysql:_mysql /usr/local/var/mysql
MySQLサーバに接続
~ $ mysql -u root
データベースの一覧を見る
show databases;
データベースの作成
create database [データベース名];
データベースの削除
drop database [データベース名];
操作対象のデータベースを見る
select database();
操作するデータベースの選択
use [データベース名];
※大文字小文字は区別されない。
作業用ユーザーの作成
create user [ユーザー名] identified by '[パスワード]';
権限の設定([データベース名]における全ての権限を与える)
grant all on [データベース名].* to [ユーザー名];
応用編
権限の設定+ユーザー作成
grant all on [データベース名].* to [ユーザー名] identified by '[パスワード]';
ホスト名を確認
show variables like 'hostname';
データベースから退出
\q
MySQLサーバーを停止
$ sudo mysql.server stop
ユーザーを指定してMySQLにログイン
$ mysql -u [ユーザー名] -p [パスワード(省略可)] [データベース名]
テーブルを作る(例文)
~ $ create table users ( id int not null auto_increment primary key, name varchar(255), email varchar(255) unique, password char(32), score double, sex enum('male', 'female') default 'male', memo text, created datetime, key score (score) );
データベースのテーブルを確認
show tables;