TungNT (Blue)

tungnt.blue@gmail.com

User Tools

Site Tools


development:database:mysql

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
development:database:mysql [2024/08/06 13:32] – [4.3. Copy table từ file .ibd] tungntdevelopment:database:mysql [2025/03/19 04:14] (current) – [Kiểm tra thư mục dữ liệu] tungnt
Line 184: Line 184:
 </file> </file>
  
 +===== 1.5. Một số lệnh khác =====
 +
 +<file sql>
 +mysqld --verbose --help # https://dev.mysql.com/doc/refman/8.4/en/server-options.html
 +</file>
 +
 +==== Kiểm tra thư mục dữ liệu ====
 +
 +<code sql>
 +SHOW VARIABLES LIKE 'datadir';
 +</code>
 +
 +==== Kiểm tra dung lượng bảng đã đánh partition ====
 +
 +<code sql>
 +SELECT TABLE_NAME, PARTITION_NAME, SUBPARTITION_NAME, ROUND(DATA_LENGTH / (1024 * 1024 * 1024), 2) AS Size_GB FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'user_transactions' AND PARTITION_NAME = 'year2025'
 +</code>
 ====== 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 'blue001'@'localhost' IDENTIFIED BY 'TungNT123312##';+mysql> CREATE USER 'blue001'@'localhost' IDENTIFIED BY '***';
 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='N' */ CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb3 COLLATE utf8mb3_unicode_ci */ /*!80016 DEFAULT ENCRYPTION='N' */
  
 +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
 +</file>
  
 +Vào thư mục data lưu trữ MySQL, ví dụ: /opt/homebrew/var/mysql
 +
 +<code bash>
 +tungnt@MacBook-Pro-cua-Nguyen-2 mysql % cd /opt/homebrew/var/mysql
 +tungnt@MacBook-Pro-cua-Nguyen-2 mysql % ls -la
 +total 1277248
 +-rw-r-----    1 tungnt  admin     196608 Aug  6 20:30 #ib_16384_0.dblwr
 +-rw-r-----    1 tungnt  admin    8585216 Aug  6 20:29 #ib_16384_1.dblwr
 +drwxr-x---   34 tungnt  admin       1088 Aug  6 17:50 #innodb_redo
 +drwxr-x---   12 tungnt  admin        384 Aug  6 17:50 #innodb_temp
 +drwxr-xr-x   41 tungnt  admin       1312 Aug  6 18:08 .
 +drwxrwxr-x   15 tungnt  admin        480 Nov 15  2023 ..
 +-rw-r-----    1 tungnt  admin       1435 Jun  8  2021 MBP-cua-Nguyen.err
 +-rw-r-----    1 tungnt  admin          4 Jun  5  2021 MBP-cua-Nguyen.pid
 +-rw-r-----    1 tungnt  admin     135021 Aug  6 20:29 MacBook-Pro-cua-Nguyen-2.local.err
 +-rw-r-----    1 tungnt  admin          6 Aug  6 17:50 MacBook-Pro-cua-Nguyen-2.local.pid
 +-rw-r-----    1 tungnt  admin   11601861 Nov  5  2023 MacBook-Pro-cua-Nguyen.local.err
 +-rw-r-----    1 tungnt  admin         56 May 25  2021 auto.cnf
 +drwxr-x---   13 tungnt  admin        416 Jul 28  2022 bank_crawler
 +-rw-r-----    1 tungnt  admin       6250 Jul 26 17:15 binlog.000208
 +-rw-r-----    1 tungnt  admin        883 Aug  6 17:38 binlog.000209
 +-rw-r-----    1 tungnt  admin        158 Aug  6 17:47 binlog.000210
 +-rw-r-----    1 tungnt  admin        181 Aug  6 17:49 binlog.000211
 +-rw-r-----    1 tungnt  admin       1279 Aug  6 20:29 binlog.000212
 +-rw-r-----    1 tungnt  admin         80 Aug  6 17:50 binlog.index
 +-rw-------    1 tungnt  admin       1676 May 25  2021 ca-key.pem
 +-rw-r--r--    1 tungnt  admin       1112 May 25  2021 ca.pem
 +-rw-r--r--    1 tungnt  admin       1112 May 25  2021 client-cert.pem
 +-rw-------    1 tungnt  admin       1680 May 25  2021 client-key.pem
 +-rw-r-----    1 tungnt  admin       4433 Aug  6 17:49 ib_buffer_pool
 +-rw-r-----    1 tungnt  admin   79691776 Aug  6 20:29 ibdata1
 +-rw-r-----    1 tungnt  admin   12582912 Aug  6 17:50 ibtmp1
 +drwxr-x---    8 tungnt  admin        256 Jun 15 10:14 mysql
 +-rw-r-----    1 tungnt  admin   71303168 Aug  6 20:29 mysql.ibd
 +-rw-r-----    1 tungnt  admin          5 Jun 15 10:14 mysql_upgrade_info
 +drwxr-x---  114 tungnt  admin       3648 Jun 15 10:14 performance_schema
 +-rw-------    1 tungnt  admin       1676 May 25  2021 private_key.pem
 +-rw-r--r--    1 tungnt  admin        452 May 25  2021 public_key.pem
 +-rw-r--r--    1 tungnt  admin       1112 May 25  2021 server-cert.pem
 +-rw-------    1 tungnt  admin       1680 May 25  2021 server-key.pem
 +drwxr-x---    3 tungnt  admin         96 May 25  2021 sys
 +drwxr-x---  118 tungnt  admin       3776 May  7 15:08 template@002e1site@002evn
 +drwxr-x---    3 tungnt  admin         96 Aug  6 17:45 test
 +drwxr-x---   89 tungnt  admin       2848 Jul 19  2023 test@002dtemplate@002e1site@002evn
 +-rw-r-----    1 tungnt  admin  234881024 Aug  6 20:30 undo_001
 +-rw-r-----    1 tungnt  admin  234881024 Aug  6 20:30 undo_002
 +</code>
 +
 +Trong thư mục data trên, có thể nhìn thấy các database: 
 +
 +<code>
 ++------------------------+
 +| Database               |
 ++------------------------+
 +| bank_crawler           |
 +| information_schema     |
 +| mysql                  |
 +| performance_schema     |
 +| sys                    |
 +| template.1site.vn      |
 +| test                   |
 +| test-template.1site.vn |
 ++------------------------+
 +</code>
 +
 +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---   3 tungnt  admin      96 Aug  6 17:45 .
 +drwxr-xr-x  41 tungnt  admin    1312 Aug  6 18:08 ..
 +-rw-r-----   1 tungnt  admin  114688 Jul 25 18:04 users.ibd
 +</code>
 +
 +**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='N' */
 +    -> ;
 +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,
 +    ->   `name` VARCHAR(255) COLLATE utf8mb3_unicode_ci DEFAULT NULL,
 +    ->   PRIMARY KEY (`id`)
 +    -> ) 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)
 </file> </file>
-===== 4.4. Tham khảo ===== 
  
-https://dba.stackexchange.com/questions/174/how-can-i-move-a-database-from-one-server-to-another+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---   2 tungnt  admin    64 Aug  6 20:28 . 
 +drwxr-xr-x  41 tungnt  admin  1312 Aug  6 18:08 .. 
 +tungnt@MacBook-Pro-cua-Nguyen-2 mysql %  
 +tungnt@MacBook-Pro-cua-Nguyen-2 mysql % cp test/users.ibd test_new  
 +tungnt@MacBook-Pro-cua-Nguyen-2 mysql % ls -la test_new            
 +total 224 
 +drwxr-x---   3 tungnt  admin      96 Aug  6 20:29 . 
 +drwxr-xr-x  41 tungnt  admin    1312 Aug  6 18:08 .. 
 +-rw-r-----   1 tungnt  admin  114688 Aug  6 20:29 users.ibd 
 +</code> 
 + 
 +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) 
 +</file> 
 + 
 +**Tham khảo:** https://dba.stackexchange.com/questions/174/how-can-i-move-a-database-from-one-server-to-another 
 + 
 +**I recently moved a 30GB database with the following stragegy:** 
 + 
 +  * **Old Server** 
 +    * Stop mysql server 
 +    * Copy contents of datadir to another location on disk (~/mysqldata/*) 
 +    * Start mysql server again (downtime was 10-15 minutes) 
 +    * compress the data (tar -czvf mysqldata.tar.gz ~/mysqldata) 
 +    * 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:database:mysql:partition|Partition]]   * [[development:database:mysql:partition|Partition]]
   * [[development:database:mysql:transaction|Transaction]]   * [[development:database:mysql:transaction|Transaction]]
 +  * [[development:database:mysql:debezium|Debezium MySQL]]
   * [[development:database:mysql:su-dung-explain-de-toi-uu-cau-lenh-mysql|Sử dụng EXPLAIN để tối ưu câu lệnh MySQL]]   * [[development:database:mysql:su-dung-explain-de-toi-uu-cau-lenh-mysql|Sử dụng EXPLAIN để tối ưu câu lệnh MySQL]]
   * [[development:database:mysql:giai-phap-generate-id-trong-database-instagram|Một số giải pháp generate ID]]   * [[development:database:mysql:giai-phap-generate-id-trong-database-instagram|Một số giải pháp generate ID]]
Line 307: Line 513:
   * [[https://www.youtube.com/watch?v=f-G8M_Ko4KI|4 Nguyên tắc lựa chọn Database mà tôi áp dụng trong mọi dự án triển khai của mình]]   * [[https://www.youtube.com/watch?v=f-G8M_Ko4KI|4 Nguyên tắc lựa chọn Database mà tôi áp dụng trong mọi dự án triển khai của mình]]
   * [[https://www.youtube.com/watch?v=5z5_fMQn4Tc|Thiết kế Database đáp ứng 400 triệu người tại Quora]]   * [[https://www.youtube.com/watch?v=5z5_fMQn4Tc|Thiết kế Database đáp ứng 400 triệu người tại Quora]]
 +  * [[development:database:mysql:inverse-indexes|Inverse Indexes]]
 +  * [[development:database:mysql:cluster|Cluster]]
development/database/mysql.1722951155.txt.gz · Last modified: 2024/08/06 13:32 by tungnt

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki