Skip to the content.

Tìm hiểu sử dụng MySQL.

1. MySQL Access Control System

2. Truy cập vào MySQL

Tạo tài khoản người dùng sử dụng USER CREATE.

Lệnh để tạo user:

CREATE USER user_account IDENTIFIED BY password;

Trong đó, user_account sẽ có dạng 'username'@'hostname', nếu không chỉ định hostname thì nó sẽ có giá trị là % tương đương với tất cả các host. password là mật khẩu của user đó dưới dạng clear text, sau đó mật khẩu này sẽ được mysql mã hóa.

Thay đổi password cho user.

Cú pháp:

ALTER USER 'wpuser'@'10.88.88.153' IDENTIFIED BY '123';

hoặc

UPDATE mysql.user SET Password=PASSWORD('123@123Aa') WHERE User='wpuser' AND Host='10.88.88.153';
FLUSH PRIVILEGES;

Xóa người dùng

Cú pháp

DROP USER 'username'@'hosname';

4. Phân quyền.

Ví dụ cấp nhiều quyền một lúc:

GRANT SELECT, UPDATE, DROP ON db2.table1 TO lam@%;

Lệnh trên sẽ cấp quyền SELECT, UPDATE, DROP đối với table table1 của database db2 cho user lam.

Các quyền sử dụng với GRANT

|Privilege |Meaning | |—-|—| |ALL | Grant all privileges at specified access level except GRANT OPTION | |ALTER |Allow user to use of ALTER TABLE statement| |ALTER ROUTINE |Allow user to alter or drop stored routine |CREATE |Allow user to create database and table |CREATE ROUTINE |Allow user to create stored routine |CREATE TABLESPACE |Allow user to create, alter or drop tablespaces and log file groups |CREATE TEMPORARY TABLES |Allow user to create temporary table by using CREATE TEMPORARY TABLE |CREATE USER |Allow user to use the CREATE USER, DROP USER, RENAME USER, and REVOKE ALL PRIVILEGES statements. |CREATE VIEW |Allow user to create or modify view. |DELETE |Allow user to use DELETE |DROP |Allow user to drop database, table and view |EVENT |Enable use of events for the Event Scheduler. |EXECUTE |Allow user to execute stored routines |FILE |Allow user to read any file in the database directory. |GRANT OPTION |Allow user to have privileges to grant or revoke privileges from other accounts.
|INDEX |Allow user to create or remove indexes. |INSERT |Allow user to use INSERT statement |LOCK TABLES |Allow user to use LOCK TABLES on tables for which you have the SELECT privilege |PROCESS |Allow user to see all processes with SHOW PROCESSLIST statement. |PROXY |Enable user proxying. |REFERENCES |Allow user to create foreign key |RELOAD |Allow user to use FLUSH operations |REPLICATION CLIENT |Allow user to query to see where master or slave servers are |REPLICATION SLAVE |Allow the user to use replicate slaves to read binary log events from the master. |SELECT |Allow user to use SELECT statement |SHOW DATABASES |Allow user to show all databases |SHOW VIEW |Allow user to use SHOW CREATE VIEW statement |SHUTDOWN |Allow user to use mysqladmin shutdown command |SUPER |Allow user to use other administrative operations such as CHANGE MASTER TO, KILL, PURGE BINARY LOGS, SET GLOBAL, and mysqladmin command |TRIGGER |Allow user to use TRIGGER operations. |UPDATE |Allow user to use UPDATE statement |USAGE |Equivalent to “no privileges”

5. Backup Database.

Backup toàn bộ database

Để backup 1 database sử dung lệnh mysqldump, cú pháp:

mysqldump -u [user] -p[password] -h [hostname] [database] > [dump_file.sql]

Ví dụ backup database wordpress:

mysqldump -u root -p123 wordpress > wordpress_db.sql

Chỉ backup cấu trúc database

mysqldump -u [username] -p[password] --no-data [database_name] > [dump_file.sql]

chỉ backup dữ liệu trong database

mysqldump -u [username] -p[password] -no-create-info [database_name] > [dump_file.sql]

Backup nhiều database cùng lúc vào một file

mysqldump -u [username] -p[password]  [dbname1,dbname2,…] > [dump_file.sql]
 
mysqldump -u [username] -p[password] --all-database > [dump_file.sql]

6. Restore backup

Ví dụ khôi phục lại database wordpress

mysql -u root -p123 
CREATE DATABASE wordpress;
exit

mysql -u root -p123 wordpress < wordpress_db.sql

7. Thao tác với database


- Tạo một database mới:

CREATE DATABASE database_name;

- Xóa một database:

DROP DATABASE database_name;

- Chọn một database để làm việc:

USE DATABASE database_name;

## 8. Thao tác với table
Chọn database để làm việc:

MariaDB [(none)]> USE wordpress; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A

Database changed MariaDB [wordpress]>

- Hiển thị các bảng trong database:

MariaDB [wordpress]> SHOW TABLES; +———————–+ | Tables_in_wordpress | +———————–+ | wp_commentmeta | | wp_comments | | wp_links | | wp_options | | wp_postmeta | | wp_posts | | wp_term_relationships | | wp_term_taxonomy | | wp_termmeta | | wp_terms | | wp_usermeta | | wp_users | +———————–+ 12 rows in set (0.00 sec)

MariaDB [wordpress]>


- Hiển thị nội dung của một bảng

MariaDB [wordpress]> SELECT * FROM wp_users; +—-+————+————————————+—————+———————+———-+———————+———————+————-+————–+ | ID | user_login | user_pass | user_nicename | user_email | user_url | user_registered | user_activation_key | user_status | display_name | +—-+————+————————————+—————+———————+———-+———————+———————+————-+————–+ | 1 | root | $P$BM6dusEQr4UXfClLlqXTVy2arOpa280 | root | lamthroot@gmail.com | | 2019-07-23 02:13:21 | | 0 | root | +—-+————+————————————+—————+———————+———-+———————+———————+————-+————–+ 1 row in set (0.00 sec)

MariaDB [wordpress]>



# Cấu hình enable log MySQL
Log rất quan trọng để khắc phục sự cố hay tìm hiểu chi tiết về ứng dụng đang chạy.

Có 3 loại log trong MySQL là:
- **Error log** - chứa thông tin của các lỗi được tạo ra bởi MySQL server. Nó giúp gỡ lỗi các vấn đề về 
- **Genaral Log** - chứa các log chung của các hoạt động MySQL như query, user connection và disconnect,... Nó giúp theo dõi những hoạt động trên mysql server.
- **Slow query log** - chứa các query chậm, mà dùng nhiều thời gian để phản hồi hơn dự kiến.

### Error log
Để kích hoạt error log MySQL hoặc thay đổi vị trí error log MySQL. Chỉnh sửa tệp cấu hình MySQL và cập nhật cài đặt dưới [mysqld] theo yêu cầu của bạn. Tập tin này chứa lỗi được tạo trong máy chủ MySQL.

 [mysqld]
 log_error = /var/log/mysql/error.log
### General Log in MySQL
To enable general logs in MySQL or to change location of general log files, edit configuration file and make following changes. Uncomment following lines to enable general logs and change log file path to create log on different directory. This file contains all general logs of mysql server for eg: queries, user connect or disconnect etc.

general_log_file = /var/log/mysql/mysql.log general_log = 1

### Slow Query Log in MySQL
To enable or change file path of MySQL slow query logs. Edit MySQL configuration file and uncomment or add the following line in [mysqld] section. This file contains logs of those queries which is taking more time to complete. Which queries taking time more than defined as long_query_time are logged.

log_slow_queries = /var/log/mysql/mysql-slow.log long_query_time = 2

### Restart MySQL Service
After making any of the above changes in MySQL configuration, You must have to restart MySQL services to apply changes. Basically, the service management differs with most of the Linux operating systems. Here is the command used for the popular Linux operating systems to restart MySQL service.
This method requires a server restart.

sudo service mysql restart sudo systemctl restart mysql.service ```