Welcome to Centmin Mod Community
Register Now

Install Reset Forgottten Mariadb 10.4 and above Password

Discussion in 'Add Ons' started by brijendrasial, Oct 21, 2023.

  1. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    Code:
    #!/bin/bash
    
    # Reset Forgotten Mysql Root Password in Rescumode for CentMinMod Installer [CMM]
    
    # Scripted by Brijendra Sial @ Bullten Web Hosting Solutions [https://www.bullten.com]
    
    RED='\033[01;31m'
    RESET='\033[0m'
    GREEN='\033[01;32m'
    YELLOW='\e[93m'
    WHITE='\e[97m'
    BLINK='\e[5m'
    
    
    #set -e
    #set -x
    
    echo " "
    echo -e "$GREEN*******************************************************************************$RESET"
    echo " "
    echo -e $YELLOW"Reset Forgotten Mysql Root Password in Rescumode for CentMinMod Installer [CMM]$RESET"
    echo " "
    echo -e $YELLOW"By Brijendra Sial @ Bullten Web Hosting Solutions [https://www.bullten.com]"$RESET
    echo " "
    echo -e $YELLOW"Web Hosting Company Specialized in Providing Managed VPS and Dedicated Server's"$RESET
    echo " "
    echo -e "$GREEN*******************************************************************************$RESET"
    
    echo " "
    
    echo -e $GREEN"Generating MariaDB Passowrd"$RESET
    echo " "
    
    MYSQL_PASSWORD=`openssl rand -hex 12`
    
    echo " "
    
    echo -e $GREEN"Stopping MariaDB Now"$RESET
    echo " "
    systemctl stop mariadb
    
    echo " "
    
    echo -e $GREEN"Killing All MariaDB Processes"$RESET
    echo " "
    
    echo " "
    
    echo -e $GREEN"Starting MariaDB in Safemode"$RESET
    echo " "
    mysqld_safe --skip-grant-tables > /dev/null 2>&1 &
    
    echo " "
    
    echo -e $GREEN"Waiting for 10 Seconds for MariaDB to Start"$RESET
    echo " "
    sleep 10
    
    echo " "
    
    echo -e $GREEN"Setting New Password for MariaDB"$RESET
    echo " "
    mysql -u root -B -N -e "FLUSH PRIVILEGES;ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_PASSWORD}';FLUSH PRIVILEGES;"
    
    echo " "
    
    echo -e $GREEN"Killing All MariaDB Processes"$RESET
    echo " "
    
    PID=`ps -eaf | grep mysql | grep -v grep | awk '{print $2}'`
    if [[ "" !=  "$PID" ]]; then
       kill -9 $PID
       wait $PID 2>/dev/null
    fi
    
    echo " "
    
    echo -e $GREEN"Restarting MariaDB Now"$RESET
    echo " "
    systemctl start mariadb
    
    echo -e $YELLOW"Your New Password is ${MYSQL_PASSWORD}"$RESET
    echo " "
    
    echo -e $YELLOW"Changing Password in /root/.my.cnf File"$RESET
    echo " "
    sed -i "/password=*/c password=${MYSQL_PASSWORD}" /root/.my.cnf
    
    echo " "
    
    Code:
    git clone https://github.com/Brijendrasial/Reset-forgotten-Mariadb-Password.git
    cd Reset-forgotten-Mariadb-Password
    chmod +x reset-password.sh
    ./reset-password.sh
    


     
    Last edited: Oct 21, 2023
  2. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
  3. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    seriously saying now 10.4. I will soon check on 10.5 and if changes needed I will update.
     
  4. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    So centmin.sh menu option 11, submenu option 0 not working for you?

    Code (Text):
    --------------------------------------------------------
         Centmin Mod Menu 130.00beta01 centminmod.com    
    --------------------------------------------------------
    1).  Centmin Install
    2).  Add Nginx vhost domain
    3).  NSD setup domain name DNS
    4).  Nginx Upgrade / Downgrade
    5).  PHP Upgrade / Downgrade
    6).  Option Being Revised (TBA)
    7).  Option Being Revised (TBA)
    8).  Option Being Revised (TBA)
    9).  Option Being Revised (TBA)
    10). Memcached Server Re-install
    11). MariaDB MySQL Upgrade & Management
    12). Zend OpCache Install/Re-install
    13). Install/Reinstall Redis PHP Extension
    14). SELinux disable
    15). Install/Reinstall ImagicK PHP Extension
    16). Change SSHD Port Number
    17). Multi-thread compression: zstd,pigz,pbzip2,lbzip2
    18). Suhosin PHP Extension install
    19). Install FFMPEG and FFMPEG PHP Extension
    20). NSD Install/Re-Install
    21). Data Transfer (TBA)
    22). Add Wordpress Nginx vhost + Cache Plugin
    23). Update Centmin Mod Code Base
    24). Exit
    --------------------------------------------------------
    Enter option [ 1 - 24 ] 11
    --------------------------------------------------------
    

    Code (Text):
    --------------------------------------------------------
         MariaDB MySQL Upgrade & Management Sub-Menu      
    --------------------------------------------------------
    0). Regenerate New MySQL Root User Password (local server)
    1). MariaDB 5.2.x Branch Upgrade Only
    2). MariaDB 5.2.x to MariaDB 5.5 YUM upgrade
    3). MariaDB 5.5 to MariaDB 10.x YUM upgrade
    4). MariaDB 5.5 to MariaDB 10.1.x YUM upgrade
    5). MariaDB 10.x to MariaDB 10.1.x YUM upgrade
    6). MariaDB 10.1.x to MariaDB 10.2.x YUM upgrade
    7). MariaDB 10.2.x to MariaDB 10.3.x YUM upgrade
    8). MariaDB 10.3.x to MariaDB 10.4.x YUM upgrade
    9). MariaDB 10.4.x to MariaDB 10.5.x YUM upgrade
    10). MariaDB 10.5.x to MariaDB 10.6.x YUM upgrade
    11). Back to Main menu
    --------------------------------------------------------
    Enter option [ 0 - 11 ] 0
    --------------------------------------------------------
    
    This routine will reset the MySQL root user password
    and properly update the /root/.my.cnf file as well.
    
    This routine is only for local MariaDB MySQL servers not remote ones
    
    Do you want to continue? [y/n]: y
    
    Process begins in 10 seconds...
    
    Previous MySQL root password:
    
    [client]
    user=root
    password=yMhHWRlxp92PMcDTdCyz1EloxlIdKL6P
    
    mysqladmin -u root -pyMhHWRlxp92PMcDTdCyz1EloxlIdKL6P password LnepZxCOgHsDizDoTGxaxE2KiaC26elT
    
    New MySQL root user password: LnepZxCOgHsDizDoTGxaxE2KiaC26elT
    
    /root/.my.cnf updated
    
    [client]
    user=root
    password=LnepZxCOgHsDizDoTGxaxE2KiaC26elT
    
    
     
  5. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    I actually tried that but it works only if password is known. I added this script just because when password is lost or mistakenly changes in .my.cnf file.
     
  6. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    I figured as much. You reminded me to look at this centmin.sh menu option 11 submenu option 0 as it has some OS specific routines for EL7 and EL6 but left out checks for EL8 and EL9 as well. So thanks :)
     
  7. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    updated the script to work with 10.4 and above
     
  8. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Cheers. I really should add this to centmin.sh menu option 11 submenu option 0, so playing with new routine that supports both known root and unknown root password by asking end user if they know the root password

    preview

    Code (Text):
    --------------------------------------------------------
         MariaDB MySQL Upgrade & Management Sub-Menu       
    --------------------------------------------------------
    0). Regenerate New MySQL Root User Password (local server)
    1). MariaDB 5.2.x Branch Upgrade Only
    2). MariaDB 5.2.x to MariaDB 5.5 YUM upgrade
    3). MariaDB 5.5 to MariaDB 10.x YUM upgrade
    4). MariaDB 5.5 to MariaDB 10.1.x YUM upgrade
    5). MariaDB 10.x to MariaDB 10.1.x YUM upgrade
    6). MariaDB 10.1.x to MariaDB 10.2.x YUM upgrade
    7). MariaDB 10.2.x to MariaDB 10.3.x YUM upgrade
    8). MariaDB 10.3.x to MariaDB 10.4.x YUM upgrade
    9). MariaDB 10.4.x to MariaDB 10.5.x YUM upgrade
    10). MariaDB 10.5.x to MariaDB 10.6.x YUM upgrade
    11). Back to Main menu
    --------------------------------------------------------
    Enter option [ 0 - 11 ] 0
    --------------------------------------------------------
    
    When you answer no to - Do you know the existing MySQL root password question,
    this routine will regenerate a new MySQL root user password &
    properly update the /root/.my.cnf file as well. To do this MySQL
    server needs to be temporarily shutdown and restarted. To prevent
    visitor shown errors, this routine will also temp shutdown Nginx.
    So you will want to do this at a off peak site/server time ideally.
    
    When you answer yes to - Do you know the existing MySQL root password question,
    this routine will not need to shutdown MariaDB MySQL & Nginx server
    
    This routine is only for local MariaDB MySQL servers not remote ones
    
    Do you want to continue? [y/n]: y
    Do you know the existing MySQL root password set in /root/.my.cnf? [y/n]: y
    
    Process begins in 10 seconds...
    
    Previous MySQL root password:
    
    [client]
    user=root
    password=n192bkwRcevJYX0krSgH5BVRmPshtB8rGNwd
    
    mysqladmin -u root -pn192bkwRcevJYX0krSgH5BVRmPshtB8rGNwd password WfSHjmcKo24VuIymj6867qsIRHuCdXOEaDF
    
    New MySQL root user password: WfSHjmcKo24VuIymj6867qsIRHuCdXOEaDF
    
    /root/.my.cnf updated
    
    [client]
    user=root
    password=WfSHjmcKo24VuIymj6867qsIRHuCdXOEaDF
    


    and if they don't

    Code (Text):
    --------------------------------------------------------
         MariaDB MySQL Upgrade & Management Sub-Menu       
    --------------------------------------------------------
    0). Regenerate New MySQL Root User Password (local server)
    1). MariaDB 5.2.x Branch Upgrade Only
    2). MariaDB 5.2.x to MariaDB 5.5 YUM upgrade
    3). MariaDB 5.5 to MariaDB 10.x YUM upgrade
    4). MariaDB 5.5 to MariaDB 10.1.x YUM upgrade
    5). MariaDB 10.x to MariaDB 10.1.x YUM upgrade
    6). MariaDB 10.1.x to MariaDB 10.2.x YUM upgrade
    7). MariaDB 10.2.x to MariaDB 10.3.x YUM upgrade
    8). MariaDB 10.3.x to MariaDB 10.4.x YUM upgrade
    9). MariaDB 10.4.x to MariaDB 10.5.x YUM upgrade
    10). MariaDB 10.5.x to MariaDB 10.6.x YUM upgrade
    11). Back to Main menu
    --------------------------------------------------------
    Enter option [ 0 - 11 ] 0
    --------------------------------------------------------
    
    When you answer no to - Do you know the existing MySQL root password question,
    this routine will regenerate a new MySQL root user password &
    properly update the /root/.my.cnf file as well. To do this MySQL
    server needs to be temporarily shutdown and restarted. To prevent
    visitor shown errors, this routine will also temp shutdown Nginx.
    So you will want to do this at a off peak site/server time ideally.
    
    When you answer yes to - Do you know the existing MySQL root password question,
    this routine will not need to shutdown MariaDB MySQL & Nginx server
    
    This routine is only for local MariaDB MySQL servers not remote ones
    
    Do you want to continue? [y/n]: y
    Do you know the existing MySQL root password set in /root/.my.cnf? [y/n]: n
    
    Process begins in 10 seconds...
    
    Proceeding with root password reset without known password
    Redirecting to /bin/systemctl stop nginx.service
    mysql -u root -e "UPDATE mysql.user SET Password = PASSWORD('pySwn3tihEWMnquGRoGtvKoQwJ53mfg5Q6P') WHERE User = 'root'; FLUSH PRIVILEGES;"
    
    New MySQL root user password: pySwn3tihEWMnquGRoGtvKoQwJ53mfg5Q6P
    
    /usr/local/src/centminmod/inc/mariadb_submenu.inc: line 79: 3011903 Killed                  mysqld_safe --skip-grant-tables > /dev/null 2>&1
    systemctl start mariadb
    service nginx start
    Redirecting to /bin/systemctl start nginx.service
    MySQL root user password updated: pySwn3tihEWMnquGRoGtvKoQwJ53mfg5Q6P
    
     
  9. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    Thats much needed eva :)
     
  10. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    on which mariadb verion you tested it eva?
     
  11. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    Manually tested MariaDB 10.3 and just triggered automated testing for MariaDB 10.4 and 10.6 default setup options for Centmin Mod 130.00beta01's private AlmaLinux/RockyLinux/Oracle Linux EL8/EL9 configurations :)
     
  12. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    Yes I saw that the reset way is different from version 10.4 and above.
     
  13. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    That was my experience, but seems MariaDB 10.3 tested worked with 10.4 method too. But I did test with MariaDB 10.3 EL8 appstream and not MariaDB 10.3 YUM repo from mariadb.org - so not sure if that made a difference.

    Will see what automated tests reveal :)

    centmin.sh menu option 11 submenu option 0 yes and no known password reset tests amongst other automated tests on AlmaLinux 8 vs Rocky Linux 8 vs Oracle Linux 8

    cmm-github-workflow-actions-mysql-password-reset-tests-01.png
     
  14. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    Keep it posted.
     
  15. eva2000

    eva2000 Administrator Staff Member

    54,907
    12,240
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +18,811
    Local Time:
    3:57 AM
    Nginx 1.27.x
    MariaDB 10.x/11.4+
    First automated test looks good on MariaDB 10.4 for AlmaLinux 8

    cmm-github-workflow-actions-mysql-password-reset-tests-02.png
     
  16. brijendrasial

    brijendrasial Active Member

    208
    154
    43
    Mar 21, 2018
    Ratings:
    +236
    Local Time:
    11:27 PM
    1.13.9
    10.0.22-MariaDB
    awesome