OS/Linux

MariaDB 설치 - 원격 접속 설정

파란크리스마스 2019. 1. 26. 13:07
728x90

출처

MariaDB 설치

$ sudo apt install mariadb-server

MariaDB 초기화 - 권한 설정(mysql_secure_installation)

$ sudo mysql_secure_installation
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none): 

root 암호 설정 여부

OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 
Set root password? [Y/n] 

root 암호 설정

New password: mariadb!
Re-enter new password: mariadb! 
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] 

root 원격 접속 여부

 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] 

test 데이터베이스 삭제 여부

 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] 

지금까지 설정한 권한 정보 적용 여부

 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] 

설정 완료

 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
 
Thanks for using MariaDB!

원격 접속 허용 하기 (환경파일 : /etc/mysql/my.cnf 수정)

$ sudo vi /etc/mysql/my.cnf

내용추가

[mysqld]    
bind-address = 0.0.0.0

/etc/mysql/my.cnf 전체

# The MariaDB configuration file
#
# The MariaDB/MySQL tools read configuration files in the following order:
# 1. "/etc/mysql/mariadb.cnf" (this file) to set global defaults,
# 2. "/etc/mysql/conf.d/*.cnf" to set global options.
# 3. "/etc/mysql/mariadb.conf.d/*.cnf" to set MariaDB-only options.
# 4. "~/.my.cnf" to set user-specific options.
#
# If the same option is defined multiple times, the last one will apply.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.

#
# This group is read both both by the client and the server
# use it for options that affect everything
#
[client-server]

# Import all .cnf files from configuration directory
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mariadb.conf.d/

[mysqld]
bind-address = 0.0.0.0

서비스 등록

$ sudo systemctl enable mariadb
$ sudo systemctl start mariadb
$ sudo systemctl status mariadb
● mariadb.service - MariaDB 10.1.34 database server
   Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-01-26 03:22:15 UTC; 7min ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 1113 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 27 (limit: 855)
   CGroup: /system.slice/mariadb.service
           └─1113 /usr/sbin/mysqld
 
Jan 26 03:22:10 orangepizero systemd[1]: Starting MariaDB 10.1.34 database server...
Jan 26 03:22:13 orangepizero mysqld[1113]: 2019-01-26  3:22:13 3069987648 [Note] /usr/sbin/mysqld (mysqld 10.1.3
Jan 26 03:22:15 orangepizero systemd[1]: Started MariaDB 10.1.34 database server.
Jan 26 03:22:15 orangepizero /etc/mysql/debian-start[1351]: /usr/bin/mysql_upgrade: the '--basedir' option is al
Jan 26 03:22:15 orangepizero /etc/mysql/debian-start[1351]: Looking for 'mysql' as: /usr/bin/mysql
Jan 26 03:22:15 orangepizero /etc/mysql/debian-start[1351]: Looking for 'mysqlcheck' as: /usr/bin/mysqlcheck
Jan 26 03:22:15 orangepizero /etc/mysql/debian-start[1351]: This installation of MySQL is already upgraded to 10
Jan 26 03:22:15 orangepizero /etc/mysql/debian-start[1362]: Checking for insecure root accounts.
Jan 26 03:22:15 orangepizero /etc/mysql/debian-start[1366]: Triggering myisam-recover for all MyISAM tables and 
pi@orangepizero:~$ netstat -tnlp | grep 3306

MariaDB 포트 확인

$ netstat -tnlp | grep 3306
(Not all processes could be identified, non-owned process info
 will not be shown, you would have to be root to see it all.)
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      -                   

MariaDB 접속

$ su -
Password: 
 
# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 30
Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> 

사용자 추가

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 54
Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> create user 'user1'@'%' identified by 'user1!!';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> grant all privileges on *.* to 'user1'@'%' with grant option;
Query OK, 0 rows affected (0.01 sec)
 
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> exit
Bye

user1 접속

$ mysql -u user1 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 36
Server version: 10.1.34-MariaDB-0ubuntu0.18.04.1 Ubuntu 18.04
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> 

사용자 목록 조회

MariaDB [(none)]> select user, host from mysql.user;
+-------+-----------+
| user  | host      |
+-------+-----------+
| user1 | %         |
| root  | localhost |
| user1 | localhost |
+-------+-----------+
3 rows in set (0.00 sec)