阿里云Ubuntu 安装卸载 MySQL 并进行远程连接 以及遇到的问题

89 阅读2分钟

注意:阿里云Ubuntu自带Mysql安装包,如果安装过Mysql出现问题卸载重装,没有问题直接安装。

1.卸载MySQL

  • 停止mysql服务

    sudo systemctl stop mysql
    
  • 卸载mysql安装包

    sudo apt-get remove --purge mysql-server mysql-client mysql-common
    
  • 删除Mysql配置文件和数据

    sudo rm -rf /etc/mysql /var/lib/mysql
    
  • 清除残留文件和依赖文件

    sudo apt-get autoremove
    sudo apt-get autoclean
    

2.安装MySQL

  • 更新源

    sudo apt-get update
    
  • 安装Mysql,安装后自动运行

    sudo apt install mysql-server
    
  • 安装完成,查看MySQL运行状况

    sudo service mysql status
    

image-20230915203907119.png

  • 设置开机自启动

    sudo update-rc.d -f mysql defaults
    
  • root登录MySQL,此时是默认方式登录,无密码

    mysql -u root -p
    

    此时弹出输入密码,最好不要输入密码,或者设置密码

  • 使用Mysql数据库

    use mysql;
    

image-20230915203949248.png

  • 查看user表的信息

    select host, user, authentication_string, plugin from user;
    

image-20230915204115842.png

  • 修改root用户的authentication_string

    当 root 用户的 authentication_string 为空且 plugin 为 auth_socket 时,表示 root 用户使用的是操作系统的身份验证,而不是 MySQL 的密码验证。这意味着您无法使用密码直接登录 root 用户。

    将 plugin 修改为 caching_sha2_password 的目的是为了启用 MySQL 的密码验证机制,使 root 用户可以使用密码进行登录。

    在执行 ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password; 命令后,root 用户的密码将更改为新的密码。新密码是您在登录时需要提供的密码,而不是之前的空密码。需要确保提供的密码与之前的空密码不同。

    ALTER USER 'root'@'localhost' IDENTIFIED WITH caching_sha2_password;
    
  • 设置新密码

    如果提示Your password does not satisfy the current policy requirements,将密码设置的复杂一点,默认密码等级需要大小写英文+数字+特殊字符

    alter user 'root'@'localhost' identified by 'Password@123';
    
  • 更改root用户的host

    update user set host='%' where user='root';
    
  • 刷新权限

    FLUSH PRIVILEGES;
    
  • 退出Mysql

    exit
    
  • 修改配置文件允许远程访问

    vim /etc/mysql/mysql.conf.d/mysqld.cnf
    

    输入i进入编辑模式

    找到bind-address 将地址修改为 0.0.0.0

    外界可以访问

image-20230915205101980.png

**Esc+:wq** 保存并退出
  • 重启MySQL服务

    sudo service mysql restart
    
  • 配置安全组

    进入阿里云控制台,选择 云服务器ECS-网络与安全-安全组-安全组ID/名称

image-20230915205417955.png

3.远程连接

image-20230915205645604.png

4.遇到的问题

安装这个mysql真的是看了很多教程,有的不全面,有的多次更新安装包执行导致进程被占用,

以下问题是我找到解决方案的:

E: dpkg was interrupted, you must manually run ‘sudo dpkg --configure -a’ to correct the problem.

E:dpkg被中断,您必须手工运行‘sudo dpkg --configure -a’解决此问题。

sudo dpkg --configure -a
sudo apt-get update
sudo apt-get upgrade
或者
sudo rm /var/lib/dpkg/updates/*
sudo apt-get update
sudo apt-get upgrade

如果本篇文章对你有用的话就点个赞吧!!!!