ksino's diary

覚えたことを忘れないように、小さなことでも書いていく。

Ubuntu 16.04 LTSにMySQLをインストールする

インストール

sudo apt update
sudo apt install mysql-server mysql-client

インストール中にrootユーザのパスワード入力を求められる。

DBの作成

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> create database test_db;
Query OK, 1 row affected (0.00 sec)

mysql> grant all on test_db.* to 'user' identified by 'password';
Query OK, 0 rows affected, 1 warning (0.03 sec)

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

mysql> quit
Bye

DBを使用してみる

$ mysql -u user -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.16-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test_db            |
+--------------------+
2 rows in set (0.00 sec)

mysql> connect test_db;
Connection id:    6
Current database: test_db

mysql> create table emp (id INT, name varchar(20));
Query OK, 0 rows affected (0.32 sec)

mysql> insert into emp values(1, 'aaa');
Query OK, 1 row affected (0.08 sec)

mysql> insert into emp values(2, 'bbb');
Query OK, 1 row affected (0.04 sec)

mysql> select * from emp;
+------+------+
| id   | name |
+------+------+
|    1 | aaa  |
|    2 | bbb  |
+------+------+
2 rows in set (0.00 sec)

mysql> quit
Bye

MySQLの停止/起動/再起動/ステータス

$ sudo systemctl stop mysql
$ sudo systemctl start mysql
$ sudo systemctl restart mysql
$ sudo systemctl status mysql

自動起動の無効化/有効化

$ sudo systemctl disable mysql
$ sudo systemctl enable mysql