Ubuntu-18.04下安装mysql

2023-02-25,,

安装mysql服务器

1、 sudo apt-get install mysql-server

2、 sudo apt-get install mysql-client

登录问题

安装成功后,我们会发现我们没有登录的权限。

hhz@hhz-virtual-machine:~$ mysql -u root -p
Enter password:
ERROR (): Access denied for user 'root'@'localhost'

然后发现当我们输入 sudo mysql -u root -p 这个命令的时候可以登录(提示输入密码时直接回车即可),显然这不是我们想要的。

那怎么解决这个问题呢?

解决方案:删除root用户,然后重新创建

1、我们先登录:

hhz@hhz-virtual-machine:~$ sudo mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-0ubuntu0.18.04. (Ubuntu) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2、然后查看用户:

mysql> select user,host from mysql.user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| debian-sys-maint | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
rows in set (0.00 sec)

3、删除root帐号

mysql> drop user 'root'@'localhost';
Query OK, rows affected (0.00 sec)

4、重新创建一个root帐号

mysql> create user 'root'@'%' identified by '';
Query OK, rows affected (0.01 sec)

5、接下来我们用 quit; 命令来退出mysql,然后试试用新的帐号登录

hhz@hhz-virtual-machine:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is
Server version: 5.7.-0ubuntu0.18.04. (Ubuntu) Copyright (c) , , Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

ok,登录问题已经完美解决

远程访问问题

1、授权

mysql> grant all privileges on *.* to 'root'@'%' with grant option;
Query OK, rows affected (0.00 sec) mysql> flush privileges;
Query OK, rows affected (0.01 sec)

2、用 sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf 打开这个文件,然后将 bind-address = 127.0.0.1 改成 bind-address = 0.0.0.0 允许任何IP地址访问,如果设置多个请用逗号隔开

3、重启服务: service mysql restart

4、接下来我们在windows下用Navicat来连接试试

Ubuntu-18.04下安装mysql的相关教程结束。

《Ubuntu-18.04下安装mysql.doc》

下载本文的Word格式文档,以方便收藏与打印。