การบริหารจัดการ ฐานข้อมูล ด้วย MySQL

การติดตั้ง package

# yum install mariadb  mariadb-server
# systemctl enable mariadb
# systemctl start mariadb

# mysql -u root

ตั้งค่า password ให้แก่ user root

# mysql_secure_installation

หลังจากการ ตั้งค่า password แล้ว ต่อไปการ login จะต้องเพิ่ม option -p

# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> \q

การเชื่อมต่อด้วยคำสั่ง mysqladmin

[root@webserver ~]# mysqladmin -u root -p version
Enter password: 
mysqladmin  Ver 9.0 Distrib 5.5.56-MariaDB, for Linux on x86_64
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Server version        5.5.56-MariaDB
Protocol version    10
Connection        Localhost via UNIX socket
UNIX socket        /var/lib/mysql/mysql.sock
Uptime:            19 hours 56 min 8 sec

Threads: 1  Questions: 225  Slow queries: 0  Opens: 0  Flush tables: 2  Open tables: 26  Queries per second avg: 0.003

การเชื่อมต่อ ด้วย option -e จะได้ผลลัพท์

[root@webserver ~]# mysql -u root -p -e "show databases"
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| webdata            |
+--------------------+
[root@webserver ~]#

ต้องการดูวิธีการใช้ คำสั่ง โดยการใช้ ? หรือ "help"

MariaDB [(none)]> ? create

MariaDB [(none)]> help create

MariaDB [(none)]> help create database

Name: 'CREATE DATABASE'
Description:
Syntax:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
    [create_specification] ...

create_specification:
    [DEFAULT] CHARACTER SET [=] charset_name
  | [DEFAULT] COLLATE [=] collation_name

CREATE DATABASE creates a database with the given name. To use this
statement, you need the CREATE privilege for the database. CREATE
SCHEMA is a synonym for CREATE DATABASE.

URL: http://dev.mysql.com/doc/refman/5.5/en/create-database.html

ทดสอบการสร้าง Database ชื่อ testdb และ testuser

MariaDB [(none)]> create database testdb;
MariaDB [(none)]> create user 'testuser'@'localhost' identified by 'password';
MariaDB [(none)]> grant all on testdb.* to 'testuser' identified by 'password';

จาก 3 บรรทัดสามารถเขียนย่อ ได้ด้วยการสร้าง user ในขณะที่มีการ assign database permision

MariaDB [(none)]> create database testdb;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> grant all on testdb.* to 'testuser' identified by 'password';
Query OK, 0 rows affected (0.00 sec)

ออกจาก การเชื่อมต่อ

MariaDB [(none)]> exit

สร้าง ตาราง ด้วยสิทธิของ testuser

# mysql -u testuser -p

ต่อมาจะเป็นการสร้าง ตารางชื่อว่า "customers"

MariaDB [(none)]> use testdb;
Database changed
MariaDB [testdb]> create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);
Query OK, 0 rows affected (0.01 sec)

MariaDB [testdb]> show tables;
+------------------+
| Tables_in_testdb |
+------------------+
| customers        |
+------------------+
1 row in set (0.00 sec)

MariaDB [testdb]> describe customers;
+-------------+---------+------+-----+---------+----------------+
| Field       | Type    | Null | Key | Default | Extra          |
+-------------+---------+------+-----+---------+----------------+
| customer_id | int(11) | NO   | PRI | NULL    | auto_increment |
| first_name  | text    | YES  |     | NULL    |                |
| last_name   | text    | YES  |     | NULL    |                |
+-------------+---------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Reset password root ให้แก่ Database

1 หยุดการทำงานของ Database ที่ทำงานอยู่ และ ทำการ start service ขึ้นมาใหม่ด้วย option ที่ไม่ต้องการ password หรือ safemode

# systemctl stop mariadb
# mysqld_safe --skip-grant-tables &
[1] 5062
# ps -ef | grep 5062
root      5062  4997  0 05:00 pts/0    00:00:00 /bin/sh /bin/mysqld_safe --skip-grant-tables
mysql     5208  5062  0 05:00 pts/0    00:00:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --skip-grant-tables --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      5232  4997  0 05:00 pts/0    00:00:00 grep --color=auto 5062

2 ให้ login ด้วย user root

mysql -u root

MariaDB [(none)]>  use mysql;
MariaDB [(none)]>  update user SET PASSWORD=PASSWORD("password") WHERE USER='root';
MariaDB [(none)]>  flush privileges;
MariaDB [(none)]>  exit

3 หยุดการทำงาน ของ mysqld_safe ก่อน ด้วยการ kill process แล้วจึง ค่อย restart service start Mysql

# kill -9 5062
# systemctl start mariadb
[root@webserver ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SELECT User, Host, Password FROM mysql.user;
+----------+-----------+-------------------------------------------+
| User     | Host      | Password                                  |
+----------+-----------+-------------------------------------------+
| root     | localhost | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| root     | 127.0.0.1 | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| root     | ::1       | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| testuser | %         | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
| webuser  | %         | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 |
+----------+-----------+-------------------------------------------+
5 rows in set (0.00 sec)

results matching ""

    No results matching ""