xo / usql

Universal command-line interface for SQL databases
MIT License
8.88k stars 347 forks source link

mariadb password #344

Closed loynoir closed 2 years ago

loynoir commented 2 years ago

Given

Reproduce

Actual

kenshaw commented 2 years ago

@loynoir could you share more information about what the issue is? Please check the syntax of your .usqlpass file, and ensure it has permissions 0600. Additionally, if there are characters in your username/password/database name/etc that are "not URL safe", they need to be encoded using the URL % style, as it's a quirk/requirement of how usql treats database DSNs exclusively as URLs.

loynoir commented 2 years ago

@kenshaw

$ cat docker-compose.yml
services:
  mariadb:
    image: >-
      mariadb:10.8.3-jammy@sha256:31b72b164a6bf86d6d0df3e27be53682742b6078188802fc3d974298129a77a4
    environment:
      MARIADB_ROOT_PASSWORD: password
$ docker-compose up --remove-orphans --force-recreate
$ docker-compose exec mariadb hostname -i
172.27.0.2
$ usql 'maria://root:password@172.27.0.2:3306/mysql' -c 'select now();' && echo OK
OK
$ echo 'maria:172.27.0.2:3306:mysql:root:password' > ~/.usqlpass
$ chmod 0600 ~/.usqlpass
$ usql 'maria://172.27.0.2:3306/mysql' -c 'select now();' && echo OK
error: mysql: 1045: Access denied for user ''@'172.27.0.1' (using password: NO)
nineinchnick commented 2 years ago

You do have to specify the user when connecting, as there is no default for it. Usql uses it to match an entry from usqlpass.

loynoir commented 2 years ago

@nineinchnick

Ah, careless mistake in reproduce.

Add username, still error.

$ cat docker-compose.yml
services:
  mariadb:
    image: >-
      mariadb:10.8.3-jammy@sha256:31b72b164a6bf86d6d0df3e27be53682742b6078188802fc3d974298129a77a4
    environment:
      MARIADB_ROOT_PASSWORD: password
$ docker-compose up --remove-orphans --force-recreate
$ docker-compose exec mariadb hostname -i
172.28.0.2
$ usql 'maria://root:password@172.28.0.2:3306/mysql' -c 'select now();' && echo OK
OK
$ echo 'maria:172.28.0.2:3306:mysql:root:password' > ~/.usqlpass
$ chmod 0600 ~/.usqlpass
$ usql 'maria://root@172.28.0.2:3306/mysql' -c 'select now();' && echo OK
error: mysql: 1045: Access denied for user 'root'@'172.28.0.1' (using password: NO)
nineinchnick commented 2 years ago

Maria is an alias. Can you try using mysql in usqlpass?

loynoir commented 2 years ago

@nineinchnick It works.

$ echo 'maria:172.28.0.2:3306:mysql:root:password' > ~/.usqlpass
$ chmod 0600 ~/.usqlpass
$ usql 'maria://root@172.28.0.2:3306/mysql' -c 'select now();' && echo OK
error: mysql: 1045: Access denied for user 'root'@'172.28.0.1' (using password: NO)
$
$ echo 'mysql:172.28.0.2:3306:mysql:root:password' > ~/.usqlpass
$ chmod 0600 ~/.usqlpass
$ usql 'maria://root@172.28.0.2:3306/mysql' -c 'select now();' && echo OK
OK
$ usql 'maria://172.28.0.2:3306/mysql' -c 'select now();' && echo OK
OK
kenshaw commented 2 years ago

@nineinchnick should we change github.com/xo/dburl/passfile.Entry.Equals to support the scheme aliases?

nineinchnick commented 2 years ago

Yes, aliases should be unique anyway and it'll make a much better ux.

kenshaw commented 2 years ago

I recall now the reason I didn't implement the support for aliases is that there's just so many of them, and I didn't want people having problems/issues with their .usqlpass file if there was a typo or something. Explicit over shortcuts. I realize now in hindsight that general use by people not familiar with the code would expect the scheme aliases to work.

I imagine adding scheme alias support will just create more "support issues" than otherwise. I think there's only one of 2 ways forward:

1) Document that scheme aliases don't work with .usqlpass 2) Change how passfile handles matching to support aliases

kenshaw commented 2 years ago

@nineinchnick I agree; I'll implement this now.