Failed to connect MYSQL with workbench , EC2 – DevOps

Error :

Failed to connect to mysql at server ip with user root access denied for user ‘root’@’localhost'(using password:YES)

Solution

1. Open and edit /etc/my.cnf or /etc/mysql/my.cnf, depending on your distribution.

2. Add skip-grant-tables under [mysqld]

3. Restart MySQL

4. You should be able to log in to MySQL now using the below command mysql -u root -p

5. Run mysql> flush privileges;

6. Set new password by ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘NewPassword’;   (here don’t log from the same screen open another ssh session for the same server and remove the key skip-grant-tables and stop and start the mysql but don’t exiting for another where you are already in the mql)

7. Go back to /etc/my.cnf and remove/comment skip-grant-tables

8. Restart MySQL

9. Now you will be able to login with the new password mysql -u root -p

Here might still you face error , if yes follows

Run the from the sql screen where you already in

SELECT user,authentication_string,plugin,host FROM mysql.user;

Output

+——————+——————————————-+———————–+———–+|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 |+——————+——————————————-+———————–+———–+4rowsinset(0.00sec)

In this example, you can see that the root user does in fact authenticate using the auth_socket plugin. To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing, and note that this command will change the root password you set in Step 2:

ALTER USER ‘root’@’localhost’ IDENTIFIED WITH mysql_native_password BY ‘password’;

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

FLUSH PRIVILEGES;

Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:

SELECT user,authentication_string,plugin,host FROM mysql.user;

Output

+——————+——————————————-+———————–+———–+|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 |+——————+——————————————-+———————–+———–+4rowsinset(0.00sec)

You can see in this example output that the root MySQL user now authenticates using a password. Once you confirm this on your own server, you can exit the MySQL shell:

exit

NOW open another screen and test your solution 

mysql>CREATEUSER’root’@’%’IDENTIFIED BY’PASSWORD’;

mysql>GRANTALLPRIVILEGES ON*.*TO’root’@’%’WITHGRANTOPTION;

mysql>FLUSH PRIVILEGES;

From <https://stackoverflow.com/questions/50177216/how-to-grant-all-privileges-to-root-user-in-mysql-8-0>

Source :- 

https://stackoverflow.com/questions/41645309/mysql-error-access-denied-for-user-rootlocalhost

install my sql on amazon ec2

Install an RPM repository package by running the commands below:

sudo yum install https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm

A new repository file has been created inside the /etc/yum.repos.d directory.

$ ls /etc/yum.repos.d
amzn2-core.repo  amzn2-extras.repo  mysql-community.repo  mysql-community-source.repo

You can also view list of configured repositories with yum command.

$ sudo yum repolist

Once the repository has been added, install MySQL 8 server packages on Amazon Linux 2.

sudo amazon-linux-extras install epel -y
sudo yum -y install mysql-community-server

The next step is to start MySQL server services.

sudo systemctl enable –now mysqld

A superuser account ‘root’@’localhost is created with initial password set and stored in the error log file. To reveal it, use the following command:

use the following command:

$ sudo grep ‘temporary password’ /var/log/mysqld.log

Use this initial password to harden the server.

$ sudo mysql_secure_installation -p
Enter password: <INPUT-PRINTED-PASSWORD>

Set new password and set other settings to better secure access to MySQL server.

Securing the MySQL server deployment.

The existing password for the user account root has expired. Please set a new password.

New password: <SET-NEW-PASSWORD>

Re-enter new password: <CONFIRM-NEW-PASSWORD>
The ‘validate_password’ component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.

Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :

… skipping.
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!

You can update root password anytime from MySQL shell.

$ mysql -uroot -p
mysql> ALTER USER ‘root’@’localhost’ IDENTIFIED BY ‘MyNewStrongP@ssw0d!’;

The Password Policy is requires:

  • At least one uppercase letter
  • At least one lowercase letter
  • At least one digit
  • At least one special character
  • Total password length is at least 8 characters.

You have installed MySQL 8 server successfully on Amazon Linux 2 and ready to roll..

Below are other guides on Amazon Linux available in our website.