나만의 워드프레스를 만들어보자 Create my own wordpress(3) – maria DB 연결하기

🚀 Ubuntu 22.04에서 MariaDB 설치 및 설정하기

안녕하세요! 이번 글에서는 Ubuntu 22.04 서버에 MariaDB를 설치하고 기본 설정하는 방법을 알아보겠습니다. MariaDB는 MySQL의 대체제로, 오픈소스 관계형 데이터베이스 관리 시스템입니다.

📋 사전 준비

  • Ubuntu 22.04 LTS 서버
  • 루트 또는 sudo 권한이 있는 사용자 계정
  • 인터넷 연결

🔧 설치 과정

1. 시스템 업데이트

먼저 패키지 목록을 최신 상태로 업데이트합니다:

sudo apt update
sudo apt upgrade -y

2. MariaDB 설치

이제 MariaDB 서버와 클라이언트를 설치합니다:

sudo apt install mariadb-server mariadb-client -y

설치가 완료되면 MariaDB 서비스가 자동으로 시작됩니다. 서비스 상태를 확인해 보겠습니다:

sudo systemctl status mariadb

다음과 같은 출력을 볼 수 있으면 성공적으로 설치 및 실행된 것입니다:

● mariadb.service - MariaDB 10.3.34 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-08-14 12:34:56 UTC; 1min 12s ago

3. MariaDB 보안 설정

MariaDB의 기본 설정은 보안에 취약할 수 있으므로, 다음 명령으로 기본 보안 설정을 진행합니다:

sudo mysql_secure_installation

이 스크립트는 다음과 같은 설정을 하게 됩니다:

  1. 루트 비밀번호 설정: Enter current password for root (enter for none): [엔터 키]
  2. 비밀번호 변경 여부: Switch to unix_socket authentication [Y/n] n
  3. 루트 비밀번호 변경: Change the root password? [Y/n] Y
    • 새 비밀번호 입력 및 확인
  4. 익명 사용자 제거: Remove anonymous users? [Y/n] Y
  5. 원격 루트 로그인 비활성화: Disallow root login remotely? [Y/n] Y
  6. 테스트 데이터베이스 제거: Remove test database and access to it? [Y/n] Y
  7. 권한 테이블 리로드: Reload privilege tables now? [Y/n] Y

4. MariaDB 로그인 테스트

설정이 잘 되었는지 확인하기 위해 MariaDB에 로그인해 봅시다:

sudo mysql -u root -p

비밀번호를 입력하면 MariaDB 콘솔에 접속됩니다:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.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 DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

사용자 생성 및 권한 부여

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

데이터베이스 목록 확인

SHOW DATABASES;

MariaDB 콘솔 종료

EXIT;

⚙️ MariaDB 설정 최적화

필요에 따라 MariaDB 설정을 최적화할 수 있습니다. 주요 설정 파일은 /etc/mysql/mariadb.conf.d/50-server.cnf에 있습니다:

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

주요 설정 예시:

[mysqld]
# 기본 문자셋 설정
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# InnoDB 설정
innodb_buffer_pool_size = 256M  # 서버 RAM의 약 70%로 설정하는 것이 좋습니다
innodb_log_file_size = 64M

# 쿼리 캐시 설정
query_cache_type = 1
query_cache_size = 32M

설정을 변경한 후에는 MariaDB 서비스를 재시작해야 합니다:

sudo systemctl restart mariadb

🔒 MariaDB 외부 접속 허용 (선택사항)

기본적으로 MariaDB는 로컬 접속만 허용합니다. 외부에서 접속하려면 다음과 같이 설정합니다:

  1. 바인딩 주소 변경:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

bind-address = 127.0.0.1를 찾아 bind-address = 0.0.0.0으로 변경합니다.

  1. 사용자에게 원격 접속 권한 부여:
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
  1. 방화벽 설정:
sudo ufw allow 3306/tcp
  1. MariaDB 재시작:
sudo systemctl restart mariadb

📊 시스템 상태 모니터링

MariaDB 상태를 확인하는 명령어:

SHOW STATUS;
SHOW VARIABLES;
SHOW PROCESSLIST;

시스템 자원 사용량을 확인하는 명령어:

top
htop  # 설치 필요: sudo apt install htop

🔄 백업 및 복원

백업

sudo mysqldump -u root -p --all-databases > all_databases.sql
sudo mysqldump -u root -p mydatabase > mydatabase.sql

복원

sudo mysql -u root -p mydatabase < mydatabase.sql

🔍 문제 해결

로그 확인

MariaDB 로그는 다음 위치에서 확인할 수 있습니다:

sudo tail -f /var/log/mysql/error.log

서비스 재시작

문제 발생 시 서비스를 재시작해보세요:

sudo systemctl restart mariadb

마치며

이제 Ubuntu 22.04에 MariaDB가 성공적으로 설치되고 기본 설정이 완료되었습니다. 이 데이터베이스를 활용하여 웹 애플리케이션, CMS 또는 기타 데이터베이스 기반 프로젝트를 시작할 수 있습니다.

더 자세한 MariaDB 설정과 최적화에 대해서는 공식 MariaDB 문서를 참조하세요.

행복한 데이터베이스 관리 되세요! 🚀

🚀 Installing and Configuring MariaDB on Ubuntu 22.04

Hello there! In this guide, I’ll walk you through installing and configuring MariaDB on an Ubuntu 22.04 server. MariaDB is an open-source relational database management system that serves as a drop-in replacement for MySQL.

📋 Prerequisites

  • Ubuntu 22.04 LTS server
  • A user account with sudo privileges
  • Internet connection

🔧 Installation Process

1. Update Your System

First, let’s update the package list and upgrade existing packages:

sudo apt update
sudo apt upgrade -y

2. Install MariaDB

Now, let’s install the MariaDB server and client packages:

sudo apt install mariadb-server mariadb-client -y

After installation, the MariaDB service should start automatically. Let’s check its status:

sudo systemctl status mariadb

You should see output similar to this, indicating successful installation and running:

● mariadb.service - MariaDB 10.3.34 database server
     Loaded: loaded (/lib/systemd/system/mariadb.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-08-14 12:34:56 UTC; 1min 12s ago

3. Secure MariaDB Installation

The default MariaDB installation might have some security vulnerabilities. Let’s secure it using the mysql_secure_installation script:

sudo mysql_secure_installation

You’ll be guided through several steps:

  1. Root Password: Enter current password for root (enter for none): [Press Enter]
  2. Switch Authentication: Switch to unix_socket authentication [Y/n] n
  3. Change Root Password: Change the root password? [Y/n] Y
    • Enter and confirm new password
  4. Remove Anonymous Users: Remove anonymous users? [Y/n] Y
  5. Disallow Remote Root Login: Disallow root login remotely? [Y/n] Y
  6. Remove Test Database: Remove test database and access to it? [Y/n] Y
  7. Reload Privileges: Reload privilege tables now? [Y/n] Y

4. Test MariaDB Login

Let’s test our configuration by logging into MariaDB:

sudo mysql -u root -p

Enter your password when prompted. You should now be in the MariaDB console:

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 31
Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.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)]>

🛠️ Basic Database Management

Creating a Database

CREATE DATABASE mydatabase CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

Creating a User and Granting Privileges

CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;

Listing Databases

SHOW DATABASES;

Exiting the MariaDB Console

EXIT;

⚙️ Optimizing MariaDB Configuration

You can optimize MariaDB settings based on your needs. The main configuration file is located at /etc/mysql/mariadb.conf.d/50-server.cnf:

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

Example configuration settings:

[mysqld]
# Character set
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# InnoDB settings
innodb_buffer_pool_size = 256M  # Set to about 70% of server RAM
innodb_log_file_size = 64M

# Query cache settings
query_cache_type = 1
query_cache_size = 32M

After changing settings, restart MariaDB:

sudo systemctl restart mariadb

🔒 Allowing Remote Connections (Optional)

By default, MariaDB only allows local connections. To enable remote access:

  1. Change the binding address:
sudo nano /etc/mysql/mariadb.conf.d/50-server.cnf

Find bind-address = 127.0.0.1 and change it to bind-address = 0.0.0.0

  1. Grant remote access privileges to your user:
GRANT ALL PRIVILEGES ON mydatabase.* TO 'myuser'@'%' IDENTIFIED BY 'mypassword';
FLUSH PRIVILEGES;
  1. Configure the firewall:
sudo ufw allow 3306/tcp
  1. Restart MariaDB:
sudo systemctl restart mariadb

📊 Monitoring System Status

Commands to check MariaDB status:

SHOW STATUS;
SHOW VARIABLES;
SHOW PROCESSLIST;

Commands to check system resource usage:

top
htop  # Install with: sudo apt install htop

🔄 Backup and Restore

Creating Backups

sudo mysqldump -u root -p --all-databases > all_databases.sql
sudo mysqldump -u root -p mydatabase > mydatabase.sql

Restoring from Backups

sudo mysql -u root -p mydatabase < mydatabase.sql

🔍 Troubleshooting

Checking Logs

View MariaDB logs for troubleshooting:

sudo tail -f /var/log/mysql/error.log

Restarting the Service

If you encounter issues, try restarting the service:

sudo systemctl restart mariadb

Conclusion

You’ve now successfully installed and configured MariaDB on your Ubuntu 22.04 server! You can use this database for web applications, content management systems, or any other database-driven projects.

For more detailed MariaDB configuration and optimization, refer to the official MariaDB documentation.

Happy database management! 🚀

답글 남기기