Tìm hiểu sử dụng MySQL.
1. MySQL Access Control System
- MySQL thực hiện một hệ thống đặc quyền và kiểm soát truy cập để có thể tạo các quy tắc truy cập để có thể quản lý và chặn các client trái phép truy cập vào hệ thống.
- Kiểm soát truy cập của MySQL có hai giai đoạn khi một client kết nối đến server:
- Xác thực kết nối: Khi kết nối đến một server MySQL, client sẽ phải xác thực tài khoản và mật khẩu, thêm vào đó, host client sử dụng để kết nối đến database server phải khớp với host nằm trong table grant trong MySQL.
- Xác thực yêu cầu: Khi kết nối được thiết lập thành công, với mỗi yêu cầu từ client, MySQL sẽ kiểm tra xem client có quyền để thực hiện câu lệnh đó không. MySQL có khả năng kiểm tra quyền ở cấp độ database, table và field.
- Database
mysql
được tự động tạo ra khi MySQL được cài đặt. Trong đó, có năm table chính dùng để phân quyền. GRAND và REVOKE là hai lệnh thường xuyên được sử dụng mà tương tác với các bảng này.user
: Là table chứa user và các đặc quyền toàn cục. Nó được dùng để cho phép hay chặn user từ một host nào đó. Đặc quyền được cấp trong tableuser
sẽ được áp dụng với mọi database trên server.db
: Là table chứa các đặc quyền ở mức database. Tabledb
được sử dụng để xác định cơ sở dữ liệu nào mà người dùng có thể truy cập từ một host nào đó. Một đặc quyền cho một database sẽ được áp dụng ở mức database và cho các đối tượng thuộc database.table_priv
vàcolumens_priv
: chứa các đặc quyền ở mức table và mức colume. Quyền mức table áp dụng cho cả table và colume của nó trong khi quyền ở mức colume chỉ áp dụng cho quyền của colume.procs_priv
: chứa quyền liên quan đến stored funtions và stored procedures
2. Truy cập vào MySQL
- Truy cập vào mysql trên localhost:
mysql -u username -p
- Truy cập cập mysql trên host khác:
mysql -u username -p -h hostname
Trong đó:-u
là chỉ định tài khoản người dùng,-p
để xác định mật khẩu, nếu mật khẩu không được đưa ra thì nó sẽ hỏi sau đó,-h
để xác định host muốn kết nối đến.3. Tương tác với tài khoản.
- Trong MySQL, bạn phải chỉ định cả user và host mà user sử dụng để kết nối đến database. Tài khoản người dùng chứa username và host name được tách biệt bởi dấu
@
. - Tài khoản người dùng được lưu trong table
user
trong databasemysql
.
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.
- Kiểm tra quyền của một user sử dụng
SHOW GRANTS FOR 'username'@'hostname';
> CREATE USER 'lamth'@'localhost'; Query OK, 0 rows affected (0.00 sec) > SHOW GRANTS FOR 'lamth'@'localhost'; +-------------------------------------------+ | Grants for lamth@localhost | +-------------------------------------------+ | GRANT USAGE ON *.* TO 'lamth'@'localhost' | +-------------------------------------------+ 1 row in set (0.00 sec)
Đổi tên cho user.
Cú pháp:
RENAME USER 'old_username'@'hostname' TO 'new_username'@'hostname';
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.
- Sau khi tạo user, user sẽ không co quyền gì. Để gán quyền cho một user, dùng lệnh
GRANT
. - Cú pháp:
GRANT privilege,[privilege],.. ON privilege_level TO user [IDENTIFIED BY password] [REQUIRE tsl_option] [WITH [GRANT_OPTION | resource_option]];
Ví dụ:
GRANT ALL ON db1.* TO lamth99@localhost WITH GRANT OPTION;
Trong đó:
ALL
là để cấp tất cả các quyền đối với database làdb1
và tất cả các bảng trong nó cho userlamth99@localhost
,WITH GRANT OPTION
là để thêm quyền tùy chọn là quyền được grant quyền cho người khác.
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
- Tạo một database mới có tên giống với tên database đã tạo backup.
- chạy lệnh sau:
mysql -u [user] -p [database_name] < [filename].sql
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
- Hiển thị các database: ``` SHOW DATABASES; +——————–+ | Database | +——————–+ | information_schema | | mysql | | performance_schema | | wordpress | +——————–+ 4 rows in set (0.00 sec)
- 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 ```