1.环境 ubuntu18.04 华为云远程服务器 2.停止MySQL服务 sudo systemctl stop mysql 3.卸载MySql sudo apt-get remove --purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-* 4.删除相关文件和目录 sudo rm -rf /etc/mysql /var/lib/mysql /var/log/mysql 5.清理未使用的包 sudo apt-get autoremove sudo apt-get autoclean 6.更新包列表 sudo apt-get update 7.安装 sudo apt-get install mysql-server 8.安全配置(可选,但推荐) sudo mysql_secure_installation 9.启动MySQL服务 sudo systemctl start mysql 10.检查状态 sudo systemctl status mysql 11.配置MySQL以允许远程连接(非root 11-16步) sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf 找到:bind-address = 127.0.0.1 更改:bind-address = 0.0.0.0 这将允许来自所有IP地址的连接。 12.重启服务器 sudo systemctl restart mysql 13.登录 mysql -u root -p 密码:1 14.在MySQL中,您需要为允许远程访问的用户创建或修改用户。进入MySQL命令行 CREATE USER 'your_user'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES; 15.配置防火墙 sudo ufw allow 3306/tcp 16.mysql -u your_user -p -h your_server_ip 17.配置MySQL以允许远程连接(root 11-16步) CREATE USER 'root'@'%' IDENTIFIED BY 'your_password'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; FLUSH PRIVILEGES;