ubuntu2204,mysql8.x安装

ubuntu 2204, MySQL8.x安装

sudo apt-get update
sudo apt-get upgrade

# 习惯性的先设置一下时区,这里我使用东八区
date -R # 若发现时间正常则无需设置

tzselect

# 依次选择 4 -> 10 -> 1 -> 1

cp /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

date -R

# 同步时间
ntpdate ntp.aliyun.com

date -R

# 开始安装mysql
sudo apt-get install mysql-server
sudo apt-get install mysql-client

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# [mysqld] 下插入一行 写入skip-grant-tables,保存并退出,:wq!

service mysql restart

mysql -u root -p

update user set authentication_string='xxx密码xxx' where user = 'root';

flush privileges;

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'xxx密码xxx';

exit

sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
# 使用#注释掉[mysqld] 下的 skip-grant-tables,保存并退出,:wq!

service mysql restart

mysql -u root -p
# 输入你的密码

常见问题

错误1,修改host为%后无法登录

ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

解决办法:
  1. vim /etc/mysql/mysql.conf.d/mysqld.cnf,在[mysqld] 下加上 skip-grant-tables,退出并保存:wq!
  2. systemctl restart mysql
  3. mysql -u root,此时无需密码
  4. use mysql; , 选择mysql库
  5. update user set authentication_string='' where user='root';,先清空该字段
  6. flush privileges;,刷新权限
  7. ALTER user 'root'@'localhost' IDENTIFIED BY 'xxxxxYour Passwordxxxxxx';,重置密码
  8. exit,退出当前session,测试一下
  9. mysql -u root -p,重新登录即可

若希望所有ip都可连接mysql则
```sql
select user,host,plugin from user;

update mysql.user set host='%' where user='root';

update user set authentication_string='' where user='root';

flush privileges;

ALTER user 'root'@'%' IDENTIFIED BY 'xxxxxYour Passwordxxxxxx';

错误2,外部使用静态ip,无法访问数据库

ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.66.1:3306’ (10061)
PS C:\Users\xxx> mysql -h 192.168.66.1 -u root -P 3306 -p
Enter password: ****
ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.66.1:3306’ (10061)

解决办法

vim /etc/mysql/mysql.conf.d/mysqld.cnf,将[mysqld]

bind-address            = 127.0.0.1
mysqlx-bind-address     = 127.0.0.1

修改成:

bind-address            = 0.0.0.0
mysqlx-bind-address     = 0.0.0.0

你可能感兴趣的:(#,mysql,ubuntu,mysql)