初学MySQL装在了Ubuntu Linux 虚拟机上面 造成了一直连接不成功的局面(困住了我一个星期,很难受!!!!!!)
造成我巨大困扰的原因就是我使用的虚拟机软件是 vbox(Oracle VM VirtualBox)
原因是我的网络模式为如图:
NAT模式
如图 NAT模式无法实现主机连通虚拟机(需要通过端口!!)
然鹅!!!!
网上的几乎所有解决问题的帖子都没有涉及到此方面问题(烦躁QWQ)
既然好不容易写一次博客,那就写的详细一点吧
1.安装mysql
apt -get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev
三条命令搞定
2.查看默认用户名和密码
新版的MySQL默认用户名并不是 root 进入配置文件
sudo cat /etc/mysql/debian.cnf
[sudo] password for thin-dev:
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint --用户名
password = YXry2UP4YPNXnBhD --密码
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = YXry2UP4YPNXnBhD
socket = /var/run/mysqld/mysqld.sock
3.使用默认用户名和密码登录
mysql -udebian-sys-maint -p
mysql -udebian-sys-maint -p
Enter password: --复制下来即可
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.32-0ubuntu0.20.04.2 (Ubuntu)
Copyright (c) 2000, 2023, Oracle and/or its affiliates.
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.
mysql>
这样就登录上了
4.修改用户名密码
show databases;
show databases;
+--------------------+
| Database |
+--------------------+
| Test |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.01 sec)
这里有我创建的Test ,可以忽略!
切换数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
将root用户密码置空:
update user set authentication_string='' where user='root';
mysql> update user set authentication_string='' where user='root';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
为root用户设置密码
ALTER user 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '123456';
配置MySQL远程
#修改配置文件,注释掉 bind-address = 127.0.0.1
$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
# 保存退出,然后进入mysql服务,执行授权命令:
$ mysql -uroot -p
#进入数据库后
use mysql;
select host from user where user='root';
将root的host改掉
update user set host = '%' where user ='root';
需要刷新
flush privileges
退出数据库后刷新(重启)数据库
sudo service mysql restart
正常情况下奥,到这步就可以正常连接数据库了!
but vbox不可以(两点原因)
1.因为NAT模式的原因,这里需要端口映射才可以正常连接
将端口设置好就可以啦,
2.ip地址的问题
这里用的IP地址并不是虚拟机得出的IP
ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:36:d3:79:1f txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 10.0.2.15 netmask 255.255.255.0 broadcast 10.0.2.255
inet6 fe80::a00:27ff:fe41:a1e8 prefixlen 64 scopeid 0x20<link>
ether 08:00:27:41:a1:e8 txqueuelen 1000 (Ethernet)
RX packets 11133 bytes 1114631 (1.1 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10036 bytes 3145588 (3.1 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 16224 bytes 2591077 (2.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 16224 bytes 2591077 (2.5 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
并不是这里的ip!!!!!!!!
并不是这里的ip!!!!!!!!
并不是这里的ip!!!!!!!!
这里可以看到哪些是虚拟机生成的
我这里因为创建过好多虚拟机,所以会有点多,找i到你要连接的那个虚拟机
这里的IP就是你需要的真正的虚拟机地址!!!!!
这里的IP就是我上面所找到的IP
这样子就🆗l!!!!!!