Engineering · 1 min read
連線 URI 是標準式,flag 只是把它拆開
同一個 URI 就裝下全部五個座標,跨引擎長得一樣;CLI flag 只是把這條 URI 拆開,真正的坑是大小寫撞名:mysql 的 -P 是 port、-p 是密碼。
同一個 URI 形狀就裝下全部五個座標,而且跨引擎長得一模一樣:
scheme://user:password@host:port/database
- Postgres:
postgresql://u:[email protected]:5432/db - Mongo:
mongodb://u:[email protected]:27017/db - MySQL:概念上同形狀,但
mysqlCLI 只吃 flag。
CLI 的 flag 其實就是這條 URI 被拆開——-h=host,port/user/db 就是其餘幾塊。真正的坑是大小寫撞名:
- psql:
-p=port(小寫)、-U=user(大寫)、沒有帶密碼值的 flag(用PGPASSWORD或 URI)。 - mysql:
-P=port(大寫)、-p=密碼且要黏著(-pSecret,中間一有空格就會被當成 database 名)、-u=user。
這次的實例: psql postgresql://postgres:[email protected]:5433/demo_db 原封不動就連上了——證明只要用 URI,就完全繞過猜 flag 這件事。
記一條 URI 就好,flag 是它被拆開的樣子;不確定 flag 時,直接丟 URI。
參考來源
- PostgreSQL libpq connection — https://www.postgresql.org/docs/current/libpq-connect.html
- Postgres connection strings and psql — https://tapoueh.org/blog/2019/09/postgres-connection-strings-and-psql/
Related: 回到遠端資料庫連線總覽;另見五個座標與 MySQL CLI 參數。