MacBookPro m1 docker install mysql, and use idea to connect to the local database
Docker install mysql steps:
1. Pull the image
docker pull --platform linux/x86_64 mysql:5.7
2. View the image and start mysql
## 查看你本地所有镜像
docker images
## 由于我这里之前有拉取过,可以把多余的镜像删掉,没有可以跳过
docker rmi IMAGE ID
## 设置端口号,密码并运行(这里我使用的是3316端口,密码123456,可以根据自己喜好去修改)
docker run -it --name mysql-test -p 3316:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql/mysql-server
## 启动完后可查看
docker ps -a
3. Enter the command in the terminal (mysql-test here must be the same as the name you installed)
## 进入编辑器后左侧出现:bash-4.4# 即成功进入
docker exec -it mysql-test bash
4. Login to mysql
Login error:
bash-4.4# mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
At first I checked other solutions, but nothing worked. After the following command is executed, it will be fine
bash-4.4# mysql -h 127.0.0.1 -p 3316 -u root -p
Enter password:
ERROR 1130 (HY000): Host '127.0.0.1' is not allowed to connect to this MySQL server
bash-4.4# netstat -nat |grep :3316
bash: netstat: command not found
bash-4.4# mysql -h loaclhost -p 3316 -u root -p
Enter password:
ERROR 2005 (HY000): Unknown MySQL server host 'loaclhost' (-2)
bash-4.4# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 8.0.28 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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>
5. Modify user permissions
## 使用 mysql 自带数据库
use mysql;
## 再进行修改
update user set host = ‘%’ where user = ‘root’;
## 刷新权限控制
lush privileges;
## 查询是否更新成功
select user, host from user;
6. Open idea to connect to mysql
Click Database - "+ - "Data Source - "MySQL
to fill in the connection port password, etc.:
Pord:填写设置的端口,我这里是3316
User:root
Password:填写设置的密码,我这里是123456
Database:我直接选择了我新建的库,当然也可以不选择
Then click Test Connection, click OK after no problem
to connect successfully: