0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MySQL

Last updated at Posted at 2020-08-15

MySQL サーバーが動いているかどうかは、管理者コマンドを使って調べることができる。
[vagrant@localhost mysql_lessons]$ sudo service mysqld status

MySQLサーバーに接続 mysql -u ユーザー名

mysql の root ユーザーがパスワードなしで、すでに設定されていれば mysql -u root としてあげると接続できる。
[vagrant@localhost mysql_lessons]$ mysql -u root

使えるコマンドが表示されました

mysql> help;

現在のユーザーを表示する

mysql> select user();

入力(;)が足りないと
-> ;

; を忘れたときに、今のコマンドをキャンセルして次のコマンドを打ちたい場合には \c としてあげれば OK です。

mysql> select user()
-> \c

MySQL サーバーへの接続を終了するには quit; とするか、もしくは単に \q

mysql> \q

画面をクリアするには

Control + L

データベースを作る

ログイン

[vagrant@localhost mysql_lessons]$ mysql -u root

データベースの一覧を表示

mysql> show databases;

データベースを作る create database データベース名

mysql> create database mydb01;

データベースを削除する drop database データベース名

mysql> drop database mydb03;

操作対象になっているデータベースを確認する select database();

mysql> select database();

操作対象を設定する use データベース名;

実際にデータベースの中のデータを扱っていくには、操作対象として選択する
mysql> use mydb02;

権限が限られた作業用ユーザーを作る

作業用ユーザーを作る create user ユーザー名@サーバー名 identified by 'パスワード';

作業用ユーザーを作るには、 root ユーザーで作業する
dbuser01 という名前で localhost からアクセスしてくるユーザーを設定する。

mysql> create user dbuser01@localhost identified by '6AVAkig2';

権限を通す。grant

dbuser01 に対して mydb01 の全てのテーブルに関する全ての権限を与える、という意味。

mysql> grant all on mydb01.* to dbuser01@localhost;

MySQL サーバーへの接続を終了するには quit; とするか、もしくは単に \q

mysql> quit;

ユーザー名とデータベース名を指定して接続 mysql -u ユーザー名 -p データベース名

-p はパスワードを入力する時。
[vagrant@localhost mysql_lessons]$ mysql -u dbuser01 -p mydb01

作業用ユーザーを削除する。 drop user ユーザー名@サーバー名;

root ユーザーで作業する

mysql> drop user dbuser01@localhost;

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?