Termux-packages: MySQL need to run as ROOT to connect as a root user in mysql

Created on 31 Jul 2019  路  4Comments  路  Source: termux/termux-packages

Problem description
mysql need to run as ROOT to connect as a root user in mysql

Steps to reproduce
In Tab 1

$ mysqld

In Tab 2

$ mysql -u root
ERROR 1698 (28000): Access denied for user 'root'@'localhost'

when i try executing mysql -u root as a root user it'll be working fine

$ tsu
# mysql -u root
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.4.6-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Expected behavior
mysql can connect without Root Permissions

Additional information
when installing mariadb i got this message

$ pkg install mariadb
Hit:1 https://termux.net stable InRelease
Ign:2 https://dl.bintray.com/grimler/game-packages-21 games InRelease
Ign:3 https://dl.bintray.com/grimler/science-packages-21 science InRelease
Get:4 https://dl.bintray.com/grimler/game-packages-21 games Release [5344 B]
Hit:4 https://dl.bintray.com/grimler/game-packages-21 games Release
Get:6 https://dl.bintray.com/grimler/science-packages-21 science Release [5348 B]
Hit:6 https://dl.bintray.com/grimler/science-packages-21 science Release
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  mariadb
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 16.1 MB of archives.
After this operation, 146 MB of additional disk space will be used.
Get:1 https://termux.net stable/main arm mariadb arm 10.4.6 [16.1 MB]
Fetched 16.1 MB in 36s (446 kB/s)
Selecting previously unselected package mariadb.
(Reading database ... 9501 files and directories currently installed.)
Preparing to unpack .../mariadb_10.4.6_arm.deb ...
Unpacking mariadb (10.4.6) ...
Setting up mariadb (10.4.6) ...
Initializing mysql data directory...
chown: cannot access '/auth_pam_tool_dir': No such file or directory
Cannot change ownership of the '/auth_pam_tool_dir' directory
 to the 'u0_a122' user. Check that you have the necessary permissions and try again.
Processing triggers for man (1.14.5-1) ...

output of termux-info

Updatable packages:
All packages up to date
Subscribed repositories:
https://dl.bintray.com/grimler/science-packages-21 science/stable
https://dl.bintray.com/grimler/game-packages-21 games/stable
https://termux.net stable/main
https://termux.net stable/main
System information:
Linux localhost 3.10.65-gbc393db #1 SMP PREEMPT Thu Oct 6 07:38:15 PHT 2016 armv7l Android
Termux-packages arch:
arm
Android version:
5.1.1
Device manufacturer:
samsung
Device model:
SM-T285

Most helpful comment

Not a bug actually. The DB post-installation message shows:

Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is u0_a123@localhost, it has no password either, but
you need to be the system 'u0_a123' user to connect.

So, mariadb actually has 2 users with all privileges. The first one root can be accessed only from system root and the second one which is your Termux app user (in my case it is u0_a123).

$ mysql -u u0_a123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.6-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Changing root password

  1. Connect to database with:

    mysql -u $(whoami)
    
  2. Update root password with following SQL commands:

    use mysql;
    set password for 'root'@'localhost' = password('YOUR_ROOT_PASSWORD_HERE');
    flush privileges;
    quit;
    
  3. Now you should be able to login with mysql -u root -p:

    $ mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 16
    Server version: 10.4.6-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]>
    

All 4 comments

Not a bug actually. The DB post-installation message shows:

Two all-privilege accounts were created.
One is root@localhost, it has no password, but you need to
be system 'root' user to connect. Use, for example, sudo mysql
The second is u0_a123@localhost, it has no password either, but
you need to be the system 'u0_a123' user to connect.

So, mariadb actually has 2 users with all privileges. The first one root can be accessed only from system root and the second one which is your Termux app user (in my case it is u0_a123).

$ mysql -u u0_a123
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.6-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>

Changing root password

  1. Connect to database with:

    mysql -u $(whoami)
    
  2. Update root password with following SQL commands:

    use mysql;
    set password for 'root'@'localhost' = password('YOUR_ROOT_PASSWORD_HERE');
    flush privileges;
    quit;
    
  3. Now you should be able to login with mysql -u root -p:

    $ mysql -u root -p
    Enter password: 
    Welcome to the MariaDB monitor.  Commands end with ; or \g.
    Your MariaDB connection id is 16
    Server version: 10.4.6-MariaDB MariaDB Server
    
    Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    MariaDB [(none)]>
    

when i tried method you gave.
i get a access denied when executing mysql query

This is what i do
```
$ whoami
u0_a122
$ mysql -u u0_a122
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 10.4.6-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mysql;
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'

Your DB probably is not installed correctly (''@'localhost' is something wrong).
There shouldn't be errors like

chown: cannot access '/auth_pam_tool_dir': No such file or directory
Cannot change ownership of the '/auth_pam_tool_dir' directory
 to the 'u0_a122' user. Check that you have the necessary permissions and try again.

Output should be:

Initializing mysql data directory...
Installing MariaDB/MySQL system tables in '/data/data/com.termux/files/usr/var/lib/mysql' ...
OK

I pushed a fix for DB installation script https://github.com/termux/termux-packages/commit/37fae5cf752a5198bfcea1fc3ac3fb23a5fa7fcc. Let's hope @fornwall will rebuild packages for android-5 branch. Otherwise I can upload debs here.

Thank you! @xeffyr
After patching mysql_install_db with your patch.
The problem is solved.

Should we close the issue now?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

roalyr picture roalyr  路  3Comments

am11 picture am11  路  3Comments

bkdwt picture bkdwt  路  3Comments

StephanBeer picture StephanBeer  路  3Comments

newmania picture newmania  路  3Comments