Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


Need some help changing MySQL root password from command line
New on LowEndTalk? Please Register and read our Community Rules.

All new Registrations are manually reviewed and approved, so a short delay after registration may occur before your account becomes active.

Need some help changing MySQL root password from command line

zserozsero Member
edited September 2012 in General

I'm trying to write a script for changing MySQL root password from command line. I'm following the official documentation from here:
http://dev.mysql.com/doc/refman/5.6/en/server-options.html#option_mysqld_init-file

This is the script I wrote:

shopt -s xpg_echo

echo "stopping running MySQL server\n"
invoke-rc.d mysql stop

echo "creating init file in a mysqld readable location\n"

cat > /tmp/mysql-init <<END
UPDATE mysql.user SET Password=PASSWORD('x123') WHERE User='root';
FLUSH PRIVILEGES;
END

echo "running mysqld_safe with init-file in the background\n"

mysqld_safe --init-file=/tmp/mysql-init &

echo "stopping mysql\n"

invoke-rc.d mysql stop

echo "deleting the init file\n"

rm /tmp/mysql-init

echo "starting mysql\n"

invoke-rc.d mysql start

My problem is that I have to start mysqld_safe with & and stop it later. My problem is that I don't know when it's initialized. Do you know any trick for checking that? Now the script goes faster then how mysql initializes. I'm thinking about a for loop what check at every 100 ms if mysql has initialized, and only then stops it. How do you do this?

Comments

  • gbshousegbshouse Member, Host Rep

    Use mysqladmin to reset root password

  • @gbshouse said: Use mysqladmin to reset root password

    you need to know the old one for this

  • ExpertVMExpertVM Member, Host Rep

    Why don't you start up the mysql_safe first, then execute the SQL statement?

    query.sql
    use mysql; UPDATE mysql.user SET Password=PASSWORD('x123') WHERE User='root'; FLUSH PRIVILEGES; END

    then execute this
    mysql < query.sql

    After which, check for exit code
    [svr1:/root]# echo $? 0

    Hope this helps

  • zserozsero Member
    edited September 2012

    It requires root password, like the other solution. I think I'll just settle down for a sleep 5 after mysqld_safe, it works reliably.

Sign In or Register to comment.