리눅스(CentOS) Mysql 설치 및 UTF-8 설정하기, 한글깨짐 현상 해결
리눅스 설치 및 UTF-8 설정하기
[root@localhost local]# yum -y install mysql*
yum 명령어를 통해 mysql을 인스톨 합니다.
설치가 완료 되었다면 Mysql 데몬을 실행합니다.
[root@localhost local]# service mysqld start
MySQL 데이타베이스 초기화 중: Installing MySQL system tables...
OK
Filling help tables...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h localhost.localdomain password 'new-password'
Alternatively you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd /usr/mysql-test ; perl mysql-test-run.pl
Please report any problems with the /usr/bin/mysqlbug script!
[ OK ]
mysqld (을)를 시작 중: [ OK ]
데몬이 정상적으로 실행 되었습니다.
Mysql 접속 테스트를 진행합니다.
※ 초기 root계정의 패스워드는 없으므로 Enter password부분은 그냥 Enter키를 눌러서 넘겨주도록 합니다.
[root@localhost local]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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>
접속이 되었다면, Mysql의 인코딩 타입을 확인합니다.
mysql> show variables like 'c%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | latin1_swedish_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
| completion_type | 0 |
| concurrent_insert | 1 |
| connect_timeout | 10 |
+--------------------------+----------------------------+
14 rows in set (0.00 sec)
mysql>
인코딩 확인 결과 character_set이 latin 1로 설정 되어 있습니다.
이럴 경우 한글깨짐 현상이 발생하므로 utf-8 타입으로 변경해주도록 하겠습니다.
리눅스 환경에서는 my.cnf 파일을 수정해주시면 되고, 윈도우 환경에서는 my.ini 파일을 수정하시면 되겠습니다.
리눅스에서 my.cnf 파일은 /etc/ 하위 경로에 있습니다.
[root@localhost local]# vi /etc/my.cnf
[client]
#추가
default-character-set = utf8
[mysqld]
#추가
init_connect="SET collation_connection = utf8_general_ci"
init_connect="SET NAMES utf8"
default-character-set = utf8
character-set-server = utf8
collation-server = utf8_general_ci
[mysqldump]
#추가
default-character-set = utf8
[mysql]
#추가
default-character-set = utf8
인코딩 설정 변경 후 데몬을 재시작 합니다.
[root@localhost local]# service mysqld restart
mysqld 를 정지 중: [ OK ]
mysqld (을)를 시작 중: [ OK ]
[root@localhost local]#
[root@localhost local]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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> show variables like 'c%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
| collation_connection | utf8_general_ci |
| collation_database | utf8_general_ci |
| collation_server | utf8_general_ci |
| completion_type | 0 |
| concurrent_insert | 1 |
| connect_timeout | 10 |
+--------------------------+----------------------------+
14 rows in set (0.00 sec)
mysql>
character_set 인코딩 타입이 utf8 설정으로 변경된 것을 확인할 수 있습니다.