ログイン
mysql -u root -p
データベース一覧
show databases;
データベースの作製
create database testdb;
データベースの削除
drop database testdb;
データベースを選択
use database testdb;
選択しているデータベースの確認
select database();
テーブル一覧
show tables;
show table status;
テーブルの作製
CREATE TABLE `testtable` (
`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT "ID",
`name` VARCHAR(100) NOT NULL COMMENT "ユーザー名"
) ENGINE=[InnoDB/MyISAM] DEFAULT CHARSET=[文字コード];
テーブルの削除
drop table testtable;
テーブル名の変更
alter table oldname rename newname;
カラムの追加
alter table `testtable` add [カラム情報];
ターブル情報
desc `tablename`;
show full columns from `tablename`;
インサート
insert into testtable (id,`name`) values (1,"yamada");
アップデート
update `testtable` set `name` = "suzuki" where `id` = 1;
デリート
delete from `testtable` where `id`=1;
トランザクション
start transaction;
...query;
commit; or rollback;
ダンプ
mysqldump -u root -p -x testdb > dumpdb.dump
リストア
mysql -u root -p new_db < dumpdb.dump
実行結果をファイルに出力
mysql -u root -p -e "select * from testtable" testdb > /tmp/testtable_select_query.txt
MySqlのユーザ情報
SELECT Host, User, Password FROM mysql.user;
ユーザの追加
create user `user`@`localhost` IDENTIFIED BY 'password';
ユーザの権限付与
grant all privileges on testdb.* to user@localhost IDENTIFIED BY 'password';
現在のユーザのパスワード変更
set password = password('newpassword');
ユーザを指定してパスワードの変更
mysql > set password for 'user'@'localhost' = password('pass');