Mysql installation on aws ec2 instance through userdata - mysql

I am giving this script as a userdata in an ec2 instance to install and setup mysql. The code runs fine manually but when given to ec2 instance as userdata it doesnot creates the database or user.
#!/bin/bash
sudo yum -y update
sudo yum -y install aspell aspell-en httpd24 mysql mysql-server php56 php56-cli php56-gd php56-intl php56-mbstring php56-mysqlnd php56-opcache php56-pdo php56-soap php56-xml php56-xmlrpc php56-pspell --skip-broken
sudo yum list installed
sudo /sbin/chkconfig httpd on
sudo /sbin/chkconfig mysqld on
sudo /sbin/service httpd start
sudo /sbin/service mysqld start
sudo mysqladmin -u root password '123a'
sudo mysql -u root -p123a
CREATE DATABASE db;
CREATE USER '1234'#'localhost' IDENTIFIED BY '123a';
GRANT FILE ON *.* TO '1234'#'localhost';
FLUSH PRIVILEGES;
Mysql version: 5.7.28

Your grant command is not right, nowadays it is simply without password
sudo mysql -u root -p123a -e "CREATE DATABASE db DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;use db;create user 'db'#'localhost' identified by '123a'; grant all on testdb.* to 'db';GRANT ALL ON db.* TO ' db '#'localhost';GRANT ALL ON db.* TO ' db '#'%';FLUSH PRIVILEGES;"

Related

MySQL password not resetting even after re-installation on Ubuntu

I forgot my Mysql Root password.I have uninstalled Mysql using the following commands-
sudo apt-get remove --purge mysql*
sudo apt-get autoremove
sudo apt-get autoclean
After uninstalling MySQL I ran the following command to verify if MySQL was uninstalled properly-
dpkg --get-selections | grep mysql
Then I executed the following command to install MySQL-
sudo apt-get install mysql-server
I didn't get any message to set root password.
After Executing the following command it ask's for root password which i haven't set.
mysql -u root -p
Please help.
Stop the MySQL server process with the command service mysql stop
Start the MySQL server with the command mysqld_safe —skip-grant-tables —skip-networking
Connect to the MySQL server as the root user with the command mysql -u root
Exec the following MySQL commands to reset the root password:
mysql> use mysql;
​mysql> update user set authentication_string=password('NEWPASSWORD') where user='root';
​mysql> flush privileges;
​mysql> quit
Where NEWPASSWORD is the new password to be used.
Restart the MySQL daemon with the command service mysql restart. You should now be able to log into MySQL with the new password.

how to recover mysql password in ubuntu 16.04

Mysql version - mysql Ver 14.14 Distrib 5.7.18, for Linux (x86_64) using EditLine wrapper.
I had forgotten my password and tried many commands online.Also the problem is that grant tables command not working in my terminal.
~$ cat /etc/issue
Ubuntu 16.04.3 LTS \n \l
~$ aptitude show mysql-server |grep Version
Version: 5.7.20-0ubuntu0.16.04.1
In file: /etc/mysql/debian.cnf are two important lines:
user = debian-sys-maint
password = <unique password>
Use that user and password to login to mysql:
Once logged in as debian-sys-maint you can:
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
The following adjustments works for my setup:
$ sudo mkdir /var/run/mysqld
$ sudo chown mysql: /var/run/mysqld
$ sudo kill -9 $(sudo cat /var/run/mysqld/mysqld.pid)
1) Stop the Database Server.
sudo systemctl stop mysql
2) Restarting the Database Server Without Permission Checking. For this following command
sudo mysqld_safe --skip-grant-tables --skip-networking &
3) Connect to the database as the root user
mysql -u root
4) Change the Root Password
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
5) Restart the database server narmally
sudo kill `cat /var/run/mysqld/mysqld.pid`
sudo systemctl start mysql
Now login with new password in database:
mysql -u root -p
Or you can follow the following link:
https://www.digitalocean.com/community/tutorials/how-to-reset-your-mysql-or-mariadb-root-password
I had tried all the approaches but didn't work for me. The following approach worked. I am hopeful that it will work on different versions of ubuntu.
OS info:
mysql --version
mysql Ver 8.0.28-0ubuntu0.20.04.3 for Linux on x86_64 ((Ubuntu))
First, stop MySQL service using command
sudo systemctl stop mysql
Other commands didn't work and here how I start the MySQL server without any permission-checking. To do so, run:
// open the mysql systemd configuration file your default text editor.
// In my case, it is nano editor.
sudo systemctl edit mysql
// Add the following 3 lines
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
Reload systemd configuration using command:
sudo systemctl daemon-reload
Start MySQL service
sudo systemctl start mysql
Now, connect to MySQL server as root user without password:
sudo mysql -u root
mysql> FLUSH PRIVILEGES;
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'add-newpassword-here';
Revert the modified systemd configuration
sudo systemctl revert mysql
You are all Done.

ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using password: NO) on MAC OSX

I'm trying to reinstall mysql on my MAC OS X Yosemite. for this I followed the instruction as mentioned below
sudo rm /usr/local/mysql
sudo rm -rf /usr/local/mysql*
sudo rm -rf /Library/StartupItems/MySQLCOM
sudo rm -rf /Library/PreferencePanes/MySQL*
vim /etc/hostconfig and removed the line MYSQLCOM=-YES-
rm -rf ~/Library/PreferencePanes/MySQL*
sudo rm -rf /Library/Receipts/mysql*
sudo rm -rf /Library/Receipts/MySQL*
sudo rm -rf /var/db/receipts/com.mysql.*
I also tried
brew uninstall mysql
after this I installed mysql using homebrew using command brew install mysql.
After installation when I tried to run mysql -u root It throws the following error
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: NO)
I didn't set password for mysql.I don't know what's going wrong.Any suggestion will be appreciated. Thank you
I installed mysql with homebrew and got the same problem as you because mysql has had an existing database with an existing password there. See this article for more details.
$ brew services stop mysql
$ sudo pkill mysqld
$ sudo rm -rf /usr/local/var/mysql/ # NOTE: this will delete your existing database!!!
$ brew postinstall mysql
$ brew services restart mysql
$ mysql -uroot
Now sql generates an aliatory password that appears in the last screen.
sudo mysql -u root -h 127.0.0.1 -p
Enter password: (aliatory password)
we can change it
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new-password';
I have resolved this issue for myself.
Please check my github with this link: https://github.com/LeVanTuan/access_sql
ERROR 1045 (28000): Access denied for user 'root'#'localhost' (using
password: YES/NO)
Fix bug access denied on macos 10.12 (Sierra)
install mysql (https://dev.mysql.com/downloads/mysql/) if already
installed, skip this step
Update password of 'root' to access.
Open Terminal (Launchpad -> Other -> Terminal or (Command + space) ->
Type 'terminal' )
Then type follow below:
export PATH=$PATH:/usr/local/mysql/bin/
sudo mysqld_safe --skip-grant-tables
then, restart Terminal (quit then open again)
then, keep type:
export PATH=$PATH:/usr/local/mysql/bin/
mysql -u root mysql
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY '123456';
\q
sudo /usr/local/mysql/support-files/mysql.server start
extra: start mysql: sudo /usr/local/mysql/bin/mysqld_safe
--skip-grant-tables stop mysql: sudo /usr/local/mysql/support-files/mysql.server stop
I hope it will help you
In my case with a Mac M1 and Mysql 8.0.29-arm64 this answer worked for me:
https://www.codegrepper.com/code-examples/sql/how+to+reset+root+password+in+mysql+m1+mac;
Run the server in safe mode with privilege bypass:
> sudo mysqld_safe --skip-grant-tables
In a new Terminal window
> mysql -u root
UPDATE mysql.user SET authentication_string=null WHERE User='root';
FLUSH PRIVILEGES;
exit;
Then
> mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED WITH caching_sha2_password BY 'yourpasswd';
Uninstalling mysql completely and installing with an installer solved the problem for me.
Solution:
Remove mysql complete from your computer
Download and Install mysql without brew. Specify your desire password here or based on the version the installer might mention you a password
set the path for for mysql
export PATH=$PATH:/usr/local/mysql/bin
Check whether its installed properly mysql --version
mysql -uroot p to login
change the password if required mysql> SET PASSWORD FOR 'root'#'localhost' = PASSWORD('root')
I tried all the commands mentioned in the stackoverflow, but nothing worked. Finally, I deleted every mysql folder in mac and reinstalled mysql. Finally the command
mysql -u root
worked for me. Then atlast, I set password for the mysql root by editing the configuration file.
**on your mac terminal type
mysql -u root -p
You forgot to reset your temporary password. Re-install mysql, use the temporary password to connect to mysql and run:
mysql> SET PASSWORD = PASSWORD('your_new_password');
Update for anyone still not finding a resolution with a new M1 Mac. This completely clears your database FYI!!
First run brew services stop mysql
I found it easier to just open finder and use the got to folder and open /opt/homebrew, under the var folder I deleted the MySQL folder and under locks I deleted the MySQL lock files.
brew install MySQL and followed the install again.

cannot access mysql database remotely on ubuntu

Here are the steps I followed for accessing the database remotely:
For machine A
sudo apt-get install mysql-server
sudo mysql_install_db
set bind_address=0.0.0.0 on path /etc/mysql/my.cnf
started mysql database on machine A
mysql -u root -p
created a user using following command
create user 'techrocks'#'%' identified by 'myass';
grant all privileges on *.* to 'techrocks'#'%';
flush privileges;
exit mysql and restart mysql, then sudo /etc/init.d/mysql restart
ON machine B I installed mysql-server and then tried to access mysql of machine A
mysql -u techrocks -h ip-address-of-machine-A -p
mysql started but when I tried to create a database as
create database mydb1;
I got access denied error
Access denied for user 'teckrocks'#'%' to database 'mydb1'
Open my.cnf (/etc/mysql/my.cnf)
bind = IP of remote
then try to connect..

MySQL: How to reset or change the MySQL root password?

How do I change the MySQL root password and username in ubuntu server? Do I need to stop the mysql service before setting any changes?
I have a phpmyadmin setup as well, will phpmyadmin get updated automatically?
Set / change / reset the MySQL root password on Ubuntu Linux. Enter the following lines in your terminal.
Stop the MySQL Server: sudo /etc/init.d/mysql stop
(In some cases, if /var/run/mysqld doesn't exist, you have to create it at first: sudo mkdir -v /var/run/mysqld && sudo chown mysql /var/run/mysqld
Start the mysqld configuration: sudo mysqld --skip-grant-tables &
Login to MySQL as root: mysql -u root mysql
Replace YOURNEWPASSWORD with your new password:
For MySQL < 8.0
UPDATE mysql.user SET Password = PASSWORD('YOURNEWPASSWORD') WHERE User = 'root';
FLUSH PRIVILEGES;
If your MySQL uses new auth plugin, you will need to use: update user set plugin="mysql_native_password" where User='root'; before flushing privileges.
Note: on some versions, if password column doesn't exist, you may want to try:
UPDATE user SET authentication_string=password('YOURNEWPASSWORD') WHERE user='root';
Note: This method is not regarded as the most secure way of resetting the password, however, it works.
For MySQL >= 8.0
FLUSH PRIVILEGES;
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YOURNEWPASSWORD';
FLUSH PRIVILEGES;
Last step:
As noted in comments by #lambart, you might need to kill the temporary password-less mysql process that you started, i.e. sudo killall -9 mysqld and then start normal daemon: sudo service mysql start
References:
Set / Change / Reset the MySQL root password on Ubuntu Linux
How to Reset the Root Password (v5.6)
How to Reset the Root Password (v8.0)
The only method that worked for me is the one described here (I am running ubuntu 14.04). For the sake of clarity, these are the steps I followed:
sudo vim /etc/mysql/my.cnf
Add the following lines at the end:
[mysqld]
skip-grant-tables
sudo service mysql restart
mysql -u root
use mysql
select * from mysql.user where user = 'root'; - Look at the top to determine whether the password column is called
password or authentication_string
UPDATE mysql.user set *password_field from above* = PASSWORD('your_new_password') where user = 'root' and host = 'localhost'; - Use the proper password column from above
FLUSH PRIVILEGES;
exit
sudo vim /etc/mysql/my.cnf
Remove the lines added in step 2 if you want to keep your security standards.
sudo service mysql restart
For reference : https://dev.mysql.com/doc/refman/5.7/en/resetting-permissions.html
The official and easy way to reset the root password on an ubuntu server...
If you are on 16.04, 14.04, 12.04:
sudo dpkg-reconfigure mysql-server-5.5
If you are on 10.04:
sudo dpkg-reconfigure mysql-server-5.1
If you are not sure which mysql-server version is installed you can try:
dpkg --get-selections | grep mysql-server
Updated notes for mysql-server-5.7
Note that if you are using mysql-server-5.7 you can not use the easier dpkg-reconfigure method shown above.
If you know the password, login and run this:
UPDATE mysql.user SET authentication_string=PASSWORD('my-new-password') WHERE USER='root';
FLUSH PRIVILEGES;
Alternatively, you can use the following:
sudo mysql_secure_installation
This will ask you a series of questions about securing your installation (highly recommended), including if you want to provide a new root password.
If you do NOT know the root password, refer to this Ubuntu-centric write up on the process.
See for more info:
https://help.ubuntu.com/16.04/serverguide/mysql.html
https://help.ubuntu.com/14.04/serverguide/mysql.html
What worked for me (Ubuntu 16.04, mysql 5.7):
Stop MySQL
sudo service mysql stop
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
On another console, log in without a password.
mysql -uroot mysql
Then:
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
EXIT;
Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
Start the MySQL service normally.
sudo service mysql start
I am sharing the step by step final solution to reset a MySQL password in Linux
Ubuntu.
Reference taken from blog (dbrnd.com)
Step 1:
Stop MySQL Service.
sudo stop mysql
Step 2:
Kill all running mysqld.
sudo killall -9 mysqld
Step 3:
Starting mysqld in Safe mode.
sudo mysqld_safe --skip-grant-tables --skip-networking &
Step 4:
Start mysql client
mysql -u root
Step 5:
After successful login, please execute this command to change any password.
FLUSH PRIVILEGES;
Step 6:
You can update mysql root password .
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
Note: On MySQL 5.7, column Password is called authentication_string.
Step 7:
Please execute this command.
FLUSH PRIVILEGES;
At first run this command:
sudo mysql
and then you should check which authentication method of your MySQL user accounts use.So run this command
SELECT user,authentication_string,plugin,host FROM mysql.user;
now you can see something like this already :
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
in the table that is in the above , you can see that all of your mysql users accounts status & if you have set a password for root account before you see mysql_native_password in plugin column instead auth_socket.
All in all for change your root password you should run :
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Be sure to change password to a strong password of your choosing.
Then for reload your server to put your new changes into effect run this;
FLUSH PRIVILEGES;
So again check the authentication methods which has employed by your mysql , by this command:
SELECT user,authentication_string,plugin,host FROM mysql.user;
and now the output is :
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *3636DACC8616D997782ADD0839F92C1571D6D78F | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *CC744277A401A7D25BE1CA89AFF17BF607F876FF | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
as you can see in the grant table your root account has mysql_native_password
.
now you can exit MYSQL shell
exit;
That's it.just you should restart mysql by sudo service mysql restart.
Now you can login to mysql as a root account with your password easily.
I faced problems with ubuntu 18.04 and mysql 5.7, this is the solution
Try restart mysql-server before execution the comands
sudo service mysql restart
MYSQL-SERVER >= 5.7
sudo mysql -uroot -p
USE mysql;
UPDATE user SET authentication_string=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;
MYSQL-SERVER < 5.7
sudo mysql -uroot -p
USE mysql;
UPDATE user SET password=PASSWORD('YOUR_PASSWORD') WHERE User='root';
UPDATE user SET plugin="mysql_native_password";
FLUSH PRIVILEGES;
quit;
Change the MySQL root password.
This method exposes the password to the command-line history, these commands should be run as root.
Login through mysql command line tool:
mysql -uroot -poldpassword
Run this command:
SET PASSWORD FOR root#'localhost' = PASSWORD('newpassword');
or
Run this command, which sets a password for the current user ('root' for this case) :
SET PASSWORD = PASSWORD('newpassword');
Stop MySQL
sudo service mysql stop
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
5.Log in without a password.
mysql -uroot mysql
6.Update the password for the root user.
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
EXIT;
Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
Start the MySQL service normally.
sudo service mysql start
If you would like to change the MySQL root password, in a terminal enter:
sudo dpkg-reconfigure mysql-server-5.5
The MySQL daemon will be stopped, and you will be prompted to enter a new password.
This works like charm I did it for Ubuntu 16.04.
Full credit to below link as I got it from there.
[https://coderwall.com/p/j9btlg/reset-the-mysql-5-7-root-password-in-ubuntu-16-04-lts][1]
Stop MySQL
sudo service mysql stop
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Start MySQL manually, without permission checks or networking.
sudo mysqld_safe --skip-grant-tables --skip-networking &
Log in without a password.
mysql -uroot mysql
Update the password for the root user.
make sure at atleast root account gets updated by the below query.
make some selection and check the existing values if you like
UPDATE mysql.user SET
authentication_string=PASSWORD('YOURNEWPASSWORD'),
plugin='mysql_native_password' WHERE User='root';
EXIT;
Turn off MySQL.
sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
Start the MySQL service normally.
sudo service mysql start
In my case this option helped : https://stackoverflow.com/a/49610152/13760371
Thank you, Rahul.
except for the following moment, when I try entered command:
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='%';
the console issued a warning:
1681 'password' is deprecated and will be removed in a future release
cured with this command:
UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
MySQL version 5.7.X
My variant:
1. > sudo service mysql stop
2. > sudo mkdir /var/run/mysqld
3. > sudo chown mysql: /var/run/mysqld
4. > sudo mysqld_safe --skip-grant-tables --skip-networking &
5. > mysql -uroot mysql
6. > UPDATE mysql.user SET authentication_string=CONCAT('*', UPPER(SHA1(UNHEX(SHA1('NEWPASSWORD'))))), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
7. > \q;
8. > sudo mysqladmin -S /var/run/mysqld/mysqld.sock shutdown
9. > sudo service mysql start
This solution belongs to the previous version of MySQL.
By logging in to MySQL using socket authentication, you can do it.
sudo mysql -u root
Then the following command could be run.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Details are available here .
This is the solution for me. I work at Ubuntu 18.04:
https://stackoverflow.com/a/46076838/2400373
But is important this change in the last step:
UPDATE mysql.user SET authentication_string=PASSWORD('YOURNEWPASSWORD'), plugin='mysql_native_password' WHERE User='root' AND Host='localhost';
For Ubuntu 18.04 and mysql version 14.14 Distrib 5.7.22 follow the below step to reset the mysql password.
Step 1
sudo systemctl stop mysql
Step 2
sudo systemctl edit mysql
This command will open a new file in the nano editor, which you'll use to edit MySQL's service overrides. These change the default service parameters for MySQL. This file will be empty, so add the following content:
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid --skip-grant-tables --skip-networking
Step 3
sudo systemctl daemon-reload
sudo systemctl start mysql
Step 4
sudo mysql -u root
Step 5
FLUSH PRIVILEGES;
Step 6
UPDATE mysql.user SET authentication_string = PASSWORD('new_password') WHEREuser = 'root';
Step 7
UPDATE mysql.user SET plugin ='mysql_native_password' WHERE user = 'root';
Step 8
sudo systemctl revert mysql
and finally
sudo systemctl restart mysql
Now enjoy
Echoing rogerdpack's comment: if you don't know the MySQL root password and you don't care about MySQL data/settings, you can reinstall it and reset the root's password as follows:
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo rm -rf /var/lib/mysql
sudo apt-get install -y mysql-server mysql-client
During the installation, you can choose the root's password:
If you know your current password, you don't have to stop mysql server.
Open the ubuntu terminal.
Login to mysql using:
mysql - username -p
Then type your password.
This will take you into the mysql console.
Inside the console, type:
> ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_password';
Then flush privileges using:
> flush privileges;
Then you are all done.
You don't need all this. Simply log in:
mysql -u root -p
Then change the current user's password as the mysql> prompt:
mysql> set password=password('the_new_password');
mysql> flush privileges;
Most of the answers on this topic are outdated; two major changes have occurred in MySQL up until the writing of this answer:
1- the 'Password' field in the user table has been replaced by 'authentication_string' column.
2- the 'Password' encryption function : PASSWORD("of some text") is deprecated.
Please refer to this link for further information:dev.mysql.com/doc/refman/8.0/en/resetting-permissions.html
1.Open nano / vim to create a file with the following content and Save file as ~/mysql-pwd
ALTER USER 'root'#'localhost' IDENTIFIED BY 'NEWPASSWORD';
Stop mysql sudo systemctl stop mysql
Run sudo mysqld -init-file=~/mysql-pwd
Restart mysql sudo systemctl start mysql
Now login mysql -u root -p. password will be your NEWPASSWORD
When you use MySQL's PASSWORD() on the system where you want to change the password, it can cause the password turn up in a MySQL log in cleartext [source]. Keeping them, their backups etc. as secure as the password sounds like nightmare to me, so I rather like to do it as follows:
On your local machine, run this with your password:
mysql -u someuser -p < <(echo "SELECT PASSWORD('mypass');")
Note the space in front to prevent it from turning up in the bash history (for other distros than Ubuntu, this might work differently – source).
On your server machine, execute the following command to change its MySQL root password (replace myhash with your password's hash as printed by the first command):
mysql -u root -p < <(echo "SET PASSWORD FOR root#localhost = 'myhash';")
Optionally, let's be a bit paranoid: On your local machine, clear your terminal screen with clear and purge your virtual terminal scrollback, to hide the cleartext password appearing in the command above.
To update the "root" Mysql user password you must have in mind that you will need of super user permissions for that. If you have super user privilegies, try the following commands:
MySQL 5.7.6 and later
sudo su
service mysql stop
mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
\q;
exit
mysql -u root -p MyNewPass
MySQL 5.7.5 and earlier
sudo su
service mysql stop
mysql -u root
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('MyNewPass');
\q;
exit
mysql -u root -p MyNewPass
As mysql documentation on the password() function says:
This function was removed in MySQL 8.0.11.
This invalidates pretty much all existing answers for mysql v8.0.11 and newer.
Per mysql documentation the new generic way to reset the root password is as follows:
The preceding sections provide password-resetting instructions
specifically for Windows and Unix and Unix-like systems.
Alternatively, on any platform, you can reset the password using the
mysql client (but this approach is less secure):
Stop the MySQL server if necessary, then restart it with the
--skip-grant-tables option. This enables anyone to connect without a password and with all privileges, and disables account-management
statements such as ALTER USER and SET PASSWORD. Because this is
insecure, if the server is started with the --skip-grant-tables
option, it enables --skip-networking automatically to prevent remote
connections.
Connect to the MySQL server using the mysql client; no password is
necessary because the server was started with --skip-grant-tables:
shell> mysql
In the mysql client, tell the server to reload the grant tables so that account-management statements work:
mysql> FLUSH PRIVILEGES;
Then change the 'root'#'localhost' account password. Replace the password with the password that you want to use.
To change the password for a root account with a different host name
part, modify the instructions to use that host name.
mysql> ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';
You should now be able to connect to the MySQL server as root using the
new password. Stop the server and restart it normally (without the
--skip-grant-tables and --skip-networking options).
You can use this command:
UPDATE mysql.user SET Password=PASSWORD('newpwd') WHERE User='root';
after that please use flush:
FLUSH PRIVILEGES;
If you know the 'root' users password, log in to mysql with that credentials. Then execute the following query to update the password.
ALTER USER 'root'#'localhost' IDENTIFIED BY 'new_passowrd';
The steps below worked for me. I'm using MySQL 8.* on Ubuntu
Stop MySQL service and check status to confirm the service stopped
sudo systemctl stop mysql
sudo systemctl status mysql
Edit the systemd config file so you can access MySQL without permission check
sudo systemctl edit mysql
Copy and paste the following 3 lines
[Service]
ExecStart=
ExecStart=/usr/sbin/mysqld --skip-grant-tables --skip-networking
After pasting the lines CTRL+0 to save and then CTRL+X to exit
Reload mysql service and start it (starts the service with --skip-grant-table)
sudo systemctl daemon-reload
sudo systemctl start mysql
5.Now connect to MySQL server without password
sudo mysql -u root
6.Load the grant tables by running
mysql> FLUSH PRIVILEGES;
Reset the root password
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH
mysql_native_password BY 'YourPasswordHere';
Close the mysql connection
mysql> exit
Revert the modification done on the mysql systemd file
sudo systemctl revert mysql
Reload the mysql daemon for changes to take place.
sudo systemctl daemon-reload
Lastly restart the MySQL service
sudo systemctl restart mysql
Now connect to mysql with the new password set in step 7
You can visit this link reset root password for mysql 8 for more details.
Instead of resetting the password there is a work around on the local machine if you have setup phpmyadmin to connect without giving the password or username. Check this out by starting mysql, apache etc. I have xampp installed in my local machine. So starting the xampp will start all the necessary services. Now going to http://localhost/phpmyadmin shows me all the databases. This confirms that you have saved the username and passsword in the config file of phpmyadmin which can be found in the phpmyadmin install location. If you have xampp installed the phpmyadmin folder can be found in the root folder of xampp installation. Search for the word password in the config.inc.php file. There you will find the password and username.
You can easily change the mysql password if deployed on xampp through provided phpadmin gui.
phpMyAdmin -> User Accounts -> Edit Privileges (Select the intended user) -> Change Password (Tab)
for mysql 5.6 this command works and you can set password through the wizard:
sudo dpkg-reconfigure mysql-server-5.6
I had to go this route on Ubuntu 16.04.1 LTS. It is somewhat of a mix of some of the other answers above - but none of them helped. I spent an hour or more trying all other suggestions from MySql website to everything on SO, I finally got it working with:
Note: while it showed Enter password for user root, I didnt have the original password so I just entered the same password to be used as the new password.
Note: there was no /var/log/mysqld.log only /var/log/mysql/error.log
Also note this did not work for me:
sudo dpkg-reconfigure mysql-server-5.7
Nor did:
sudo dpkg-reconfigure --force mysql-server-5.5
Make MySQL service directory.
sudo mkdir /var/run/mysqld
Give MySQL user permission to write to the service directory.
sudo chown mysql: /var/run/mysqld
Then:
kill the current mysqld pid
run mysqld with sudo /usr/sbin/mysqld &
run /usr/bin/mysql_secure_installation
Output from mysql_secure_installation
root#myServer:~# /usr/bin/mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: no
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : y
New password:
Re-enter new password:
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
Dropping test database...
Success.
Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!