1
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 1 year has passed since last update.

mysql 忘備録 2023

Last updated at Posted at 2020-01-19

最初に100回復唱

  • データの文字列以外はシングルクオートではなく基本バッククオート。
  • パスワードはシングルクオート。
  • @ はクオートの中に入れない。
  • 末尾にセミコロン必要。
  • 大文字小文字は区別されない。

phpmyadmin が使える環境なら使うべき。

本体インストール

2023/12 現在、mysql 9.0 では以下の通り。

$ sudo apt indtall mysql-client mysql-server
$ sudo mysql_secure_installation

下記について質問される。

  • VALIDATE PASSWORD COMPONENT を使用するか?(パスワードの強度チェックをするか)
  • anonymous user を削除するか?
  • リモートでの root ユーザのログインを無効化するか?
  • test database を削除するか?
  • privilege tables をすぐにリロードするか?

特に問題がないなら全部 y でいいはず。

root 起動

$ sudo mysql -u root -p

パスワードの設定がされていないときは Enter だけでログインできる。(すぐ設定すること)

root のパスワード設定

> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root-password';

ユーザの作成

> create user 'username'@'localhost' identified by 'password';

ローカルユーザの場合。パスワードはシングルクオート。

ユーザへの権限付与

> create database `dbname`;
> grant all privileges on dbname.* to 'username'@'localhost';

ユーザ一覧

> select user, host from mysql.user;

ユーザのパスワード変更

> set password for user = 'password'

テーブルの作成

> use database;
> create table `tablename` (`id` int (11) auto_increment not null primary key, \
    `name` varchar(32) not null, \
    `time` timestamp, \
     position double(4,3)) \
     engine=innodb, charset=utf8;

テーブルの構造を見る

> describe tablename;
+----------+-------------+------+-----+-------------------+-----------------------------+
| Field    | Type        | Null | Key | Default           | Extra                       |
+----------+-------------+------+-----+-------------------+-----------------------------+
| id       | int(11)     | NO   | PRI | NULL              | auto_increment              |
| name     | varchar(32) | NO   |     | NULL              |                             |
| time     | timestamp   | NO   |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| position | double(4,3) | YES  |     | NULL              |                             |
+----------+-------------+------+-----+-------------------+-----------------------------+
4 rows in set (0.00 sec)

テーブルへのデータの追加

> insert into tablename values (null, 'hoge', null, 3.1415926 );
> insert into tablename values (null, 'hoge', '2020-01-01 01:02:03' , 1.414213 );

データを見る

> select * from tablename;
+----+------+---------------------+----------+
| id | name | time                | position |
+----+------+---------------------+----------+
|  1 | hoge | 2020-01-01 05:34:25 |    3.142 |
|  2 | hoge | 2020-01-01 01:02:03 |    1.414 |
+----+------+---------------------+----------+
2 rows in set (0.00 sec)

select 文いろいろ

行数を制限する。

> select * from tablename limit by 10;

ソートする。asc は昇順、desc は降順。

> select * from tablename order by name asc;
> select * from tablename order by name desc;
> select * from tablename order by name desc, id asc;

検索条件を指定

> select * from tablename where name == 'hoge';
> select * from tablename where id < 10;
> select * from tablename where name == 'hoge' and id >= 1;
> select * from tablename where time between timestamp('2020-01-01 00:00:00') AND timestamp('2020-01-01 02:00:00');
> select * from tablename where time > timestamp('2020-01-01 02:00:00');

datatime 型の場合は datatime('') にする。

ファイルへの書き出し

セキュリティ設定によって、書き出しに制約がある。

> select @@secure_file_priv;
+-----------------------+
| @@secure_file_priv    |
+-----------------------+
| /var/lib/mysql-files/ |
+-----------------------+
1 row in set (0.00 sec)
> select * from tablename into outfile '/var/lib/mysql-files/hoge.sql';

@secure_file_priv が指定されていると、そこにしか書き出せない。また root 以外はデフォルトではファイル書き出し権限がない。

ファイルからの読みこみ

$ sudo mysql -u root < filename.sql

または mysql にログインした状態で

> source filename;

とする。

テーブル削除

> drop tablename;

警告も何もないので超注意。

キャラクターセットの確認

> show variables like '%char%';

参考

1
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
1
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?