Pycharm Community Edition connects to Mysql8.* in WSL2

At present 2023.08.13, the default WSL version in Windows 11 is already 2, the default Ubuntu version in WSL2 is already 22.04, and the default Mysql version in Ubuntu 22.04 is already 8.*.


Install mysql in Wsl 2

The method of installing Mysql in WSL2 refers to the official Microsoft document [ Starting to use the database on the Windows Subsystem for Linux ], which can be eaten with confidence.

To install MySQL on a Linux distribution running on WSL, simply follow the instructions for installing MySQL on Linux in the MySQL documentation. wsl.confSystem support may need to be enabled in the configuration file first .

Example using the Ubuntu distribution:

  1. Open the Ubuntu command line and update the available packages:sudo apt update
  2. After updating the package, install MySQL with the following command:sudo apt install mysql-server
  3. Confirm the installation and get the version number:mysql --version
  4. Start the MySQL server/check status:systemctl status mysql
  5. To open a MySQL prompt, enter:sudo mysql
  6. To see available databases, enter at the MySQL prompt:SHOW DATABASES;
  7. To create a new database, enter:CREATE DATABASE database_name;
  8. To delete a database, enter: DROP DATABASE database_name;

Install the Pycharm plugin

In the community version of Pycharm, there is no DATABASE label by default, you need to use a third-party plug-in, the first is the installation of the plug-in

  1. ctrl + alt + sOpen the settings panel
  2. PluginsSearch in , databaseselect the first install.
    insert image description here

set up mysql

By default, the Mysql installation under Wsl can be used directly without a password to sudo mysqllog in.

  1. Edit the configuration file vi /etc/mysql/mysql.conf.d/mysqld.cnfand comment out the lines 31 and 32.
 31 # bind-address          = 127.0.0.1
 32 # mysqlx-bind-address   = 127.0.0.1

Notice:

  1. Yes mysqld.cnf, no mysql.cnf, note the name;
  2. It is to comment out these two lines, not to uncomment!
  1. Log in to MySQL and directly sudo mysqlexecute the following command to modify the host value of the root user.
# 使用mysql 数据库
mysql > use mysql;
# 特定用户的host 修改
mysql > update user set host='%' where user='root';
# 指定用户的授权
mysql > grant all privileges on test.* to root@'%'
  1. Add user "test" for remote access.
# root用户没有SYSTEM_USER权限,需要先赋权
grant system_user on *.* to 'root';
# mysql 8.0 以后需要用如下命令开启远程服务,其中添加了 用户名/密码:test/123456 的用户
CREATE USER 'test'@'%' IDENTIFIED BY '123456';
GRANT ALL ON *.* TO 'test'@'%'; 
ALTER USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
FLUSH PRIVILEGES;

After executing the above code, restart the mysql service service mysql restart.

remote access

The specific operation is shown in the figure below:
(The text is a bit off, just make do with it)
Please add a picture description

References

  1. Detailed steps for remote calling and connecting (navicat) mysql 8.* in wsl2 under windows
  2. 解决mysql8 提示 ERROR 1410 (42000): You are not allowed to create a user with GRANT
  3. Get started with databases on Windows Subsystem for Linux

Guess you like

Origin blog.csdn.net/weixin_43302112/article/details/132256586