MySQL - Sizuha/devdog GitHub Wiki

PATH ์„ค์ •

๋‹ค์Œ ๊ฒฝ๋กœ๋ฅผ PATH์— ์ถ”๊ฐ€.

/usr/local/mysql/bin

MySQL์„ UTF-8๋กœ ์„ค์ •=

์šฐ์„  /etc/my.cnf๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

/usr/local/mysql/support-files/ ๊ฒฝ๋กœ์—์„œ my-huge.cnf, my-large.cnf, my-medium.cnf, my-small.cnf ํŒŒ์ผ ์ค‘ ํ•˜๋‚˜๋ฅผ /etc/my.cnf๋กœ ๋ณต์‚ฌ.

sudo cp /usr/local/mysql/support-files/my-huge.cnf /etc/my.cnf

/etc/my.cnf ๋‚ด์šฉ์— ๋‹ค์Œ ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€/์ˆ˜์ •.

[mysqld]
character-set-server=utf8

[mysql]
default-character-set=utf8

์„ค์ • ์ €์žฅ ํ›„, MySQL ์žฌ์‹œ์ž‘. ์„ค์ • ํ™•์ธ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํ•œ๋‹ค.

mysql> show variables like "%character%";
mysql> show variables like "%collation%";

DB ์„œ๋ฒ„ ์‹œ์ž‘

sudo /usr/local/mysql/support-files/mysql.server start

๊ด€๋ฆฌ

User ๊ด€๋ฆฌ

$ mysql -u root -pPASSWORD
mysql> use mysql;

์‚ฌ์šฉ์ž ์ถ”๊ฐ€

๋‚ด๋ถ€์—์„œ ์ ‘์†ํ•˜๋Š” ์‚ฌ์šฉ์ž

mysql> insert into user (host, user, password) values ('localhost', 'dbuser', password('xxxxxxxx'));

์™ธ๋ถ€์—์„œ ์ ‘์†ํ•˜๋Š” ์‚ฌ์šฉ์ž

mysql> insert into user (host, user, password) values ('%', 'dbuser', password('xxxxxxxx'));

๊ถŒํ•œ ์„ค์ •

๊ถŒํ•œ ๋ณด๊ธฐ
mysql> show grants for USER@'%';
๊ถŒํ•œ ์ง€์ •
mysql> grant select, insert, update, delete on DB_Name.TableName to USER@'%';

์™€์ผ๋“œ ์นด๋“œ ์‚ฌ์šฉ:

mysql> grant select, insert, update, delete on DB_Name.* to USER@'%';

root ์•”ํ˜ธ ์„ค์ •

mysqladmin ์ด์šฉ

$ mysqladmin -u root -p password

update๋ฌธ ์ด์šฉ

$ mysql -u root 
mysql> update user set password = password('์ƒˆ๋กœ์šด_์•”ํ˜ธ') where user = 'root';
mysql> flush privileges;

MySQL 5.7 ์ด์ƒ์ธ ๊ฒฝ์šฐ,

$ mysql -u root 
mysql> UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE User='root';
mysql> FLUSH PRIVILEGES;

set password๋ฅผ ์ด์šฉ

mysql> set password for root = password('์ƒˆ๋กœ์šด_์•”ํ˜ธ');

root ์•”ํ˜ธ ๋ฆฌ์…‹

$ sudo /usr/local/mysql/support-files/mysql.server stop
$ sudo /usr/local/mysql/bin/mysqld_safe --skip-grant-tables

์ดํ›„ ๋‹ค๋ฅธ ํ„ฐ๋ฏธ๋„ ์ฐฝ์—์„œ,

$ mysql -u root

update๋ฌธ์„ ์ด์šฉํ•˜์—ฌ root ์•”ํ˜ธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

MySQL์„ ๋‹ค์‹œ ์‹œ์ž‘.

$ sudo /usr/local/mysql/support-files/mysql.server start

์ดํ›„์—, mysql ์ฝ˜์†”์—์„œ ๋งŒ์•ฝ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์—๋Ÿฌ๊ฐ€ ๋‚˜์˜จ๋‹ค๋ฉด...

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์•”ํ˜ธ๋ฅผ ๋‹ค์‹œ ์„ค์ •ํ•ด ์ค€๋‹ค.

mysql> SET PASSWORD = PASSWORD('your_new_password');

ํ…Œ์ŠคํŠธ

$ mysql -u root -p

๋กœ๊ทธ์ธ

$ mysql -u ๊ณ„์ •์ด๋ฆ„(ex. root) -p

DB ๊ด€๋ฆฌ

DB ๋ณด๊ธฐ

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋ชฉ๋ก ํ‘œ์‹œ.

mysql> show databases;

DB ์—ด๊ธฐ.

mysql> use DB์ด๋ฆ„;

DB ์ƒ์„ฑ

mysql> create database DB์ด๋ฆ„;

๋ฌธ์ž์—ด ์ธ์ฝ”๋”ฉ์„ utf-8์„ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋Š” DB ์ƒ์„ฑ.

mysql> CREATE DATABASE <dbname> CHARACTER SET utf8;

Dump

DB ๋ฐฑ์—…

$ mysqldump -u {user} -p {database-name} > {dump-output}.sql

DB ๋ณต์›

$ mysql -u {user} -p {database-name} < {dump-output}.sql

Table ๊ด€๋ฆฌ

ํ…Œ์ด๋ธ” ๊ตฌ์กฐ ๋ณด๊ธฐ

mysql> DESC ํ…Œ์ด๋ธ”์ด๋ฆ„;
mysql> DESCRIBE ํ…Œ์ด๋ธ”์ด๋ฆ„;
mysql> EXPLAIN ํ…Œ์ด๋ธ”์ด๋ฆ„;

SQL

INSERT

INSERT  INTO  ใƒ†ใƒผใƒ–ใƒซๅ  VALUES (โ€˜ๅ€ค1โ€ฒ [ , โ€˜ๅ€ค2โ€™ ]ใƒปใƒปใƒป);

UPDATE

UPDATE ใƒ†ใƒผใƒ–ใƒซๅ SET ใƒ•ใ‚ฃใƒซใƒ‰=ๅ€ค[, ...] WHERE ๆกไปถ;

DELETE

ํ…Œ์ด๋ธ”์—์„œ ์กฐ๊ฑด์— ๋งž๋Š” ํ–‰์„ ์‚ญ์ œํ•œ๋‹ค.

DELETE FROM ใƒ†ใƒผใƒ–ใƒซๅ WHERE ๆกไปถ;

WHERE์ ˆ์„ ์ƒ๋žตํ•˜๋ฉด ํ…Œ์ด๋ธ” ์•ˆ์˜ ๋ชจ๋“  ํ–‰์ด ์‚ญ์ œ๋œ๋‹ค.

MySQL: ํ•จ์ˆ˜

๋‚ ์งœ/์‹œ๊ฐ„

  • now() : ํ˜„์žฌ ๋‚ ์งœ์™€ ์‹œ๊ฐ„
  • date() : ๋‚ ์งœ ๋ถ€๋ถ„๋งŒ ์ถ”์ถœํ•œ๋‹ค
Nam Description
ADDDATE() Add dates
ADDTIME() Add time
CONVERT_TZ() Convert from one timezone to another
CURDATE() Return the current date
CURRENT_DATE(), CURRENT_DATE Synonyms for CURDATE()
CURRENT_TIME(), CURRENT_TIME Synonyms for CURTIME()
CURRENT_TIMESTAMP(), CURRENT_TIMESTAMP Synonyms for NOW()
CURTIME() Return the current time
DATE_ADD() Add two dates
DATE_FORMAT() Format date as specified
DATE_SUB() Subtract two dates
DATE() Extract the date part of a date or datetime expression
DATEDIFF() Subtract two dates
DAY() Synonym for DAYOFMONTH()
DAYNAME() Return the name of the weekday
DAYOFMONTH() Return the day of the month (1-31)
DAYOFWEEK() Return the weekday index of the argument
DAYOFYEAR() Return the day of the year (1-366)
EXTRACT Extract part of a date
FROM_DAYS() Convert a day number to a date
FROM_UNIXTIME() Format date as a UNIX timestamp
HOUR() Extract the hour
LAST_DAY Return the last day of the month for the argument
LOCALTIME(), LOCALTIME Synonym for NOW()
LOCALTIMESTAMP, LOCALTIMESTAMP() Synonym for NOW()
MAKEDATE() Create a date from the year and day of year
MAKETIME MAKETIME()
MICROSECOND() Return the microseconds from argument
MINUTE() Return the minute from the argument
MONTH() Return the month from the date passed
MONTHNAME() Return the name of the month
NOW() Return the current date and time
PERIOD_ADD() Add a period to a year-month
PERIOD_DIFF() Return the number of months between periods
QUARTER() Return the quarter from a date argument
SEC_TO_TIME() Converts seconds to 'HH:MM:SS' format
SECOND() Return the second (0-59)
STR_TO_DATE() Convert a string to a date
SUBDATE() When invoked with three arguments a synonym for DATE_SUB()
SUBTIME() Subtract times
SYSDATE() Return the time at which the function executes
TIME_FORMAT() Format as time
TIME_TO_SEC() Return the argument converted to seconds
TIME() Extract the time portion of the expression passed
TIMEDIFF() Subtract time
TIMESTAMP() With a single argument, this function returns the date or datetime expression. With two arguments, the sum of the arguments
TIMESTAMPADD() Add an interval to a datetime expression
TIMESTAMPDIFF() Subtract an interval from a datetime expression
TO_DAYS() Return the date argument converted to days
UNIX_TIMESTAMP() Return a UNIX timestamp
UTC_DATE() Return the current UTC date
UTC_TIME() Return the current UTC time
UTC_TIMESTAMP() Return the current UTC date and time
WEEK() Return the week number
WEEKDAY() Return the weekday index
WEEKOFYEAR() Return the calendar week of the date (1-53)
YEAR() Return the year
YEARWEEK() Return the year and week
โš ๏ธ **GitHub.com Fallback** โš ๏ธ