insert into user (Host,User,authentication_string,ssl_cipher,x509_issuer,x509_subject) values ('localhost','유저명', '', '','','');
insert into user (Host,User,authentication_string,ssl_cipher,x509_issuer,x509_subject) values ('localhost','유저명', '', '','','');
위와 같이 MySQL5.X버전의 경우와 같은 SQL문으로 user를 생성하지마세요.
유저가 생성되지 않습니다.
그럼 mysql에 user를 만들어 비밀번호를 반영하여 접속하는 방법을 설명하겠습니다.
먼저 MySQL에 root로 접속하신 후 mysql DB 이동해 주세요.
0. MySQL에 root로 접속하여 mysql DB 로 이동해 주세요.
use mysql;
1. 사용자 정보 조회를 먼저 조회하세요.
SELECT user,authentication_string,plugin,host FROM mysql.user;
SELECT user,authentication_string,plugin,host FROM mysql.user;
그러면 위와 같이 뜨게 됩니다.
단, plugin 부분이 "caching_sha2_password" 로 되어서 암호화된 비밀번호의 형태가 다를 수 있습니다.
위의 경우에는 제가 "mysql_native_password" 로 변경한 것이기 때문입니다.
여전히 MySQL 클라이언트에서는 "caching_sha2_password" 플러그인이 반영되지 않아서 connect 가 안되기에 기존에 사용하던 "mysql_native_password" 로 변경하였기 때문입니다.
이 부분에 대해서는 아래에 변경하는 방법을 적어놨습니다.
2. 사용자 생성를 생성합니다. create user '유저명'@'localhost' identified by '비밀번호';
create user '유저명'@'localhost' identified by '비밀번호';
3. 생성된 사용자를 확인합니다. SELECT user,authentication_string,plugin,host FROM mysql.user;
SELECT user,authentication_string,plugin,host FROM mysql.user;
4. 생성된 사용자의 권한을 적용시켜 줍니다. GRANT ALL PRIVILEGES ON *.* TO '유저명'@'localhost'; GRANT GRANT OPTION ON *.* TO '유저명'@'localhost';
GRANT ALL PRIVILEGES ON *.* TO '유저명'@'localhost';
GRANT GRANT OPTION ON *.* TO '유저명'@'localhost';
5. MySQL에 실시간 반영시켜 줍니다. flush privileges;
flush privileges;
참고. MySQL에서 caching_sha2_password 을 mysql_native_password으로 플러그인을 변경하기
ALTER user '유저명'@'localhost' IDENTIFIED WITH mysql_native_password by '비밀번호';
ALTER user '유저명'@'localhost' IDENTIFIED WITH mysql_native_password by '비밀번호';
여전히 MySQL 클라이언트에서는 "caching_sha2_password" 플러그인이 반영되지 않아서 connect 가 안되기에 기존에 사용하던 "mysql_native_password" 로 변경하였기 때문입니다.
6. 사용자를 삭제할 수 있습니다. drop user 유저명@localhost;
drop user 유저명@localhost;
flush privileges;
삭제하고 나면 꼭 "flush privileges;" 를 해주세요. MySQL에 제대로 반영을 시켜줘야 합니다.
그렇지 않으면 메모리상에 USER 정보가 남아 있어 동일한 "유저명" 으로 재생성할 수 없습니다.
예를 들어 아래와 같이 SQL문중 delete 문으로 유저를 삭제하고서 다시 동일한 유저를 생성하게 되면 에러가 뜨게 됩니다.
ERROR 1396 (HY000): Operation 관련 에러가 뜨게 됩니다.
mysql> delete from user where User='유저명';
Query OK, 1 row affected (0.00 sec)
mysql> create user '유저명'@'localhost' identified by '비밀번호';
ERROR 1396 (HY000): Operation CREATE USER failed for '유저명'@'localhost'
mysql> delete from user where User='유저명';
Query OK, 1 row affected (0.00 sec)
mysql> create user '유저명'@'localhost' identified by '비밀번호';
ERROR 1396 (HY000): Operation CREATE USER failed for '유저명'@'localhost'
아래는 실제 에러가 떠서 "Drop User " 명령문으로 삭제하고 CREATE USER 명령문으로 재 생성한 화면입니다.
jaeui-MacBookPro:~ jaehonghan$ brew services start mysql
==> Successfully started `mysql` (label: homebrew.mxcl.mysql)
jaeui-MacBookPro:~ jaehonghan$ brew services list
Name Status User Plist
emacs stopped
mysql started jaehonghan /Users/jaehonghan/Library/LaunchAgents/homebrew.mxcl.mysql.plist
unbound stopped
MySQL 실행 및 ROOT 비밀번호 설정
내 환경설정 파일에 PATH를 설정해 주세요. mysql 부분을 설정하시고 저장하세요.
$ vi ~/.bash_profile
$ vi ~/.bash_profile
# mysql 설정부분
export PATH="/usr/local/opt/mysql/bin:$PATH"
export LDFLAGS="-L/usr/local/opt/mysql/lib"
export CPPFLAGS="-I/usr/local/opt/mysql/include"
:wq
시작해서 SUCCESS! 메세지가 뜨면 mysql서버에 접속해 봅니다. (비밀번호 없이 접속이 가능합니다.)
$mysql -uroot
jaeui-MacBookPro:~ jaehonghan$ mysql.server start
Starting MySQL
SUCCESS!
// Ctrl+ C 를 눌러주세요. 백그라운드로 실행되니 안심하세요.
jaeui-MacBookPro:~ jaehonghan$ mysql -uroot
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.19 Homebrew
Copyright (c) 2000, 2020, 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> exit
MySQL ROOT 비밀번호 설정
1. mysql.server start명령어로 MySQL 서버를 실행시킵니다.
2. mysql_secure_installation명령어로 MySQL ROOT비밀번호를 설정합니다.
Would you like to setup VALIDATE PASSWORD component?(비밀번호 가이드 설정에 대한 질문) yes: 복잡한 비밀번호 no: 쉬운 비밀번호 --> 쉬운 비밀번호로 세팅해주세요.
Remove anonymous users? (Press y|Y for Yes. any other key for No)(사용자 설정에 관한 질문) yes: 접속시 -u 옵션필요 no: 접속시 -u 옵션 불필요
Disallow root login remotely? (Press y|Y for Yes, any other key for No)(다른 IP에서 root 아이디로 원격접속을 설정) yes: 원격접속 불가능 no: 원격접속 가능
Remove test database and access to it? (Press y|Y for Yes, any other key for No)(테스트 데이터베이스 설정) yes: 테스트 데이터베이스 제거 no: 테스트 데이터베이스 유지
Reload privilege tables now? (Press y|Y for Yes, any other key for No)(변경된 권한을 테이블에 적용) yes: 적용 no: 미적용
$ mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?
Press y|Y for Yes, any other key for No: N
Please set the password for root here.
New password:
Re-enter new password:
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!
위와 같이 해주시면 root 비밀번호를 간단히 적용하실 수 있습니다. 특히
VALIDATE PASSWORD COMPONENT can be used to test passwords and improve security. It checks the strength of password and allows the users to set only those passwords which are secure enough. Would you like to setup VALIDATE PASSWORD component?
(VALIDATE PASSWORD COMPONENT를 사용하여 비밀번호를 테스트하고 보안을 향상시킬 수 있습니다. 이 체크는 암호의 강도와 충분히 안전한 암호를 설정하여 사용자에게 허용합니다. VALIDATE PASSWORD 구성 요소를 설정 하시겠습니까?)
이 부분에서 "No" 나 "N" 을 눌러주세요. 그렇지 않으시면 아주 긴 비밀번호를 입력해야 할 수도 있습니다.
저의 경우에는 20자 짜리 길이의 비밀번호를 입력했는데요 예상강도가 50이라고 하면서 더 복잡한 암호를 요구하였습니다.
$ brew services stop mysql
Stopping `mysql`... (might take a while)
==> Successfully stopped `mysql` (label: homebrew.mxcl.mysql)
$
$ ll /usr/local/var/mysql
total 221584
drwxr-xr-x 7 jaehonghan admin 224 3 25 21:10 .
drwxrwxr-x 4 jaehonghan admin 128 3 25 21:04 ..
-rw-r----- 1 jaehonghan admin 12582912 3 25 21:10 ibdata1
-rw-r----- 1 jaehonghan admin 50331648 3 25 21:04 ib_logfile1
-rw-r----- 1 jaehonghan admin 217 3 25 21:10 ib_buffer_pool
-rw-r----- 1 jaehonghan admin 50331648 3 25 21:10 ib_logfile0
-rw-r----- 1 jaehonghan admin 152279 3 25 21:10 jaeui-MacBookPro.local.err
$ rm -rf /usr/local/var/mysql
$ ll /usr/local/var/mysql
$ brew uninstall mysql
Uninstalling /usr/local/Cellar/mysql... (319 files, 232.3MB)
$ brew services list
Name Status User Plist
emacs stopped
unbound stopped
$ brew install mysql
==> Downloading https://homebrew.bintray.com/bottles/mysql-8.0.19.catalina.bottle.2.tar.gz
Already downloaded: /Users/jaehonghan/Library/Caches/Homebrew/downloads/16119cce36310b0d4f34d54a3a0dd701d068e946c1d4bd4c7bb409b9f9899760--mysql-8.0.19.catalina.bottle.2.tar.gz
==> Pouring mysql-8.0.19.catalina.bottle.2.tar.gz
==> /usr/local/Cellar/mysql/8.0.19/bin/mysqld --initialize-insecure --user=jaehonghan --basedir=/usr/local/Cellar/mysql/8.0.19 --datad
==> Caveats
We've installed your MySQL database without a root password. To secure it run:
mysql_secure_installation
MySQL is configured to only allow connections from localhost by default
To connect run:
mysql -uroot
To have launchd start mysql now and restart at login:
brew services start mysql
Or, if you don't want/need a background service you can just run:
mysql.server start
==> Summary
🍺 /usr/local/Cellar/mysql/8.0.19: 286 files, 289.2MB
$ brew services list
Name Status User Plist
emacs stopped
mysql stopped
unbound stopped