<현상황 오류들>

 

- mysql 접속 

-> 소켓 오류

(django_site) ubuntu@ip-172-31-11-144:~$ sudo mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

- mysql 실행

-> 제어 프로세스가 오류 코드와 함께 종료되었기 때문에 mysql.service에 대한 작업이 실패

(django_site) ubuntu@ip-172-31-11-144:~/django_site$ sudo systemctl start mysql
Job for mysql.service failed because the control process exited with error code.
See "systemctl status mysql.service" and "journalctl -xe" for details.

-> 에러 로그 확인해보기

MySQL의 경우 디폴트로 에러 로그는 /var/log 디렉토리 밑에 mysqld.log 파일에 기록된다구 함

(django_site) ubuntu@ip-172-31-11-144:~$ cd /var/log
(django_site) ubuntu@ip-172-31-11-144:/var/log$ ls
alternatives.log    cloud-init-output.log  journal        syslog                        ubuntu-advantage.log
alternatives.log.1  cloud-init.log         kern.log       syslog.1                      unattended-upgrades
amazon              dist-upgrade           kern.log.1     syslog.2.gz                   vsftpd.log
apt                 dmesg                  kern.log.2.gz  syslog.3.gz                   vsftpd.log.1
auth.log            dmesg.0                kern.log.3.gz  syslog.4.gz                   vsftpd.log.2
auth.log.1          dmesg.1.gz             landscape      syslog.5.gz                   vsftpd.log.3
auth.log.2.gz       dmesg.2.gz             lastlog        syslog.6.gz                   wtmp
auth.log.3.gz       dmesg.3.gz             mysql #요깅     syslog.7.gz
btmp                dpkg.log               nginx          ubuntu-advantage-timer.log
btmp.1              dpkg.log.1             private        ubuntu-advantage-timer.log.1
(django_site) ubuntu@ip-172-31-11-144:/var/log$ cd mysql
(django_site) ubuntu@ip-172-31-11-144:/var/log/mysql$ ls
error.log  error.log.1.gz
(django_site) ubuntu@ip-172-31-11-144:/var/log/mysql$ cd error.log
-bash: cd: error.log: Not a directory
(django_site) ubuntu@ip-172-31-11-144:/var/log/mysql$ sudo nano error.log

 

2022-05-13T06:52:40.495024Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.29-0ubuntu0.20.04.3) starting as process 5592
2022-05-13T06:52:40.535334Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2022-05-13T06:52:40.535452Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2022-05-13T06:52:40.535516Z 0 [ERROR] [MY-010119] [Server] Aborting
2022-05-13T06:52:40.536454Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.29-0ubuntu0.20.04.3)  (Ubuntu).

1.  < [System] /usr/sbin/mysqld (mysqld 8.0.29-0ubuntu0.20.04.3) starting as process 5592 > 

들어갔다가 건들면 안될 거 같아서 빠꾸 . .

(django_site) ubuntu@ip-172-31-11-144:/usr/sbin$ cd mysqld
-bash: cd: mysqld: Not a directory
(django_site) ubuntu@ip-172-31-11-144:/usr/sbin$ sudo nano mysqld


Use "fg" to return to nano.

[1]+  Stopped                 sudo nano mysqld

2. < [ERROR] Failed to find valid data directory > 올바른 데이터 디렉토리를 찾지 못했습니다.

이 이슈에 대해선 여러 블로그 포스팅이 되어있는데 몇 포스트에서 초기화할 때 --initialize-insecure 해서 비밀번호가 없기 때문이라 함

 

그래서 한 번 비밀번호 없이 들어가면

(django_site) ubuntu@ip-172-31-11-144:~$ mysql -u root -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

웅 아님

 

구글링 해보니까

이유가 '/var/run/mysqld/mysqld.sock' 이 없어서 발생하는 문제, 어딘가에 있을 mysql.sock를 '/var/run/mysqld/mysqld.sock' 과 연결해주면 해결된다는데 . . .

일단 출처 -> https://lhr0419.medium.com/cant-connect-to-local-mysql-server-through-socket-var-run-mysqld-mysqld-sock-2-%ED%95%B4%EA%B2%B0%ED%95%B4%EB%B3%B4%EC%95%84%EC%9A%94-1d94a9bc2618

 

1. sudo mkdir /var/run/mysqld 

생성

 

2. sudo find / -name /mysql.sock 

(django_site) ubuntu@ip-172-31-11-144:~$ sudo find / -name /mysql.sock
find: warning: ‘-name’ matches against basenames only, but the given pattern contains a directory separator (‘/’), thus the expression will evaluate to false all the time.  Did you mean ‘-wholename’?

당황스럽당

 

2-1.  (mac) No search file or directory

Linux 에서는 보통 mysql.sock 파일이 없다는 메세지로 명확하게 출력해주지만 맥 OS에서 다른 메세지는 없이 파일과 디렉토리를 찾을 수 없다는 메세지만 출력된다구 함

오류 해결

> sudo mkdir /var/mysql

> sudo chmod 755 /var/mysql

> sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

(django_site) ubuntu@ip-172-31-11-144:~$ sudo mkdir /var/mysql
(django_site) ubuntu@ip-172-31-11-144:~$ cd /var
(django_site) ubuntu@ip-172-31-11-144:/var$ ls
backups  cache  crash  lib  local  lock  log  mail  mysql  opt  run  snap  spool  tmp  www
(django_site) ubuntu@ip-172-31-11-144:/var$ sudo chmod 755 /var/mysql
(django_site) ubuntu@ip-172-31-11-144:/var$ sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

 

확인해보깅

(django_site) ubuntu@ip-172-31-11-144:~$ sudo find / -name mysql.sock
/var/mysql/mysql.sock

오케이 떴당

출처 -> https://m.blog.naver.com/PostView.naver?isHttpsRedirect=true&blogId=xeroad&logNo=221447336604

 

3. mysql.sock을 < /var/run/mysqld/mysqld.sock > 에 심볼릭링크로 생성

> sudo ln -s /var/mysql/mysql.sock /var/run/mysqld/mysqld.sock

(django_site) ubuntu@ip-172-31-11-144:~$ sudo ln -s /var/mysql/mysql.sock /var/run/mysqld/mysqld.sock

이렇게하면 끝 ! 

근데 mysql 재실행할 때마다 심보릭 링크를 걸어주어야 함

간단한 방법은 my.cnf 파일을 수정하는 것

 

4. 마무리 !

# my.cnf 파일 찾기
(django_site) ubuntu@ip-172-31-11-144:~$ sudo find / -name my.cnf
/etc/alternatives/my.cnf
/etc/mysql/my.cnf
/var/lib/dpkg/alternatives/my.cnf

3군데가 나오는데 무엇을 수정해야할까 . . . .

블로그에서 나오는 건 ... /path/to/my.cnf 인데 후 . . .

 

4-1. 

(django_site) ubuntu@ip-172-31-11-144:~$ sudo nano /etc/alternatives/my.cnf

4-2.

(django_site) ubuntu@ip-172-31-11-144:~$ sudo nano /etc/mysql/my.cnf

4-3.

(django_site) ubuntu@ip-172-31-11-144:~$ sudo nano /var/lib/dpkg/alternatives/my.cnf

4-4. vi /path/to/my.cnf

 

- mysql 설정파일의 우선 순위

MySQL을 실행시킬 때 my.cnf라는 이름의 설정 파일을 사용하게 되는데, MySQL에서는 my.cnf는 여러 곳에 위치가 가능하며, 여러 경로에서 가장 먼저 로드되는 my.cnf 파일 설정내용으로 MySQL이 설정된다

ㅊㅊ -> https://linked2ev.github.io/database/2021/06/19/MariaDB-6.-MySQL-%EC%84%A4%EC%A0%95-%ED%8C%8C%EC%9D%BC-my.cnf-%EC%9C%84%EC%B9%98/

MySQL이 my.cnf 파일 위치 확인

(django_site) ubuntu@ip-172-31-11-144:~$ mysql --help | grep my.cnf
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf

 

 

똑같잔나 엉엉

ㄷㅏ 삭제하기 

ㅊㅊ -> https://2vup.com/ubuntu-remove-mysql/

ㅊㅊ ->https://shlee0882.tistory.com/240

ㅊㅊ -> https://kig6022.tistory.com/14

1.  기존 mysql과 관련된 것들 다 삭제

sudo apt-get remove --purge mysql*
sudo apt-get autoremove
sudo apt-get autoclean

1-1. mysql-server 설치

sudo apt-get install mysql-server

1-2. 로그인하면

(django_site) ubuntu@ip-172-31-11-144:~$ mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

에러뜸

 

2. 이럴 땐 < sudo mysql -u root > 로 로그인

mysql> 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
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | auth_socket           |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

2-1. 현재 root 계정의 plugin이 auth_socket이므로 mysql_native_password로 update 시켜준다.

mysql> USE mysql;
Database changed
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

(django_site) ubuntu@ip-172-31-11-144:~$ service mysql restart

2-2. root plugin 확인하기

(django_site) ubuntu@ip-172-31-11-144:~$ sudo mysql -u root

mysql> 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
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| debian-sys-maint | localhost | caching_sha2_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
5 rows in set (0.00 sec)

그럼 이제 < mysql -u root -p > 로 접속 가능

3. 슈퍼유저 만들기

3-1. mysql를 root 권한으로 접속하기

(django_site) ubuntu@ip-172-31-11-144:~$ mysql -u root -p

3-2. mysql 사용자로 접속 후 

mysql> 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
mysql> CREATE DATABASE django_data; #django_data 라는 database 생성
Query OK, 1 row affected (0.01 sec)

mysql> CREATE USER '아이디' identified by '비밀번호'; #생성
Query OK, 0 rows affected (0.01 sec)
# 사용자 권한 부여
mysql> GRANT ALL PRIVILEGES ON *.* TO 'jhj'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> EXIT;
Bye
(django_site) ubuntu@ip-172-31-11-144:~$ mysql -u jhj -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 8.0.29-0ubuntu0.20.04.3 (Ubuntu)

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

(django_site) ubuntu@ip-172-31-11-144:~$ mysql -u jhj -p
Enter password:
ERROR 1045 (28000): Access denied for user 'jhj'@'localhost' (using password: YES)
# 비밀번호 잘못 입력시 에러뜨면 성공