如何在Linux上重新安装MySQL v5.x

时间:2020-01-09 10:41:22  来源:igfitidea点击:

我的MySQL数据库安装已损坏。
如何删除旧的mysql服务器。
如何在Linux或类似Unix的oses上重新安装MySQL数据库服务器5.x版本?
软件升级破坏了我的系统。
如何再次卸载并重新安装mysql以再次获得完整的mysql?

您可以轻松地在Linux或类似Unix的操作系统上重新安装mysql服务器。
步骤如下:

  • 备份数据库和所有配置文件
  • 擦除/卸载现有的mysql服务器/客户端。
  • 删除所有文件的数据目录。
  • 删除所有的mysql配置文件。
  • 完全重新安装mysql服务器。
  • 恢复配置文件和数据库。

步骤1:备份备份备份 重要的事情说3遍

进行备份在执行备份之前,对系统进行备份有多么重要。
你需要备份

  • MySQL数据库数据目录(例如/var/lib/mysql /
  • MySQL数据库使用mysqldump命令。
  • Mysql配置文件/etc/my.cnf、/etc/logrotate.d/mysqld和其他文件。
  • Mysql日志文件(例如/var/log/mysqld.log

在此示例中,我正在基于CentOS/RHEL 6.x的服务器上备份所有重要文件:

# mkdir /root/mysql-files/
# tar zcvf /root/mysql-files/mysql.config-files.dd-mm-yyyy.tar.gz /etc/logrotate.d/mysqld /var/log/mysqld.log /etc/my.cnf /root/my.cnf /var/lib/mysql/

在此示例中,我将使用名为mysql-backup.sh的shell脚本备份所有数据库:

#!/bin/sh
# mysql-backup.sh: Dump MySQL databases.
# Note: Test only on RHEL/CentOS/Debian/Ubuntu Linux.
# Author: theitroad <www.theitroad.local> Under GPL v2.0+
# ----------------------------------------------------
NOW=$(date +"%d-%m-%Y")
BAK="/root/mysql-files/$NOW"
 
##################
## SET ME FIRST ##
##################
MUSER="root"
MPASS="YOUR-ROOT-PASSWORD-HERE"
MHOST="localhost"
 
MYSQL="$(which mysql)"
MYSQLDUMP="$(which mysqldump)"
GZIP="$(which gzip)"
 
if [ ! -d $BAK ]; then
  mkdir -p $BAK
else
:
fi
 
DBS="$($MYSQL -u $MUSER -h $MHOST -p$MPASS -Bse 'show databases')"
echo -n "Dumping...${THISDB}..."
for db in $DBS
do
echo -n "$db "
FILE=$BAK/mysql-$db.$NOW-$(date +"%T").gz
$MYSQLDUMP -u $MUSER -h $MHOST -p$MPASS $db | $GZIP -9 > $FILE
done
echo -n  "...Done @ ${BAK} directory."
echo ""

如下运行

$ ./mysql-backup.sh

输出示例:

Dumping......blog theitroad mysql...Done @ /root/mysql-files/08-12-2013 directory.

验证备份:

# ls -l /root/mysql-files/08-12-2013

输出示例:

-rw-r--r--. 1 root root  1836687 Dec  8 04:00 mysql-blog.08-12-2013-04:00:18.gz
-rw-r--r--. 1 root root  7152648 Dec  8 04:00 mysql-theitroad.08-12-2013-04:00:25.gz
-rw-r--r--. 1 root root   135530 Dec  8 04:00 mysql-mysql.08-12-2013-04:00:41.gz

第2步:擦除/删除MySQL服务器

如果您使用的是Debian/Ubuntu Linux,请输入以下apt-get命令:

$ sudo apt-get purge mysql-server mysql-common mysql-client

输出示例:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package mysql-client is not installed, so not removed
The following packages were automatically installed and are no longer required:
  libnet-daemon-perl libdbi-perl libterm-readkey-perl mysql-server-core-5.5 mysql-client-core-5.5
  libplrpc-perl
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
  libdbd-mysql-perl* libmysqlclient18* mysql-client-5.5* mysql-common* mysql-server*
  mysql-server-5.5*
0 upgraded, 0 newly installed, 6 to remove and 2 not upgraded.
After this operation, 67.3 MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 82128 files and directories currently installed.)
Removing mysql-server ...
Removing mysql-server-5.5 ...
mysql stop/waiting
Purging configuration files for mysql-server-5.5 ...
Removing mysql-client-5.5 ...
Removing libdbd-mysql-perl ...
Removing libmysqlclient18 ...
Purging configuration files for libmysqlclient18 ...
Removing mysql-common ...
Purging configuration files for mysql-common ...
dpkg: warning: while removing mysql-common, directory '/etc/mysql' not empty so not removed.
Processing triggers for man-db ...
Processing triggers for ureadahead ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

使用rm命令删除配置/数据库/日志文件:

$ sudo rm -rvfi /var/lib/mysql /etc/mysql/ /var/log/mysql*

如果您使用的是CentOS/RHEL/Fedora/Red Hat/Scientific Linux,请执行以下yum命令来卸载mysql:

# yum remove mysql mysql-server

使用rm命令删除配置/数据库/日志文件:

# rm -rfvi /etc/my.cnf /var/lib/mysql/ /var/log/mysqld.log

步骤3:重新安装mysql数据库服务器

如果使用的是CentOS/RHEL/Fedora/Red Hat/Scientific Linux执行以下yum命令来安装mysql:

# yum install mysql mysql-server

如果您使用的是Debian/Ubuntu Linux,请输入以下apt-get命令:

$ sudo apt-get install mysql-client mysql-server mysql-common

步骤4:还原配置文件和数据库

还原所有配置文件和数据库,如步骤1所示。
查看如何还原mysql数据库的备份:

$ gunzip mysql-blog.08-12-2013-04:00:18.gz
$ mysql -u root -p mysql -e 'CREATE DATABASE blog;'
$ mysql -u root -p blog < mysql-blog.08-12-2013-04\:00\:18

恢复mysql配置文件如下:

# tar xvf /root/mysql-files/mysql.config-files.dd-mm-yyyy.tar.gz -C /root/backups/
# cp /root/backups/etc/my.cnf /etc
# service mysqld restart