MySQL

MySQL tips

MySQLのCLI 「mysql」に関するtips

select の最後に \G を付ける事で一覧を縦表示にしてくれる
mysql> select schema_name, default_character_set_name, default_collation_name  from information_schema.schemata;
+--------------------+----------------------------+------------------------+
| schema_name        | default_character_set_name | default_collation_name |
+--------------------+----------------------------+------------------------+
| information_schema | utf8                       | utf8_general_ci        |
| mysql              | latin1                     | latin1_swedish_ci      |
| performance_schema | utf8                       | utf8_general_ci        |
| soodb              | utf8                       | utf8_general_ci        |
| sys                | utf8                       | utf8_general_ci        |
+--------------------+----------------------------+------------------------+
5 rows in set (0.00 sec)

mysql> select schema_name, default_character_set_name, default_collation_name  from information_schema.schemata \G
*************************** 1. row ***************************
               schema_name: information_schema
default_character_set_name: utf8
    default_collation_name: utf8_general_ci
*************************** 2. row ***************************
               schema_name: mysql
default_character_set_name: latin1
    default_collation_name: latin1_swedish_ci
*************************** 3. row ***************************
               schema_name: performance_schema
default_character_set_name: utf8
    default_collation_name: utf8_general_ci
*************************** 4. row ***************************
               schema_name: soodb
default_character_set_name: utf8
    default_collation_name: utf8_general_ci
*************************** 5. row ***************************
               schema_name: sys
default_character_set_name: utf8
    default_collation_name: utf8_general_ci
5 rows in set (0.00 sec)

mysql>
ログイン
mysql -u root -p
[root@vm103 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
-- -pの後にそのままパスワード記載する事も可能(Warningでちゃうけど、、)
[root@vm103 ~]# mysql -u root -psoopass
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.39 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> exit
Bye

-- 以下のように接続タイミングでデータベースを指定する事もできます
-- 
[root@vm103 ~]# mysql -u sooni -psoopass soodb
mysql: [Warning] Using a password on the command line interface can be insecure.
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

ユーザ作成

create user 'sooni'@'%' identified by 'soopass';

ユーザへ権限付与

grant all privileges on *.* to 'sooni'@'%';
-- ユーザの作成
mysql> create user 'sooni'@'%' identified by 'soopass';
Query OK, 0 rows affected (0.00 sec)

-- 権限付与(*.*とはデータベース.テーブルを表しています)
-- ※ここでは検証用に付与していますが、通常は過剰な権限付与とならないように注意してください。
mysql> grant all privileges on *.* to 'sooni'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> select host,user from mysql.user order by user;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
| %         | root          |
| %         | sooni         |
+-----------+---------------+
5 rows in set (0.00 sec)

mysql>

データベース作成

以下はキャラクターセット、照合順序(collate)を指定して作成しています。省略するとそれぞれ、latin1,latin1_swedish_ciとなり日本語の取り扱いが大変となるためです。何よりデフォルトで作成されているinformation_schemaやperformance_schem,sysなどもutf8/utf8_general_ciで作成されているので統一しておいたほうが良いと思います。(utf8を指定しておけば、照合順序はデフォルトでutf8_general_ciになります。参考までにですが、ciとは大文字小文字が区別されません。csにすると区別します)

create database soodb default character set utf8 collate utf8_general_ci;

データベース一覧

show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| soodb              |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql>
select schema_name, default_character_set_name, default_collation_name 
from information_schema.schemata;
mysql> select schema_name, default_character_set_name, default_collation_name  from information_schema.schemata;
+--------------------+----------------------------+------------------------+
| schema_name        | default_character_set_name | default_collation_name |
+--------------------+----------------------------+------------------------+
| information_schema | utf8                       | utf8_general_ci        |
| mysql              | latin1                     | latin1_swedish_ci      |
| performance_schema | utf8                       | utf8_general_ci        |
| soodb              | utf8                       | utf8_general_ci        |
| sys                | utf8                       | utf8_general_ci        |
+--------------------+----------------------------+------------------------+
5 rows in set (0.00 sec)

mysql>

ログファイルの所在

ls -lt /var/log/mysqld.log
スポンサーリンク
タイトルとURLをコピーしました