Ubuntu下配置Mysql的办法

不小心还原云主机又忘记备份sql,只好亲自重配一遍了


卸载 mysql

要想顺利下载,首先要卸载旧版本。

  1. 删除 mysql 的数据文件

    1
    sudo rm /var/lib/mysql/ -R
  2. 删除 mysql 的配置文件

    1
    sudo rm /etc/mysql/ -R
  3. 自动卸载 mysql(包括server和client)

    1
    2
    3
    sudo apt-get autoremove mysql* --purge

    sudo apt-get remove apparmor
  4. 检查 mysql 的依赖项

    1
    dpkg --list|grep mysql
  5. 当 4 存在返回值时,使用 sudo apt-get remove xxx 依次卸载

安装 mysql

  1. 刷新 apt 存储库

    1
    sudo apt-get update
  2. 安装 mysql

    1
    sudo apt-get install mysql-server
  3. 初始化 mysql

    1
    sudo mysql_secure_installation
  4. 一大堆配置,如下

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    #1
    VALIDATE PASSWORD PLUGIN can be used to test passwords...
    Press y|Y for Yes, any other key for No: N

    #2
    Please set the password for root here...
    New password: (输入密码)
    Re-enter new password: (重复输入)

    #3
    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them...
    Remove anonymous users? (Press y|Y for Yes, any other key for No) : N

    #4
    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network...
    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y

    #5
    By default, MySQL comes with a database named 'test' that
    anyone can access...
    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N

    #6
    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.
    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y

    建议选择 N+密码+NYNY

  5. 检查服务状态

    1
    systemctl status mysql.service

    如果显示绿色 active(running) 则成功

配置 mysql

  1. 前往服务器目录 /etc/mysql/mysql.conf.d,注释掉关于 bind-address 的配置

  2. 使用命令 sudo mysql -u root -p 进入 root 用户的用户界面

  3. 1
    use mysql;

    选中 database mysql

  4. 1
    update user set host='%' where user='root';

    修改 root 的访问权限为 %,表示允许 root 用户的来自任意 ip 的访问

  5. 1
    alter user 'root'@'%' identified with mysql_native_password by 'pwd';

    对于 root 用户,不论任何来源的访问,均要求使用密码登录,密码为末尾字符串中设置的密码

    此处不能使用 password() 函数来构造密码,最新版 mysql 不支持

    此处不能使用 grant all privileges 来授予权限,最新版 mysql 不支持

  6. 4 和 5 的顺序不能颠倒

好了,此时可以使用 navicat 远程登录 mysql 了

常见错误

  1. 远程连接服务器报错 2003,连接不上服务器 3306 端口

    • 查看服务器安全策略是否开放这个端口

    • 查看上一节的 1

  2. 远程连接服务器报错 1130

    • 查看上一节的 4
  3. 远程连接服务器报错 1698

    • 查看上一节的 5
  4. 远程连接服务器报错 1045

    • 不可以直接 update root 用户的密码,否则你直接设置的密码会被解密,然后发现和你输入的密码不一致

参考文章

Ubuntu16.04彻底卸载MySQL

Ubuntu18.04 安装MySQL

--It's the end.Thanks for your read.--