development:database:mysql
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
development:database:mysql [2024/08/06 13:32] – [4.3. Copy table từ file .ibd] tungnt | development:database:mysql [2025/03/19 04:14] (current) – [Kiểm tra thư mục dữ liệu] tungnt | ||
---|---|---|---|
Line 184: | Line 184: | ||
</ | </ | ||
+ | ===== 1.5. Một số lệnh khác ===== | ||
+ | |||
+ | <file sql> | ||
+ | mysqld --verbose --help # https:// | ||
+ | </ | ||
+ | |||
+ | ==== Kiểm tra thư mục dữ liệu ==== | ||
+ | |||
+ | <code sql> | ||
+ | SHOW VARIABLES LIKE ' | ||
+ | </ | ||
+ | |||
+ | ==== Kiểm tra dung lượng bảng đã đánh partition ==== | ||
+ | |||
+ | <code sql> | ||
+ | SELECT TABLE_NAME, PARTITION_NAME, | ||
+ | </ | ||
====== 2. Tạo tài khoản và phân quyền MySQL ====== | ====== 2. Tạo tài khoản và phân quyền MySQL ====== | ||
Line 216: | Line 233: | ||
<file sql> | <file sql> | ||
- | mysql> CREATE USER ' | + | mysql> CREATE USER ' |
Query OK, 0 rows affected (0.10 sec) | Query OK, 0 rows affected (0.10 sec) | ||
Line 292: | Line 309: | ||
CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci */ /*!80016 DEFAULT ENCRYPTION=' | CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci */ /*!80016 DEFAULT ENCRYPTION=' | ||
+ | CREATE TABLE: CREATE TABLE `users` ( | ||
+ | `id` INT NOT NULL AUTO_INCREMENT, | ||
+ | `name` VARCHAR(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL, | ||
+ | PRIMARY KEY (`id`) | ||
+ | ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci | ||
+ | </ | ||
+ | Vào thư mục data lưu trữ MySQL, ví dụ: / | ||
+ | |||
+ | <code bash> | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % cd / | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % ls -la | ||
+ | total 1277248 | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | drwxr-x--- | ||
+ | drwxr-x--- | ||
+ | drwxr-xr-x | ||
+ | drwxrwxr-x | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | drwxr-x--- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw------- | ||
+ | -rw-r--r-- | ||
+ | -rw-r--r-- | ||
+ | -rw------- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | drwxr-x--- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | drwxr-x--- | ||
+ | -rw------- | ||
+ | -rw-r--r-- | ||
+ | -rw-r--r-- | ||
+ | -rw------- | ||
+ | drwxr-x--- | ||
+ | drwxr-x--- | ||
+ | drwxr-x--- | ||
+ | drwxr-x--- | ||
+ | -rw-r----- | ||
+ | -rw-r----- | ||
+ | </ | ||
+ | |||
+ | Trong thư mục data trên, có thể nhìn thấy các database: | ||
+ | |||
+ | < | ||
+ | +------------------------+ | ||
+ | | Database | ||
+ | +------------------------+ | ||
+ | | bank_crawler | ||
+ | | information_schema | ||
+ | | mysql | | ||
+ | | performance_schema | ||
+ | | sys | | ||
+ | | template.1site.vn | ||
+ | | test | | ||
+ | | test-template.1site.vn | | ||
+ | +------------------------+ | ||
+ | </ | ||
+ | |||
+ | Vào trong từng thư mục database, có thể nhìn thấy các file table: | ||
+ | |||
+ | <code bassh> | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % cd test | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 test % ls -la | ||
+ | total 224 | ||
+ | drwxr-x--- | ||
+ | drwxr-xr-x | ||
+ | -rw-r----- | ||
+ | </ | ||
+ | |||
+ | **Ví dụ để clone bảng test thành bảng test_new ta làm như sau:** | ||
+ | |||
+ | <file sql> | ||
+ | % mysql -uroot -p | ||
+ | |||
+ | mysql> show databases; | ||
+ | +------------------------+ | ||
+ | | Database | ||
+ | +------------------------+ | ||
+ | | bank_crawler | ||
+ | | information_schema | ||
+ | | mysql | | ||
+ | | performance_schema | ||
+ | | sys | | ||
+ | | template.1site.vn | ||
+ | | test | | ||
+ | | test-template.1site.vn | | ||
+ | +------------------------+ | ||
+ | 8 rows in set (0.03 sec) | ||
+ | |||
+ | mysql> CREATE DATABASE `test_new` /*!40100 DEFAULT CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci */ /*!80016 DEFAULT ENCRYPTION=' | ||
+ | -> ; | ||
+ | Query OK, 1 row affected, 2 warnings (0.04 sec) | ||
+ | |||
+ | mysql> show databases; | ||
+ | +------------------------+ | ||
+ | | Database | ||
+ | +------------------------+ | ||
+ | | bank_crawler | ||
+ | | information_schema | ||
+ | | mysql | | ||
+ | | performance_schema | ||
+ | | sys | | ||
+ | | template.1site.vn | ||
+ | | test | | ||
+ | | test-template.1site.vn | | ||
+ | | test_new | ||
+ | +------------------------+ | ||
+ | 9 rows in set (0.04 sec) | ||
+ | |||
+ | mysql> use test_new; | ||
+ | Database changed | ||
+ | mysql> CREATE TABLE `users` ( | ||
+ | -> `id` INT NOT NULL AUTO_INCREMENT, | ||
+ | -> | ||
+ | -> | ||
+ | -> ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_unicode_ci; | ||
+ | Query OK, 0 rows affected, 3 warnings (0.05 sec) | ||
+ | |||
+ | mysql> show tables; | ||
+ | +--------------------+ | ||
+ | | Tables_in_test_new | | ||
+ | +--------------------+ | ||
+ | | users | | ||
+ | +--------------------+ | ||
+ | 1 row in set (0.00 sec) | ||
+ | |||
+ | mysql> ALTER TABLE users DISCARD TABLESPACE; | ||
+ | Query OK, 0 rows affected (0.12 sec) | ||
</ | </ | ||
- | ===== 4.4. Tham khảo ===== | ||
- | https:// | + | Sau khi DISCARD TABLESPACE, copy file table từ thư mục database test sang thư mục test_new: |
+ | |||
+ | <code bash> | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % ls -la test_new | ||
+ | total 0 | ||
+ | drwxr-x--- | ||
+ | drwxr-xr-x | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % cp test/ | ||
+ | tungnt@MacBook-Pro-cua-Nguyen-2 mysql % ls -la test_new | ||
+ | total 224 | ||
+ | drwxr-x--- | ||
+ | drwxr-xr-x | ||
+ | -rw-r----- | ||
+ | </ | ||
+ | |||
+ | Sau khi copy xong, IMPORT TABLESPACE như sau: | ||
+ | |||
+ | <file sql> | ||
+ | mysql> ALTER TABLE users IMPORT TABLESPACE; | ||
+ | Query OK, 0 rows affected, 1 warning (0.07 sec) | ||
+ | |||
+ | mysql> select * from users; | ||
+ | +----+-----------+ | ||
+ | | id | name | | ||
+ | +----+-----------+ | ||
+ | | 1 | TungNT001 | | ||
+ | | 2 | VienLD | ||
+ | | 3 | CuongPT | ||
+ | +----+-----------+ | ||
+ | 3 rows in set (0.01 sec) | ||
+ | </ | ||
+ | |||
+ | **Tham khảo: | ||
+ | |||
+ | **I recently moved a 30GB database with the following stragegy: | ||
+ | |||
+ | * **Old Server** | ||
+ | * Stop mysql server | ||
+ | * Copy contents of datadir to another location on disk (~/ | ||
+ | * Start mysql server again (downtime was 10-15 minutes) | ||
+ | * compress the data (tar -czvf mysqldata.tar.gz ~/ | ||
+ | * copy the compressed file to new server | ||
+ | * **New Server** | ||
+ | * install mysql (don't start) | ||
+ | * unzip compressed file (tar -xzvf mysqldata.tar.gz) | ||
+ | * move contents of mysqldata to the datadir | ||
+ | * Make sure your innodb_log_file_size is same on new server, or if it's not, don't copy the old log files (mysql will generate these) | ||
+ | * Start mysql | ||
====== 5. Tham khảo ====== | ====== 5. Tham khảo ====== | ||
Line 302: | Line 507: | ||
* [[development: | * [[development: | ||
* [[development: | * [[development: | ||
+ | * [[development: | ||
* [[development: | * [[development: | ||
* [[development: | * [[development: | ||
Line 307: | Line 513: | ||
* [[https:// | * [[https:// | ||
* [[https:// | * [[https:// | ||
+ | * [[development: | ||
+ | * [[development: |
development/database/mysql.1722951155.txt.gz · Last modified: 2024/08/06 13:32 by tungnt