HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlModerate

Running SQL Query on all databases

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
databasesallsqlqueryrunning

Problem

I have one hosting account using cPanel and phpmyadmin.

I have 50 databases under this account, all WordPress.

I need this query modified so that it runs through all databases to update the password.

UPDATE 'wp_users' SET 'user_pass' = MD5('somepassword') WHERE 'user_login' ='admin' LIMIT 1;

Hoping for a solution that can target all databases instead of having to go through them one by one.

Thank you

Solution

I can't say anything about cPanel and phpmyadmin but In general i can do it by writing a simple script.

I have written a shell script for you

#!/bin/bash

# mysql credential 
user="root"
pass="root"

# list of all databases
all_dbs="$(mysql -u $user -p$pass -Bse 'show databases')"        

for db in $all_dbs
     do
        if test $db != "information_schema" 
            then if test $db != "mysql" 
            then mysql -u$user -p$pass $db -sN -e "UPDATE wp_users SET user_pass = MD5('somepassword') WHERE user_login ='admin' LIMIT 1;"
        fi
    fi  
     done

Code Snippets

#!/bin/bash

# mysql credential 
user="root"
pass="root"

# list of all databases
all_dbs="$(mysql -u $user -p$pass -Bse 'show databases')"        

for db in $all_dbs
     do
        if test $db != "information_schema" 
            then if test $db != "mysql" 
            then mysql -u$user -p$pass $db -sN -e "UPDATE wp_users SET user_pass = MD5('somepassword') WHERE user_login ='admin' LIMIT 1;"
        fi
    fi  
     done

Context

StackExchange Database Administrators Q#20240, answer score: 11

Revisions (0)

No revisions yet.