Last active
June 24, 2024 05:15
-
-
Save sxfmol/e83551c378ce0775bebaeff9071de479 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
安装MySQL | |
首先,你需要安装MySQL。在Debian上安装MySQL非常简单。只需执行以下命令即可: | |
sudo apt-get update | |
sudo apt-get install default-mysql-server | |
在安装过程中,你将被要求设置MySQL的root用户密码。记住这个密码,因为稍后可能需要用到它。 | |
#数据库删除 | |
sudo apt-get remove mariadb-server mariadb-client mariadb-common | |
sudo apt-get remove --purge mariadb-server mariadb-client mariadb-common | |
#用户权限 | |
FLUSH PRIVILEGES; | |
#数据库重启 | |
sudo systemctl restart mysql | |
检查MySQL服务是否在运行 | |
在设置MySQL自启动之前,你需要检查MySQL服务是否正在运行。要检查MySQL服务的状态,请运行以下命令: | |
$ sudo systemctl status mysql | |
如果MySQL服务正在运行,你将看到类似以下内容的输出: | |
● mysql.service - MySQL Community Server | |
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled) | |
Active: active (running) since Mon 2021-09-20 16:50:38 UTC; 2 days ago | |
Main PID: 1662 (mysqld) | |
Tasks: 31 (limit: 2344) | |
Memory: 236.4M | |
CGroup: /system.slice/mysql.service | |
└─1662 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid | |
如果MySQL服务未启动,则输出可能会显示“inactive”或“failed”。如果这是你的情况,请运行以下命令启动MySQL服务: | |
$ sudo systemctl start mysql | |
设置MySQL开机启动 | |
要设置MySQL开机启动,在命令行输入以下命令: | |
$ sudo systemctl enable mysql | |
这将使用systemd启动管理器将MySQL添加到开机自启动程序列表中。 | |
重启系统,检查MySQL是否自启动 | |
现在,你可以通过重启系统来测试MySQL是否自启动。在重启系统后,运行以下命令检查MySQL服务的状态: | |
$ sudo systemctl status mysql | |
清理空间 | |
https://www.vultr.com/zh/docs/block-storage/ | |
https://my.vultr.com/blockstorage/subs/detail/?id=f59d13bc-ebf4-4975-a9bc-3ae194bec323#overview | |
Mount block storage: | |
# mkdir /mnt/blockstorage | |
# echo >> /etc/fstab | |
# echo /dev/vdb1 /mnt/blockstorage ext4 defaults,noatime,nofail 0 0 >> /etc/fstab | |
# mount /mnt/blockstorage | |
挂载后的文件 | |
/dev/vdb1 | |
数据迁移:系统盘—>数据盘 | |
不用改配置,实现MySQL数据库迁移 | |
* 停止MySQL服务 | |
service mysqld stop | |
* 移动mysql目录至新挂载的数据盘目录 | |
mv /var/lib/mysql /mnt/blockstorage | |
* 建立软连接,将新的mysql目录链接至/var/lib | |
ln -s /mnt/blockstorage/mysql /var/lib | |
* 重启MySQL服务 | |
service mysqld start | |
数据库备份 | |
https://www.vultr.com/de/docs/backing-up-mysql-databases/ | |
Locate the Data Directory | |
Log in to MySQL server with your root password. | |
$ sudo mysql -u root -p | |
The following SQL will reveal the data directory of your MySQL instance. | |
mysql> select @@datadir; | |
The output is similar to this. | |
+-----------------+ | |
| @@datadir | | |
+-----------------+ | |
| /var/lib/mysql/ | | |
+-----------------+ | |
1 row in set (0.00 sec) | |
Alternate method | |
Locate the MySQL data path without logging to the MySQL server. | |
$ sudo mysqld --verbose --help | grep ^datadir | |
The output is similar to this. | |
datadir /var/lib/mysql/ | |
Backup the Database | |
Stop the MySQL service. | |
$ sudo systemctl stop mysql | |
Create a directory to store the MySQL file backup. Create a parent directory and sub-directories with the current date. This naming scheme allows you to determine the last backup date. | |
$ sudo mkdir -p /var/mysql_backups/2020_07_13_backup | |
Copy the database file with cp. | |
$ sudo cp -R /var/lib/mysql/. /var/mysql_backups/2020_07_13_backup | |
Start the database server. | |
$ sudo systemctl start mysql | |
Restore the Database | |
Stop the MySQL service. | |
$ sudo systemctl stop mysql | |
Rename the current MySQL data directory. This step preserves a backup copy of the current state. | |
$ sudo mv /var/lib/mysql /var/lib/mysql_old | |
Create a new MySQL data directory. | |
$ sudo mkdir /var/lib/mysql | |
Restore the MySQL backup. | |
$ sudo cp -R /var/mysql_backups/2020_07_13_backup/. /var/lib/mysql | |
Change the ownership of /var/lib/mysql to the mysql user. | |
$ sudo chown -R mysql:mysql /var/lib/mysql | |
Start the database server. | |
$ sudo systemctl start mysql | |
Test the Backup | |
Test the backup by logging to the MySQL server as root. | |
$ sudo mysql -u root -p | |
Verify the database schema is correct. | |
mysql> SHOW DATABASES; | |
Your databases are displayed. For example: | |
+--------------------+ | |
| Database | | |
+--------------------+ | |
| information_schema | | |
| mysql | | |
| performance_schema | | |
| sys | | |
+--------------------+ | |
mysql服务器启动 | |
MySQL 运行包括两部分,一部分是服务器端程序 mysqld,另外一部分则是客户端程序 mysql。只有 mysqld 启动了,mysql 客户端才能与之连接。mysqld 的启动方式有 4 种,同样 mysql 的连接方式也有 4 种。 | |
安全模式启动 | |
mysqld_safe --defaults-file=/etc/mysql/my.cnf --user=mysql & | |
如果启动不成功,查看是否有进程启动:ps -ef|grep mysql | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
解决方法:
确保 MySQL 服务器正在运行:
在命令行上检查 MySQL 服务器是否正在运行:
bash
复制代码
sudo systemctl status mysql
如果 MySQL 未运行,启动它:
bash
复制代码
sudo systemctl start mysql
如果 MySQL 没有安装,可以通过以下命令安装:
bash
复制代码
sudo apt update
sudo apt install mysql-server
检查 MySQL 套接字路径:
默认情况下,MySQL 的套接字通常位于 /var/run/mysqld/mysqld.sock。但是,有时候这个路径可能会因为配置或安装方式而不同。
确认 MySQL 的套接字路径:
bash
复制代码
sudo find / -type s -name 'mysqld.sock'
如果找到不同路径的套接字文件,请更新备份脚本中的 -h 参数,指定正确的套接字路径:
bash
复制代码
mysqldump -h localhost -u $DB_USER -p$DB_PASS $DB_NAME > $BACKUP_FILE
替换 -h localhost 中的 localhost 为你的 MySQL 服务器主机名或 IP 地址,或者直接使用找到的正确套接字路径。
确保用户名和密码正确:
确保在备份脚本中使用的用户名和密码正确。可以通过登录到 MySQL 来测试:
bash
复制代码
mysql -u your_username -p
输入密码后,确认能够成功登录。
检查权限问题:
确保执行备份操作的用户具有适当的权限访问 MySQL 服务器和备份目录。
避免在命令行中使用明文密码:
避免在命令行中使用明文密码是一种好的安全实践。可以考虑创建一个 .my.cnf 文件,在其中存储 MySQL 用户的密码,并确保文件权限设置为只有拥有者可以读取:
bash
复制代码
printf "[client]\nuser=%s\npassword=%s\n" "$DB_USER" "$DB_PASS" > ~/.my.cnf
chmod 600 ~/.my.cnf
然后在备份脚本中可以不再包含 -p$DB_PASS,而是直接使用 -u $DB_USER,MySQL 将自动从 .my.cnf 中读取密码。
通过这些步骤,应该能够解决 mysqldump 报错的问题,并成功执行数据库备份操作。