Backend

[MariaDB] 이모지 저장하기 위해 인코딩 확인

wonpick 2021. 5. 10. 12:30

디비를 다운시키고 난 하루 고생한 뒤 이모지를 띄우는데 성공
역시 뭐든지 한번에 되는일은 없나보다😭

1. mariadb character set 확인 (제대로 모두 utf8bm4로 되어있음을 확인)

MariaDB [mydb]> show variables like 'c%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
| collation_connection     | utf8mb4_general_ci         |
| collation_database       | utf8mb4_general_ci         |
| collation_server         | utf8mb4_general_ci         |
| completion_type          | NO_CHAIN                   |
| concurrent_insert        | AUTO                       |
| connect_timeout          | 10                         |
| core_file                | OFF                        |
+--------------------------+----------------------------+

2. /etc/mysql/my.cnf 변경

#이모티콘 인코딩 때문에 수정 -2/21 오후 2시
[client]
default-character-set = utf8mb4

[mysql]
default-character-set = utf8mb4

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

3. 데이터 베이스 테이블 character set 확인

# 데이터베이스 인코딩 확인
SELECT default_character_set_name, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE schema_name = "mydb";
+----------------------------+------------------------+
| default_character_set_name | DEFAULT_COLLATION_NAME |
+----------------------------+------------------------+
| utf8mb4                    | utf8mb4_unicode_ci     |
+----------------------------+------------------------+
1 row in set (0.00 sec)

# 테이블 인코딩 확인
SELECT CCSA.character_set_name FROM information_schema.`TABLES` T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA WHERE CCSA.collation_name = T.table_collation AND T.table_schema = “mydb” AND T.table_name = “reply”;

+--------------------+
| character_set_name |
+--------------------+
| utf8mb4            |
+--------------------+

4. 기존 데이터베이스나 테이블이 CHARSET이 UTF8로 되었다면 utf8mb4로 변경

MariaDB [(none)]> use mydb;

# 데이터베이스 변경
MariaDB [mydb]> ALTER DATABASE mydb CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

#테이블 변경
MariaDB [mydb]> ALTER TABLE reply CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

#db 재시작하기
sudo service mysql restart

5. 확인

MariaDB [mydb]> select * from reply;
+----------+---------------------+----------------------+-----------------+--------------+-------------------+
| reply_id | reply_create        | reply_contents       | parent_reply_id | work_work_id | author_author_num |
+----------+---------------------+----------------------+-----------------+--------------+-------------------+
|    
|        6 | 2021-02-21 13:59:50 | 😃                    |            NULL |            4 |              NULL |
|        7 | 2021-02-21 14:00:06 | 😀                    |            NULL |            2 |              NULL |
|        8 | 2021-02-21 14:03:02 | 😀                    |            NULL |            1 |              NULL |
+----------+---------------------+----------------------+-----------------+--------------+-------------------+
7 rows in set (0.00 sec)

6. 그래도 남아있는 오류..

이모티콘이 뜨는 순간 소리 지르고 난리도 아니었다. 저 몇줄 썼다고 저렇게 금방 되다니 진짜 배신감
하지만 여전히 아래와 같은 db가 다운되는 문제가 발생한다 ㅠㅠ

😡오류) ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
ERROR: Can't connect to the server

#임시방편으로 찾은 해결방법
🚑 sudo service mysql restart #하지만 restart해도 로딩되다가 종료가 안돼서 ctrl+c로 종료 후 그냥 디비 접속을 하면 된다.. 왜지 😨
 sudo mysql -u root -p

6-1. 플러그인 문제인가?

플러그인에 대해 완전 설명 잘해놓으신 게시물 발견
MariaDB; 인증(Authentication) 문제.
MariaDB 10.4 이후의 계정 인증 관련 변화

플러그인에 대해서 찾다가 아래 로그인 없이 접속이 가능해지는 문제도 플러그인과 관련되어있음을 알 수 있었다.
mariadb 비밀번호 없이 접속되는 문제

# 의심가는 증상 플러그인 때문에 버전이 낮은데 unix socket으로 되어있어 그런건가? 싶었지만 아니었던것 같다.. 일단 변경해줬기 때문에 기록

데이터베이스의 plugin 변경
# 버전은 15.1.xx인데 unix socket은 15.4이상만 가능하다고 한다.. (명령어도 sudo mariadb로 접속가능... 근데 난 왜 되지? 모르는거 투성이
$  mariadb -V
mariadb  Ver 15.1 Distrib 10.1.47-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2

$ sudo mariadb
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.1.47-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04

MariaDB [(none)]> use mydb;
MariaDB [mydb]> update user set plugin='mysql_native_password' where user='myuser';
😡ERROR 1146 (42S02): Table 'mydb.user' doesn't exist

MariaDB [mydb]> SELECT user,host,plugin,authentication_string FROM mysql.user;
+--------+-----------+-------------+-----------------------+
| user   | host      | plugin      | authentication_string |
+--------+-----------+-------------+-----------------------+
| root   | localhost | unix_socket |                       |

| 나의유저 |           | 아무것도 없다. |                       |
| 나의유저|            |아무것도 없다.  |                       |
+--------+-----------+-------------+-----------------------+
5 rows in set (0.00 sec)

MariaDB [mydb]>
MariaDB [mydb]> quit
Bye

#서버 또 다운되었다. 
$ sudo mariadb
😡ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")

$ sudo service mysql start
^C
$ sudo mariadb
Welcome to the MariaDB monitor.  

# 📌mydb가 아닌 mysql 접속 필요!!!
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> update user set plugin='mysql_native_password' where user='myuser';
Query OK, 2 rows affected (0.00 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [mysql]> SELECT user,host,plugin,authentication_string FROM mysql.user;
+--------+-----------+-----------------------+-----------------------+
| user   | host      | plugin                | authentication_string |
+--------+-----------+-----------------------+-----------------------+
| root   | localhost | unix_socket           |                      |

|나의 유저 |          | mysql_native_password |                       |
|나의 유저 |          | mysql_native_password |                       |
+--------+-----------+-----------------------+-----------------------+
5 rows in set (0.00 sec)

MariaDB [mysql]> set password = password("비번");
Query OK, 0 rows affected, 1 warning (0.00 sec)

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> quit
Bye

$ sudo mariadb
Welcome to the MariaDB monitor. 

MariaDB [(none)]> use mysql;
MariaDB [mysql]> update user set plugin='mysql_native_password' where user='root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [mysql]>
MariaDB [mysql]> set password = password("비번");

MariaDB [mysql]> flush privileges;

MariaDB [mysql]> quit
Bye

#플러그인을 변경해서 아래처럼 접속이 안된다. 
$ sudo mariadb;
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#이렇게 접속해야됨
$ sudo mysql -u root -p
Enter password: