How to connect to mysql server on WSL? (1)

Juhan Kim·2024년 4월 6일
0

Note!

No installation of mysql server on WSL, Just connecting to mysql server processing on Window

sudo apt update; \
sudo apt install mysql-client; \

And then run the mysql server by mysql workbench. It's a great tool.

You may have to see and check the server status like below. When you can see like this picture, then the server is running normally. If you cannot see like this, you may have to uninstall and reinstall mysql.

You can find the pid(process id) if you know the port number like below. So, I've checked that the server is running normally.

Or you can find easily by using 'resmon' tool of window. (aka. resource monitor)

If you see the network tab, then you can find the mysql server for sure.

One struggling point that I have experienced was that when I tried to connect to the root user of mysql server which is executing on window like above by WSL, I failed like below.

I tried lots of time to check if the password is not wrong, and see the users and privileges and there seemed nothing wrong.

I came through my mind that the WSL is a another single server which is totally isolated with Window and maybe the 'localhost' should be the problem. Simply put, the host of WSL and window is totally different, so we have to identify it.

You can check your ip address on cmd using 'ipconfig'.

You can also check your ip address of WSL.

For the test, I made a new test user on mysql workbench and then I connected to the user by defining the host ip address.

Finally, I have made it to connect to the mysql server which is running on window by WSL host.

And if you want to run a DML or DDL query, don't forget to give the role to the user. You can select the role as you want.

After that, you can find the user obtained the authority to access the database(schema).

If you are trying to do some query even though you don't have the right authority, then you can see the error like this.

I tested a short python script as below. I used the python ORM(Object Relationship Mapping).

from sqlalchemy import create_engine, text

''''mysql+mysqldb://user:password@localhost/dbname''''
engine = create_engine('mysql+mysqldb://testuser:shinhan!1@192.168.219.101:3306/world', echo=True)

with engine.connect() as conn:
    conn.execute(text("CREATE TABLE some_table (x int, y int)"))
    conn.execute(
        text("INSERT INTO some_table (x, y) VALUES (:x, :y)"),
        [{"x": 1, "y": 1}, {"x": 2, "y": 4}],
    )
    conn.commit()

You can check that the transaction has been successfully committed on mysql workbench like below.

profile
지식에 대한 열망이 있는 사람

0개의 댓글