How to migrate an mysql storage location to a new disk

  • 2021-12-21 05:19:00
  • OfStack

1. Prepare a new disk and format it as the same file system as the current root partition, create a directory and mount the disk


]#fdisk -l # View Disk Information 
]#fdisk /dev/sdb # Partition 
]# df -T # You can view the file system type of the root partition 
]#mkfs.xfs /dev/sdb1 # Formatting a new disk partition 
]#mkdir /data
]#mount /dev/sdb1 /data # Mount disk 
 Or write /etc/fatab File, add 1 Line: /dev/sdb1 /data xfs defaults 0 0  Auto-mount on boot 
]#df -h // Confirm mount results 

2. Stop the database service and modify the configuration file


]#systemctl stop mysqld
]#vim /etc/my.cnf

   datadir=/data/mysql
   socket=/data/mysql/mysql.sock
   log-error=/data/log/mysqld.log 
   log_bin=/data/mysql/bin-log
   log-bin-index=/data/mysql/bin-log.index
  [client]
   socket=/data/mysql/mysql.sock  #mysql Client sock The default is to read /var/lib/mysql/mysql.sock , You need to add the client's sock Configuration. 
  ]#vim /data/mysql/bin-log.index

Modify the binlog index file and re-specify the storage path of binlog.

3. Create the corresponding directory and change the permissions. Migrate the database catalog.


]#chown -R mysql:mysql /data
]#mkdir /data/log
]#mv /usr/lib/mysql /data/

4. Start the service and view the change results.


]#systemctl start mysqld
 Access to the database 
mysql >show variables like  ' datadir';
# You can see where the database is stored and see if the data is correct. 
mysql>show variables like  ' %sock%';
# View sock File location , You can also view /data/ Whether the data in the corresponding directory is correct. 

The firewall is closed and selinux is in Disabled state.

Summarize


Related articles: