Programing

MySQL에서 새 사용자를 만들고 하나의 데이터베이스에 대한 전체 액세스 권한을 부여합니다.

crosscheck 2020. 10. 3. 10:00
반응형

MySQL에서 새 사용자를 만들고 하나의 데이터베이스에 대한 전체 액세스 권한을 부여합니다.


MySQL에서 새 사용자를 만들고 dbTest같은 명령을 사용하여 만든 하나의 데이터베이스에만 전체 액세스 권한을 부여하고 싶습니다 create database dbTest;. 이를 수행하는 MySQL 명령은 무엇입니까?


사용자를 생성하려면 다음을 시도하십시오.

CREATE USER 'user'@'hostname';

데이터베이스에 대한 액세스 권한을 부여하려면 다음을 시도하십시오 dbTest.

GRANT ALL PRIVILEGES ON dbTest.* To 'user'@'hostname' IDENTIFIED BY 'password';

동일한 시스템에서 MySQL에 액세스하는 코드 / 사이트를 실행하는 경우 호스트 이름은 localhost입니다.

이제 고장입니다.

GRANT -사용자를 생성하고 데이터베이스, 테이블 등에 대한 권한을 부여하는 데 사용되는 명령입니다.

ALL PRIVILEGES-이것은 사용자가 모든 표준 권한을 가질 것임을 나타냅니다. 그러나 여기에는 GRANT 명령을 사용할 수있는 권한이 포함되지 않습니다.

dbtest.*-이것은 MySQL이 전체 dbtest 데이터베이스에서 사용하기 위해 이러한 권한을 적용하도록 지시합니다. 원하는 경우 *를 특정 테이블 이름으로 바꾸거나 루틴을 저장할 수 있습니다.

TO 'user'@'hostname'- 'user'는 생성중인 사용자 계정의 사용자 이름입니다. 참고 : 작은 따옴표가 있어야합니다. 'hostname'은 사용자가 연결할 수있는 호스트를 MySQL에 알려줍니다. 동일한 컴퓨터에서만 원하는 경우localhost

IDENTIFIED BY 'password' -짐작 하셨겠지만이 사용자의 비밀번호를 설정합니다.


통사론

MySQL / MariaDB 5.7.6 이상에서 사용자를 생성하려면 다음 CREATE USER구문을 사용하십시오 .

CREATE USER 'new_user'@'localhost' IDENTIFIED BY 'new_password';

다음 데이터베이스 (예 : 모든 액세스 권한을 부여하기 위해 my_db사용) GRANT구문을 , 예를 들어,

GRANT ALL ON my_db.* TO 'new_user'@'localhost';

여기서 ALL( priv_type가 ) 특정 권한 등으로 대체 될 수있는 SELECT, INSERT, UPDATE, ALTER, 등

그런 다음 새로 할당 된 권한을 다시로드하려면 다음을 실행하십시오.

FLUSH PRIVILEGES;

실행 중

위의 명령을 실행하려면 명령을 실행 mysql하고 프롬프트에 입력 한 다음 quit명령 또는 Ctrl- 로 로그 아웃해야합니다 D.

쉘에서 실행하려면 -e매개 변수를 사용 하십시오 ( SELECT 1위 명령 중 하나로 대체하십시오 ).

$ mysql -e "SELECT 1"

또는 표준 입력에서 문을 인쇄하십시오.

$ echo "FOO STATEMENT" | mysql

당신이 가지고있는 경우에 액세스가 거부 위로 지정 -u(사용자) 및 -p매개 변수 (암호), 또는 장기에 대한 액세스는 귀하의 자격 증명을 설정 ~/.my.cnf, 예를 들어,

[client]
user=root
password=root

쉘 통합

MySQL 구문에 익숙하지 않은 사람들을 위해 기억하고 사용하기 쉬운 편리한 쉘 함수가 있습니다 (사용하려면 더 아래에 포함 된 쉘 함수를로드해야합니다).

예를 들면 다음과 같습니다.

$ mysql-create-user admin mypass
| CREATE USER 'admin'@'localhost' IDENTIFIED BY 'mypass'

$ mysql-create-db foo
| CREATE DATABASE IF NOT EXISTS foo

$ mysql-grant-db admin foo
| GRANT ALL ON foo.* TO 'admin'@'localhost'
| FLUSH PRIVILEGES

$ mysql-show-grants admin
| SHOW GRANTS FOR 'admin'@'localhost'
| Grants for admin@localhost                                                                                   
| GRANT USAGE ON *.* TO 'admin'@'localhost' IDENTIFIED BY PASSWORD '*6C8989366EAF75BB670AD8EA7A7FC1176A95CEF4' |
| GRANT ALL PRIVILEGES ON `foo`.* TO 'admin'@'localhost'

$ mysql-drop-user admin
| DROP USER 'admin'@'localhost'

$ mysql-drop-db foo
| DROP DATABASE IF EXISTS foo

To use above commands, you need to copy&paste the following functions into your rc file (e.g. .bash_profile) and reload your shell or source the file. In this case just type source .bash_profile:

# Create user in MySQL/MariaDB.
mysql-create-user() {
  [ -z "$2" ] && { echo "Usage: mysql-create-user (user) (password)"; return; }
  mysql -ve "CREATE USER '$1'@'localhost' IDENTIFIED BY '$2'"
}

# Delete user from MySQL/MariaDB
mysql-drop-user() {
  [ -z "$1" ] && { echo "Usage: mysql-drop-user (user)"; return; }
  mysql -ve "DROP USER '$1'@'localhost';"
}

# Create new database in MySQL/MariaDB.
mysql-create-db() {
  [ -z "$1" ] && { echo "Usage: mysql-create-db (db_name)"; return; }
  mysql -ve "CREATE DATABASE IF NOT EXISTS $1"
}

# Drop database in MySQL/MariaDB.
mysql-drop-db() {
  [ -z "$1" ] && { echo "Usage: mysql-drop-db (db_name)"; return; }
  mysql -ve "DROP DATABASE IF EXISTS $1"
}

# Grant all permissions for user for given database.
mysql-grant-db() {
  [ -z "$2" ] && { echo "Usage: mysql-grand-db (user) (database)"; return; }
  mysql -ve "GRANT ALL ON $2.* TO '$1'@'localhost'"
  mysql -ve "FLUSH PRIVILEGES"
}

# Show current user permissions.
mysql-show-grants() {
  [ -z "$1" ] && { echo "Usage: mysql-show-grants (user)"; return; }
  mysql -ve "SHOW GRANTS FOR '$1'@'localhost'"
}

Note: If you prefer to not leave trace (such as passwords) in your Bash history, check: How to prevent commands to show up in bash history?


To create a user and grant all privileges on a database.

Log in to MySQL:

mysql -u root

Now create and grant

GRANT ALL PRIVILEGES ON dbTest.* To 'user'@'hostname' IDENTIFIED BY 'password';

Anonymous user (for local testing only)

Alternately, if you just want to grant full unrestricted access to a database (e.g. on your local machine for a test instance, you can grant access to the anonymous user, like so:

GRANT ALL PRIVILEGES ON dbTest.* To ''@'hostname'

Be aware

This is fine for junk data in development. Don't do this with anything you care about.


$ mysql -u root -p -e "grant all privileges on dbTest.* to
`{user}`@`{host}` identified by '{long-password}'; flush privileges;"

ignore -p option, if mysql user has no password or just press "[Enter]" button to by-pass. strings surrounded with curly braces need to replaced with actual values.


You can create new users using the CREATE USER statement, and give rights to them using GRANT.


To me this worked.

CREATE USER 'spowner'@'localhost' IDENTIFIED BY '1234'; 
GRANT ALL PRIVILEGES ON test.* To 'spowner'@'localhost'; 
FLUSH PRIVILEGES;

where

  • spowner : user name
  • 1234 : password of spowner
  • test : database 'spowner' has access right to

The below command will work if you want create a new user give him all the access to a specific database(not all databases in your Mysql) on your localhost.

GRANT ALL PRIVILEGES ON test_database.* TO 'user'@'localhost' IDENTIFIED BY 'password';

This will grant all privileges to one database test_database (in your case dbTest) to that user on localhost.

Check what permissions that above command issued to that user by running the below command.

SHOW GRANTS FOR 'user'@'localhost'

Just in case, if you want to limit the user access to only one single table

GRANT ALL ON mydb.table_name TO 'someuser'@'host';

참고URL : https://stackoverflow.com/questions/1720244/create-new-user-in-mysql-and-give-it-full-access-to-one-database

반응형