Initialize root password on MySQL 5.7 - mysql

I'm installing MySQL 5.7 on a new Ubuntu 16.04 server. Since my installation uses Ansible, no root password is defined at installation time, and I set it up later with the mysql_user role in ansible, but the root password stays empty.
I therefore tried to run the command directly on MySQL, without Ansible, but I get the same behaviour : no error on the alter user command but password is not set.
My server configuration :
OS :
Distributor ID: Ubuntu
Description: Ubuntu 16.04.1 LTS
Release: 16.04
Codename: xenial
Database : MySQL 5.7.12-0ubuntu1.1
Step to reproduce :
Install mysql-server-5.7 package without providing a root password.
Connect to mysql from the system root account
And then run the mysql commands :
mysql> select authentication_string from user where host='localhost' and user='root';
+-----------------------+
| authentication_string |
+-----------------------+
| |
+-----------------------+
1 row in set (0,00 sec)
mysql> alter user root#localhost identified by 'mypassword';
Query OK, 0 rows affected (0,00 sec)
mysql> select authentication_string from user where host='localhost' and user='root';
+-----------------------+
| authentication_string |
+-----------------------+
| |
+-----------------------+
1 row in set (0,00 sec)
However, running the same command with a hashed value works :
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*D7B8FEAED940F8F2A0055864C0A59782B4BCED02';
mysql> select authentication_string from user where host='localhost' and user='root';
+-------------------------------------------+
| authentication_string |
+-------------------------------------------+
| *D7B8FEAED940F8F2A0055864C0A59782B4BCED02 |
+-------------------------------------------+
1 row in set (0,00 sec)
And then, I can change the password with the original request :
mysql> alter user root#localhost identified by 'mypassword';
Query OK, 0 rows affected (0,00 sec)
mysql> select authentication_string from user where host='localhost' and user='root';
+-------------------------------------------+
| authentication_string |
+-------------------------------------------+
| *FABE5482D5AADF36D028AC443D117BE1180B9725 |
+-------------------------------------------+
1 row in set (0,00 sec)
Why does the "unhashed" version not work the first time ? Am I missing something or is this a bug in this version of MySQL ?

Related

Cant login to mysql new user?

I've recently downloaded and started using mysql. I've set it up hopefully correct and my root user also logs in. The mysql service is also running.
I ran the following lines to create a datbase and new user, but I could not log in to my new user. Please help. I don't know what I'm doing wrong.
I've searched for a solution online but get confused as to what is the right steps to follow.
Thank you :)
PS C:\Users\Akhil> mysql -u root -p;
Enter password: *****
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server version: 5.6.41-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb |
| performance_schema |
| sakila |
| test |
| world |
+--------------------+
7 rows in set (0.00 sec)
mysql> use mytestdb
Database changed
mysql> create user myuser identified by 'mypass';
ERROR 1396 (HY000): Operation CREATE USER failed for 'myuser'#'%'
mysql> DROP USER myuser;
Query OK, 0 rows affected (0.00 sec)
mysql> create user myuser identified by 'mypass';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON mytestdb.* TO myuser;
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
PS C:\Users\Akhil> mysql -u myuser -p
Enter password: ******
ERROR 1045 (28000): Access denied for user 'myuser'#'localhost' (using password: YES)
PS C:\Users\Akhil>
Why am I getting access denied is beyond me...Help?
I've solved the error. What I did was Login with root user. Then type the following command:-
DROP USER ''#localhost ;
then it worked like a charm!

Mysql skip-grant-tables gives access denied message after password change

After changing mysql password, I did executed skip-grant-tables, so when I try to login to mysql without password I cannot login. It gives access denied message. Why is that
Following are the steps I did. First I installed mysql and changed the password
1) sudo yum install mysql-community-server
2) sudo service mysqld start
3) sudo grep 'temporary password' /var/log/mysqld.log
4) mysql -uroot -p
5) ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass4!';
Then tried to login with skip grant tables
6) sudo service mysqld stop
7) mysqld_safe --skip-grant-tables &
Now I get Access denied message
I recently upgrade my Ubuntu 16.04 to 18.04 and this has worked for me:
First, connect in sudo mysql
sudo mysql -u root
Check your accounts present in your db
SELECT User,Host FROM mysql.user;
+------------------+-----------+
| User | Host |
+------------------+-----------+
| admin | localhost |
| debian-sys-maint | localhost |
| magento_user | localhost |
| mysql.sys | localhost |
| root | localhost |
Delete current root#localhost account
mysql> DROP USER 'root'#'localhost';
Query OK, 0 rows affected (0,00 sec)
Recreate your user
mysql> CREATE USER 'root'#'%' IDENTIFIED BY '';
Query OK, 0 rows affected (0,00 sec)
Give permissions to your user (don't forget to flush privileges)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'#'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0,00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0,01 sec)
Exit MySQL and try to reconnect without sudo.
This will make you login.

cannot create a new user on mysql 5.1.73

This does not seem to work
mysql> SET PASSWORD FOR juser201411#localhost= PASSWORD(".6,y:C2a");
ERROR 1133 (42000): Can't find any matching row in the user table
After that I tried manually adding from mysql.user
mysql> update mysql.user set password=('.6,y:C2a') where User='juser201411' and Host='localhost';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> select User from mysql.user;+------+
| User |
+------+
| root |
| root |
+------+
2 rows in set (0.00 sec)
centos 6.5 x64
Try to use the CREATE USER syntax. For more information see MySQL doc
CREATE USER 'juser201411'#'localhost' IDENTIFIED BY PASSWORD '.6,y:C2a';
For me I had put the line into a txt editor to fill it out and some of my single quotes had gotten converted to smart quotes...hard to see, but obvious once you do(!)

ERROR 1049 (42000): Unknown database

I can't seem to login to my tutorial database development environment:
Ayman$ mysql -u blog -p blog_development
Enter password:
ERROR 1049 (42000): Unknown database 'blog_development'
I can login to the database fine without the blog_development portion:
Ayman$ mysql -u blog -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1858
Not sure what gives as I granted all access:
mysql> GRANT ALL PRIVILEGES ON blog_development.*
-> TO 'blog'#'localhost'
-> IDENTIFIED BY 'newpassword';
Query OK, 0 rows affected (0.01 sec)
mysql> SHOW GRANTS FOR 'blog'#'localhost'
-> ;
+----------------------------------------------------------------------------------------- --------------------+
| Grants for blog#localhost |
+----------------------------------------------------------------------------------------- --------------------+
| GRANT USAGE ON *.* TO 'blog'#'localhost' IDENTIFIED BY PASSWORD '*FE4F2D624C07AAEBB979DA5C980D0250C37D8F63' |
| GRANT ALL PRIVILEGES ON `blog`.* TO 'blog'#'localhost' |
| GRANT ALL PRIVILEGES ON `blog_development`.* TO 'blog'#'localhost' |
+----------------------------------------------------------------------------------------- --------------------+
3 rows in set (0.00 sec)
Anybody have a clue what to try? Thanks! Also, side note- is it weird I have multiple root users?:
mysql> select User from mysql.user;
+------+
| User |
+------+
| root |
| root |
| |
| root |
| |
| blog |
| root |
+------+
7 rows in set (0.00 sec)
Edit: for those asking- I created the database blog with the CREATE DATABASE command in MySql. Here are my active databases:
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| blog |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
blog_development doesn't exist
You can see this in sql by the 0 rows affected message
create it in mysql with
mysql> create database blog_development
However as you are using rails you should get used to using
$ rake db:create
to do the same task. It will use your database.yml file settings, which should include something like:
development:
adapter: mysql2
database: blog_development
pool: 5
Also become familiar with:
$ rake db:migrate # Run the database migration
$ rake db:seed # Run thew seeds file create statements
$ rake db:drop # Drop the database
Very simple solution.
Just rename your database and configure your new database name in your project.
The problem is the when you import your database, you got any errors and then the database will be corrupted. The log files will have the corrupted database name.
You can rename your database easily using phpmyadmin for mysql.
phpmyadmin -> operations -> Rename database to
Its a common error which happens when we try to access a database which doesn't exist. So create the database using
CREATE DATABASE blog_development;
The error commonly occours when we have dropped the database using
DROP DATABASE blog_development;
and then try to access the database.

MySQL: grant permissions denied for CREATE

I setup a database & user along with grant permissions how I normally do and I'm still getting access denied and I'm not sure why:
[root#server23 redditonrails]# mysql -u redditonrails -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 431954
Server version: 5.0.45 Source distribution
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use redditonrails_development;
Database changed
mysql> create table test;
ERROR 1142 (42000): CREATE command denied to user 'redditonrails'#'localhost' for table 'test'
mysql> show grants;
+------------------------------------------------------------------------------------------------+
| Grants for redditonrails#localhost |
+------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'redditonrails'#'localhost' IDENTIFIED BY PASSWORD '*******' |
| GRANT ALL PRIVILEGES ON `redditonrails_test`.`localhost` TO 'redditonrails'#'localhost' |
| GRANT ALL PRIVILEGES ON `redditonrails_development`.`localhost` TO 'redditonrails'#'localhost' |
| GRANT ALL PRIVILEGES ON `redditonrails`.`localhost` TO 'redditonrails'#'localhost' |
+------------------------------------------------------------------------------------------------+
4 rows in set (0.00 sec)
mysql> SELECT USER(),CURRENT_USER();
+-------------------------+-------------------------+
| USER() | CURRENT_USER() |
+-------------------------+-------------------------+
| redditonrails#localhost | redditonrails#localhost |
+-------------------------+-------------------------+
1 row in set (0.00 sec)
I don't believe your syntax is right. You're specifying:
GRANT ALL PRIVILEGES ON
redditonrails_development.localhost
The expected syntax for db-level GRANT is: ON $db.$table. Based on this, you're only granting on the table named "localhost". Change to:
GRANT ALL PRIVILEGES ON
redditonrails_development.*
It would seem that your user redditonrails has all privilages on
redditonrails_development.localhost
which would mean redditonrails_development database and localhost table. what you want is to have there
redditonrails_development.*
witch would mean you have all privilages on all tables (even the new ones you're trying to create)
or at least that's how I see it.