The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

set_mysql_password in postwwwacctuser fails

Discussion in 'cPanel Developers' started by tgv, Aug 30, 2016.

  1. tgv

    tgv Member

    Joined:
    May 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Hi there,

    So I'm refactoring my old trusty postwwwacctuser bash script, part of which creates a database user with strong password and adds its credentials to .my.cnf -- basically for later use with the famous automysqlbackup script.

    Now, instead of creating a new user, I started trying to update the default MySQL user for an account with the new password.

    All excited about the new(ish) WHM API, I was trying to update the password doing it this way:

    Code:
    #!/bin/bash
    
    CPUSER=$1
    DBUSER=${CPUSER}
    DBPASS=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | head -c 16)
    CNF=/home/${CPUSER}/.my.cnf
    
    #https://confluence2.cpanel.net/display/SDK/WHM+API+1+Functions+-+set_mysql_password
    whmapi1 set_mysql_password user=${DBUSER} password=${DBPASS} cpuser=${CPUSER}
    The problem is with the cpuser parameter:

    - if it's the newly created account user then error looks like
    - if it's root,
    Both messages do not make any sense to me.

    So my questions are:
    - why interactive "SQL Services > Change MySQL User Password" feature works and API call doesn't?
    - what's wrong with the default MySQL user? should I leave it alone? why?
     
  2. cPanelBenny

    cPanelBenny Community Manager, Development, dog scratcher
    Staff Member

    Joined:
    Apr 24, 2014
    Messages:
    44
    Likes Received:
    20
    Trophy Points:
    8
    Location:
    Michigan
    cPanel Access Level:
    Root Administrator
    Twitter:
    There's definitely something wrong with our documentation on that whmapi call, so I've submitted an internal case to get that clarified and updated. In the meantime: I do something similar in a script that I use for setting up a server for testing. For my uses, the uapi is better than trying to use the WHMAPI. Maybe that will work for you?

    uapi --user=$cPanelUser Mysql create_database name=$cPaneluser_db1
    uapi --user=$cPanelUser Mysql create_user name=$cPanelUser_user1 password=$password
    uapi --user=$cPanelUser Mysql set_privileges_on_database user=$cPanelUser_user1 database=$cPanelUser_db1 privileges=DELETE,UPDATE,CREATE,ALTER
     
  3. tgv

    tgv Member

    Joined:
    May 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Thanks Benny,

    Unfortunately uapi doesn't work either when I try to do things with the default user, similar error messages.

    Just to clarify, the original idea was to create a user with access to all account databases (before they exist!) to facilitate daily backups. We do that because it's convenient to have a pile of database dumps aside of huge account archives.

    At first I was auto-creating a user with access to accountname\_% and it kinda worked fine except cPanel interface didn't know how to deal with these users and I had a huge pain mass-migrating accounts. That was years ago, can I ask -- do cPanel GUI, API, migration script etc. support wildcard database names now, in 2016?

    Then I wanted to make use of the invisible and barely documented default MySQL user, which has access to every database already. The only issue is feeding credentials to the backup script: it can parse .my.cfg file all right but I'm not comfortable leaving account password there, and wanted to change the password to a different one. Still not terribly secure but much better. Apparently this doesn't work through API.

    I'm wondering if there's a way for the automysqlbackup script to pick credentials the way phpmyadmin does, is it documented anywhere?
     
  4. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,834
    Likes Received:
    672
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator
    Hello,

    I don't recommend using a wildcard for database names, however you may find this script helpful for assigning ownership of a database or database username to a cPanel account:

    /usr/local/cpanel/bin/dbmaptool

    It's documented at:

    The dbmaptool script - Documentation - cPanel Documentation

    This thread may also help:

    Creating DB, USER, GRANTS and map to an account via SSH

    Thank you.
     
  5. tgv

    tgv Member

    Joined:
    May 29, 2008
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    1
    Thanks Michael,

    I've been using dbmaptool previously in my script, just wanted to move on from it.

    Sorry I don't know how to express it any clearer: I don't want to create new users, I just want to modify the existing, default one.
     
  6. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    30,834
    Likes Received:
    672
    Trophy Points:
    113
    cPanel Access Level:
    Root Administrator

Share This Page