티스토리 뷰

 


[ MySQL 5.7 설치 ]

  • Yum을 통한 MySQL 설치를 위해 rpm 파일 다운로드
$ rpm -ivh https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm

 

  • MySQL 5.7 설치
$ yum install -y mysql-community-server mysql-community-devel

 

  • MySQL 서비스 시작
$ systemctl start mysqld

 

  • MySQL 초기 설정 시작
# 초기 설정 시작 전 초기 암호 획득
$ grep 'password' /var/log/mysqld.log
2020-02-13T13:48:47.145695Z 1 [Note] A temporary password is generated for root@localhost: sFrZ),rk-3ar

# 초기 설정 시작
mysql_secure_installation

----------------------------------------------------------------------------------
Securing the MySQL server deployment.

Enter password for user root:        

The existing password for the user account root has expired. Please set a new password.

New password:

Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL 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? (Press y|Y for Yes, any other key for No) : y
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? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL 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? (Press y|Y for Yes, any other key for No) : y
 - 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? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
----------------------------------------------------------------------------------

 

  • MySQL UTF8 설정 및 서비스 재시작
$ vi /etc/my.cnf

-------------------------------------------------------------------------------------
[client]
default-character-set=utf8
 
[mysql]
default-character-set=utf8
 
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

-------------------------------------------------------------------------------------

$ systemctl restart mysqld

 


[ Django와 연동을 위한 Database 생성 및 설정 ]

$ mysql -u root -p

....
# Database 생성
mysql> create database [데이터베이스명];


# 계정 생성
mysql> create user '[계정명]'@'%' identified by '[비밀번호]';
Query OK, 0 rows affected (0.00 sec)


# 생성된 데이터베이스에 접근권한 부여
mysql> grant all privileges on [데이터베이스명].* to '[계정명]'@'%';
Query OK, 0 rows affected (0.00 sec)

# 변경사항 저장 및 반영
mysql> flush privileges;

 


[ Django Setting.py 데이터베이스 설정 ]

  • Django와 MySQL 연결을 위해 mysqlclient 설치
$ yum -y install gcc
$ pip install mysqlclient

 

  • Django settings.py Database 설정
$ vi /opt/todo/todo/settings.py

----------------------------------------------------------------------------------
...

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': '[데이터베이스 이름]',
        'USER': '[데이터베이스 연결 시 사용할 계정]',
        'PASSWORD': '[계정 비밀번호]',
        'HOST': '127.0.0.1',
        'PORT': '3306',
        'OPTIONS': {
            'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
            'charset': 'utf8mb4',
        },
    }
}

...
----------------------------------------------------------------------------------

 

  • ./manage.py makemigrations & migrate를 통한 Django Database 반영
    - makemigrations은 Django의 models.py에 작성된 모델 테이블을 SQL 명령어로 변환하여 Migration 파일을 만듬
    - migrate는 makemigrations을 통해 변경된 모델 테이블을 데이터베이스에 반영하는 작업을 함
$ ./manage.py makemigrations
No changes detected

$ ./manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying auth.0009_alter_user_last_name_max_length... OK
  Applying auth.0010_alter_group_name_max_length... OK
  Applying auth.0011_update_proxy_permissions... OK
  Applying sessions.0001_initial... OK

 

  • MySQL에 생성된 Django Database 및 Table 확인
mysql> show tables;
+----------------------------+
| Tables_in_todo             |
+----------------------------+
| auth_group                 |
| auth_group_permissions     |
| auth_permission            |
| auth_user                  |
| auth_user_groups           |
| auth_user_user_permissions |
| django_admin_log           |
| django_content_type        |
| django_migrations          |
| django_session             |
+----------------------------+
10 rows in set (0.00 sec)

 

 

Comments
최근에 올라온 글