Trong bài viết này, chúng ta sẽ tìm hiểu cách thay đổi Thư mục dữ liệu MySQL hoặc di chuyển dữ liệu Cơ sở dữ liệu MySQL sang vị trí mới, tình huống này có thể được sử dụng khi cơ sở dữ liệu đang phát triển rất nhanh hoặc vì một số lý do bảo mật mà chúng tôi muốn di chuyển thư mục dữ liệu
điều kiện tiên quyết
- Máy Ubuntu có người dùng không root có quyền Sudo
- MySQL được cài đặt và hoạt động
- Ổ đĩa hoặc vị trí mới mà chúng tôi muốn di chuyển vị trí dữ liệu cơ sở dữ liệu, vị trí mới sẽ là /mnt/data_vol/MySQL vì data_vol là ổ đĩa mới được gắn vào máy hoặc máy chủ
Thay đổi vị trí thư mục dữ liệu MySQL
Trước khi tiếp tục, trước tiên chúng ta sẽ tìm vị trí hiện tại của thư mục dữ liệu
$ mysql –u root –p Output: Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 472 Server version: 5.6.30-0ubuntu0.14.04.1 [Ubuntu] Copyright [c] 2000, 2016, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
Khi được nhắc nhập mật khẩu gốc của MySQL, vui lòng nhập mật khẩu. Chạy lệnh dưới đây để biết thư mục dữ liệu làm việc hiện tại cho MySQL
Mysql> select @@datadir; Output: +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set [0.00 sec]
Vì đầu ra sẽ cho thấy rằng cơ sở dữ liệu MySQL sử dụng /var/lib/MySQL làm thư mục mặc định làm thư mục dữ liệu. Trước khi chúng tôi sửa đổi bất cứ điều gì trước tiên, chúng tôi sẽ kiểm tra tính toàn vẹn của dữ liệu, chúng tôi sẽ dừng MySQL và kiểm tra trạng thái
$ sudo systemctl stop mysql
Vì systemctl sẽ không hiển thị bất cứ thứ gì cho lệnh dịch vụ
$ sudo systemctl status mysql Output: mysql.service - MySQL Community Server Loaded: loaded [/lib/systemd/system/mysql.service; enabled; vendor preset: enabled] Active: inactive [dead] since Mon 2016-09-12 13:57:43 IST; 1s ago Process: 17669 ExecStartPost=/usr/share/mysql/mysql-systemd-start post [code=exited, status=0/SUCCESS] Process: 17668 ExecStart=/usr/sbin/mysqld [code=exited, status=0/SUCCESS] Process: 17664 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre [code=exited, status=0/SUCCESS] Main PID: 17668 [code=exited, status=0/SUCCESS] Sep 12 13:55:14 ubuntu-16 systemd[1]: Starting MySQL Community Server... Sep 12 13:55:15 ubuntu-16 systemd[1]: Started MySQL Community Server. Sep 12 13:57:40 ubuntu-16 systemd[1]: Stopping MySQL Community Server... Sep 12 13:57:43 ubuntu-16 systemd[1]: Stopped MySQL Community Server.
Khi chúng tôi xác nhận rằng MySQL đã dừng, chúng tôi sẽ di chuyển dữ liệu đến vị trí mới. Để di chuyển dữ liệu, chúng tôi sẽ sử dụng Rsync với tùy chọn –a duy trì quyền của các tệp dữ liệu và –v hiển thị đầu ra dài dòng
Dưới đây là lệnh đầy đủ để di chuyển dữ liệu đến vị trí mới –
$ rsync –av /var/lib/mysql /mnt/data_vol/ OutPut: sending incremental file list mysql/ mysql/auto.cnf mysql/debian-5.7.flag mysql/ib_buffer_pool mysql/ib_logfile0 mysql/ib_logfile1 mysql/ibdata1 mysql/mysql/ mysql/mysql/columns_priv.MYD mysql/mysql/columns_priv.MYI mysql/mysql/columns_priv.frm mysql/mysql/db.MYD mysql/mysql/db.MYI mysql/mysql/db.frm mysql/mysql/db.opt …. mysql/sys/x@0024user_summary.frmmysql/sys/x@0024user_summary_by_file_io.frm mysql/sys/x@0024user_summary_by_file_io_type.frm mysql/sys/x@0024user_summary_by_stages.frm mysql/sys/x@0024user_summary_by_statement_latency.frm mysql/sys/x@0024user_summary_by_statement_type.frm mysql/sys/x@0024wait_classes_global_by_avg_latency.frm mysql/sys/x@0024wait_classes_global_by_latency.frm mysql/sys/x@0024waits_by_host_by_latency.frm mysqlsys//x@0024waits_by_user_by_latency.frm mysql/sys/x@0024waits_global_by_latency.frm sent 199,384,083 bytes received 6,858 bytes 132,927,294.00 bytes/sec total size is 199,307,568 speedup is 1.00
Sau khi rsync, di chuyển thành công thư mục dữ liệu đến vị trí mới. Vì lý do bảo mật, chúng tôi sẽ giữ thư mục dữ liệu cho đến khi xác nhận rằng dữ liệu ở vị trí mới, chúng tôi sẽ đổi tên thư mục dữ liệu hiện tại từ /var/lib/mysql thành /var/lib/mysql_backup. bên dưới là lệnh thay đổi thư mục dữ liệu hiện tại
Dưới đây là lệnh thay đổi thư mục dữ liệu hiện tại –
$ sudo mv /var/lib/mysql /var/lib/mysql_backup
Bây giờ chúng ta sẽ thay đổi thư mục dữ liệu mặc định, để thay đổi chúng ta có rất nhiều cách nhưng chúng ta sẽ chỉnh sửa mysqld. cnf nằm trong /etc/mysql/mysql. conf. d/mysqld. cnf
Để chỉnh sửa mysqld. cnf, bên dưới là lệnh
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf Output: [mysqld_safe] socket = /var/run/mysqld/mysqld.sock nice = 0 [mysqld] # # * Basic Settings # user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /mnt/data_vol/mysql/ tmpdir = /tmp lc-messages-dir = /usr/share/mysql skip-external-locking
Thay đổi cài đặt bí danh Apparmor
Ngoài ra, chúng tôi cần chỉnh sửa /etc/apparmor. d/tunables/bí danh
Ở cuối tệp, chúng tôi cần thêm các dòng sau vào quy tắc bí danh
$ sudo vi /etc/apparmor.d/tunables/alias Output: # ------------------------------------------------------------------ # # Copyright [C] 2010 Canonical Ltd. # # This program is free software; you can redistribute it and/or # modify it under the terms of version 2 of the GNU General Public # License published by the Free Software Foundation. # # ------------------------------------------------------------------ # Alias rules can be used to rewrite paths and are done after variable # resolution. For example, if '/usr' is on removable media: # alias /usr/ -> /mnt/usr/, # # Or if mysql databases are stored in /home: # alias /var/lib/mysql/ -> /home/mysql/, alias /var/lib/mysql/ -> /mnt/data_vol/mysql
Khi tệp được chỉnh sửa, chúng tôi cần khởi động lại apparmor
Dưới đây là lệnh để khởi động lại apparmor
Vì chúng tôi đã thay đổi thư mục dữ liệu mặc định, chúng tôi cần chạy lệnh bên dưới để tạo cấu trúc thư mục thư mục tối thiểu để truyền môi trường tập lệnh
$ sudo mkdir /var/lib/mysql/mysql –p
Bây giờ chúng tôi sẽ khởi động lại các dịch vụ mysql
________số 8Bây giờ chúng ta sẽ kiểm tra trạng thái của các dịch vụ MySQL bằng lệnh dưới đây
$ sudo systemctl status mysql Output: mysql.service - MySQL Community Server Loaded: loaded [/lib/systemd/system/mysql.service; enabled; vendor preset: enabled] Active: active [running] since Mon 2016-09-12 14:17:27 IST; 23s ago Process: 18481 ExecStartPost=/usr/share/mysql/mysql-systemd-start post [code=exited, status=0/SUCC Process: 18477 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre [code=exited, status=0/SUCCES Main PID: 18480 [mysqld] Tasks: 28 [limit: 512] Memory: 137.3M CPU: 329ms CGroup: /system.slice/mysql.service └─18480 /usr/sbin/mysqld Sep 12 14:17:26 ubuntu-16 systemd[1]: Starting MySQL Community Server... Sep 12 14:17:27 ubuntu-16 systemd[1]: Started MySQL Community Server.
Để đảm bảo rằng thư mục dữ liệu mới được thay đổi, chúng tôi sẽ chạy lệnh bên dưới
Mysql> select @@datadir; Output: +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set [0.00 sec]0
Khi chúng tôi xác nhận rằng thư mục dữ liệu đã được thay đổi, chúng tôi sẽ xóa thư mục dữ liệu mặc định tại /var/lib/mysql_backup, bên dưới là lệnh xóa thư mục cơ sở dữ liệu cũ
Mysql> select @@datadir; Output: +-----------------+ | @@datadir | +-----------------+ | /var/lib/mysql/ | +-----------------+ 1 row in set [0.00 sec]1
Trong cấu hình và các bước ở trên, chúng ta đã học cách di chuyển thư mục dữ liệu MySQL sang vị trí mới, điều này sẽ giúp chúng ta bảo mật hoặc lưu trữ nhiều dữ liệu hơn đến một vị trí khác