Ubuntu 安装 MySQL 8

一、背景

      做前端的女朋友让我帮忙在远程虚拟机上安装MySQL数据库,根据我多年编程经验,这个应该是几分钟的事情,满口自信答应,结果还是遇到很多坑。阮一峰约莫说过:软件编程更多是一种应用能力,和基础科学研究是不同的。编程就是用已经有的砖头搭建起图纸上的高楼。

      通过Xshell连接虚拟机,然后开始安装MySQL、排查问题、Navicat连接MySQL。

二、安装

 sudo apt-get install mysql-server          //服务端
 sudo apt-get install mysql-client          //客户端
 sudo apt-get install libmysqlclient-dev    //程序编译时链接的库

 service mysql status //查看mysql运行状态
 mysql -u root -p //登录mysql  

 #服务管理
 #启动
 sudo service mysql start
 #停止
 sudo service mysql stop
 #重启
 sudo service mysql restart
 #进入管理员账户
 sudo su

 

 #首先使用以下命令删除MySQL服务器:
 sudo apt-get remove mysql-server

   #查看当前的数据库。
   show databases;

 三、解决安装过程中的报错问题

   1.密码错误

问题:Access denied for user ‘root‘@‘localhost‘ (using password: YES) 

解决办法:

(1)虚拟机cd到 /etc/mysql/mysql.conf.d 目录下,mysql8的相关配置在文件mysqld.cnf中。

(2)vim mysqld.cnf 搜索 /mysqld 按 i 进入输入模式,在[mysqld]下输入 skip-grant-tables ,让mysql跳过密码验证

(3)重启mysql sudo service mysql restart。

(4)登录mysql,键入mysql –uroot –p;直接回车不需要输入密码(Enter)

(5)更新密码。update mysql.user set authentication_string=password('123') where user='root';

(6)flush privileges; 刷新数据库。此时报错语法不正确

 2.修改密码语法错误

问题:ERROR 1064 (42000) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘password(‘123456’)’ at line 1

解决办法:

(1)MySQL8.0后请使用alter修改用户密码。ALTER USER 'root'@'%' IDENTIFIED BY '123';

(2)此时报错 :ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

3. skip-grant-tables无法修改密码

问题:ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement

解决办法:

(1)先执行:flush privileges;

(2)再执行修改密码命令,可以了

(3)报错:ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'%'

4.MySQL8修改密码方法

借鉴 引用:

MySQL5.7和之前的用户修改密码方式:

mysql -uroot -e "Set password=password(‘123’);"
mysql -uroot -p123.com -e "use mysql;update user set authentication_string=password('456') where user='root';"
update mysql.user set authentication_string=password("123") where user='root';

以上三种方法在MySQL8.0以后版本中将不能使用,如果使用了将会导致在正确修改密码是报如下错误:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123';
ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'

如遇上以上问题请使用update语句先清空authentication_string字段,然后再修改密码即可

update user set authentication_string='' where user='root';
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

所以特别提醒童鞋们:

     MySQL8.0后请使用alter修改用户密码,因为在MySQL8.0以后的加密方式为caching_sha2_password,如果使用update修改密码会给user表中root用户的authentication_string字段下设置newpassowrd值,当再使用alter user 'root'@'localhost' identified by 'newpassword'修改密码时会一直报错,必须清空后再修改,因为authentication_string字段下只能是MySQL加密后的43位字符串密码,其他的会报格式错误,所以在MySQL8.0以后能修改密码的方法只能是:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '你的密码';

5.打开MySQL远程连接

(1)虚拟机cd到 /etc/mysql/mysql.conf.d 目录下,mysql 8 的相关配置在文件mysqld.cnf中。

(2) 因为默认3306端口只允许本地访问的,注释掉这行 #bind-address = 127.0.0.1

(3)关闭ubuntu的防火墙
       sudo ufw status # 查看防火墙状态

      sudo ufw enable #打开防火墙

      sudo ufw disable #关闭防火墙

三、总结

     直接定位具体MySQL版本、具体问题,就不会出现因为版本不一样,导致运行命令不同这个问题。 如果一开始直接搜索:mysql8 修改密码 可能就不会出现过程中的那些问题。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
回答: 在Ubuntu安装MySQL 8有几个步骤。首先,你可以通过使用命令sudo apt install mysql-server来安装MySQL服务。\[3\]安装完成后,你可以使用命令mysql -V来查看MySQL的版本。如果你发现安装的是MySQL 5.7版本而不是MySQL 8版本,你可以按照以下步骤进行修改。首先,你需要进入MySQL客户端,可以使用命令mysql -uroot -p。如果你遇到了"ERROR 1698 (28000): Access denied for user 'root'@'localhost'"的错误,那么你需要修改密码。\[2\]进入MySQL命令行后,你可以使用以下命令来修改密码:ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_new_password';其中'your_new_password'是你想要设置的新密码。修改密码后,你就可以使用新密码登录MySQL了。 #### 引用[.reference_title] - *1* *2* [05-ubuntu安装mysql8](https://blog.csdn.net/weixin_46371752/article/details/125369471)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Ubuntu20种安装MySQL8(超全教程)](https://blog.csdn.net/weixin_43441262/article/details/129672673)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fang·up·ad

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值