the reason
According to my many experiments, I found that to start mysql with docker, you must add the /var/lib/mysql inside the -v mapping, and it will appear if it is missing
ERROR 2002 (HY000): Can't connect to local MySQL
server through socket '/var/run/mysqld/mysqld.sock' (2)
Therefore, the complete command is to create a mysql data directory on the host first, then start mysql, and map it to the host
# mkdir /root/tmp1 # 新建或者保持这个目录内部干净
# docker run -d -p 3307:3306 -v /root/tmp1:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql
# docker exec -it {生成的容器id} bash
After entering the container with exec, and then running mysql -u root -p to
enter the password, the error ERROR 2002 (HY000) will not be reported.