Ubuntu MariaDB 외부 접속 허용

IOVEIT·2023년 10월 1일
0

1. 사용자 추가 시, '%'으로 추가

$ mariadb -uroot -p

MariaDB [(none)]> CREATE USER 'your_id'@'%' IDENTIFIED BY 'your_password';

호스트 IP를 수정하려면!

MariaDB [(none)]> use mysql
MariaDB [(mysql)]> select host, user, password from user;
+-----------+-------------+---------------+
| Host      | User        | Password      |
+-----------+-------------+---------------+
| localhost | sendy      | *A25BB73F1D9   |
+-----------+-------------+---------------+

* 업데이트하면 에러 발생함
> update mysql.user set Host = 'newIP' where Host = 'oldIP';
ERROR 1356 (HY000): View 'mysql.user' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights 


MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 
                  'sendy'@'192.168.%' IDENTIFIED BY '' WITH GRANT OPTION;
MariaDB [(mysql)]> select host, user, password from user;
+-----------+-------------+---------------+
| Host      | User        | Password      |
+-----------+-------------+---------------+
| localhost | sendy      | *A25BB73F1D9   |
| 192.168.% | sendy      | *A25BB73F1D9   |
+-----------+-------------+---------------+

2. conf 파일에 bind-address 수정

$ sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf

# Instead of skip-networking the default is now to listen only on
# localhost which is more compatible and is not less secure.
# bind-address            = 127.0.0.1 # 주석처리
bind-address            = 0.0.0.0
 
$ service mysql restart

3. 방화벽에서 포트 허용

$ sudo ufw allow 3306/tcp

* 위와 같이 설정하지 않고, 다른 PC에서 서버에 접속하면 오류 발생 (대상 컴퓨터에서 연결을 거부했으므로 연결하지 못했습니다.)

Traceback (most recent call last):
  File "C:\eduler\Lib\site-packages\pymysql\connections.py", line 644, in connect
    sock = socket.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "..Lib\socket.py", line 851, in create_connection
    raise exceptions[0]
  File "..Lib\socket.py", line 836, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [WinError 10061] 대상 컴퓨터에서 연결을 거부했으므로 연결하지 못했습니다
 .
 .
 .
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2003, "Can't connect to MySQL server on '172.10.10.10' ([WinError 10061] 
대상 컴퓨터에서 연결을 거부했으므로 연결하지 못했습니다)")
"ㅤ"
profile
EnCoCookLand

0개의 댓글