LinuxMintでMySQL

何回かインストールに失敗したのでメモする

インストールは・・・

sudo apt update
sudo apt install mysql-server
sudo mysql_secure_installation

ここで最初にrootのパスワードを入力する。
後はひたすら「Y」でエンター
★これを理解できず何度も繰り返した(削除とインストール)

動作確認

sudo systemctl status mysql

ログインする

sudo mysql -u root -p

 でエンター

<password> さっきのrootパスワード

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

Copyright (c) 2000, 2024, 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

自動起動設定

sudo systemctl start mysql
sudo systemctl enable mysql

さて、MySQLを全削除するには・・・

sudo systemctl stop mysql
sudo systemctl disable mysql
sudo apt-get remove --purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
sudo apt-get autoremove
sudo apt-get autoclean
sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql
sudo rm -rf /var/cache/mysql

これで完全に削除されるそうな・・・

SHOW DATABASES;


+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
+——————–+
4 rows in set (0.02 sec)

mysql>

DBを作成

CREATE DATABASE test;
SHOW DATABASES;


+——————–+
| Database |
+——————–+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+——————–+
5 rows in set (0.00 sec)

mysql>

test DBが作成できた

testtblを作成する

USE test;
CREATE TABLE testtbl (
id INT,
item VARCHAR(50),
comment VARCHAR(250)
);
SHOW TABLES;


+—————-+
| Tables_in_test |
+—————-+
| testtbl |
+—————-+
1 row in set (0.01 sec)

mysql>

DESCRIBE testtbl;


+———+————–+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+———+————–+——+—–+———+——-+
| id | int | YES | | NULL | |
| item | varchar(50) | YES | | NULL | |
| comment | varchar(250) | YES | | NULL | |
+———+————–+——+—–+———+——-+
3 rows in set (0.01 sec)

mysql>

ん〜できた

データを追加してみるか

INSERT INTO testtbl (id, item, comment) VALUES (1, 'Apple', 'This is a red apple');
INSERT INTO testtbl (id, item, comment)
VALUES
(2, 'Banana', 'This is a yellow banana'),
(3, 'Orange', 'This is an orange fruit');

SELECT * FROM testtbl;


+——+——–+————————-+
| id | item | comment |
+——+——–+————————-+
| 1 | Apple | This is a red apple |
| 2 | Banana | This is a yellow banana |
| 3 | Orange | This is an orange fruit |
+——+——–+————————-+
3 rows in set (0.00 sec)

mysql>

できた

修正(UPDATE)

 update testtbl set item='bunchan' where id=2;

Query OK, 1 row affected (0.03 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SELECT * FROM testtbl;

+——+———+————————-+
| id | item | comment |
+——+———+————————-+
| 1 | Apple | This is a red apple |
| 2 | bunchan | This is a yellow banana |
| 3 | Orange | This is an orange fruit |
+——+———+————————-+
3 rows in set (0.00 sec)

mysql>

削除(DELETE)

delete from testtbl where id=2;

Query OK, 1 row affected (0.02 sec)

SELECT * FROM testtbl;

+——+——–+————————-+
| id | item | comment |
+——+——–+————————-+
| 1 | Apple | This is a red apple |
| 3 | Orange | This is an orange fruit |
+——+——–+————————-+
2 rows in set (0.01 sec)

mysql>

いいね〜

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です