MySQLコマンド メモ

WebサーバとDBサーバが1台のマシンにある場合によく行う操作。
# そのマシンのMySQLサーバにrootで接続
# (パスワードを聞かれるので入力してEnter)
# mysql -h 127.0.0.1 -u root -p

mysql> -- UTF8でDBを作成し、接続ユーザを作成 (先頭がtestの文字は読み替えて下さい)
mysql> create database testdb default character set utf8;
mysql> grant all on testdb.* to 'testuser'@'localhost' identified by 'testpassword';
mysql> flush privileges;

mysql> -- 別マシンからDBへ接続する必要がある場合は、以下のように別ユーザを作成
mysql> -- (192.168.0.1の部分を別マシンのIPに読み替える)
mysql> grant all on testdb.* to testuser@192.168.0.1 identified by 'testpassword';

mysql> -- 作成したDBに切り替え、テーブルを作成
mysql> use testdb;

mysql> DROP TABLE IF EXISTS `users`;

mysql> CREATE TABLE `users` (
mysql>   `id`         bigint unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
mysql>   `username`   varchar(32) NOT NULL
mysql> ) ENGINE=InnoDB;