There are two techniques
TECHNIQUE #1 : Use an init file
INITFILE=/var/lib/mysql/init-file.sql
echo "SET @newpass = PASSWORD('mypass');" > ${INITFILE}
echo "CREATE USER 'root'@'localhost' IDENTIFIED BY @newpass;" >> ${INITFILE}
chown mysql:mysql ${INITFILE}
service mysql stop
service mysql start --init-file=${INITFILE}
rm -f ${INITFILE}
TECHNIQUE #2 : Inject User into mysql.user
With --skip-grant-tables
enabled, run this:
REPLACE INTO mysql.user SET
Host = 'root',
User = 'localhost',
Password = PASSWORD('whateverpasswordiwant'),
Select_priv = 'Y',
Insert_priv = 'Y',
Update_priv = 'Y',
Delete_priv = 'Y',
Create_priv = 'Y',
Drop_priv = 'Y',
Reload_priv = 'Y',
Shutdown_priv = 'Y',
Process_priv = 'Y',
File_priv = 'Y',
Grant_priv = 'Y',
References_priv = 'Y',
Index_priv = 'Y',
Alter_priv = 'Y',
Show_db_priv = 'Y',
Super_priv = 'Y',
Create_tmp_table_priv = 'Y',
Lock_tables_priv = 'Y',
Execute_priv = 'Y',
Repl_slave_priv = 'Y',
Repl_client_priv = 'Y',
Create_view_priv = 'Y',
Show_view_priv = 'Y',
Create_routine_priv = 'Y',
Alter_routine_priv = 'Y',
Create_user_priv = 'Y',
Event_priv = 'Y',
Trigger_priv = 'Y',
Create_tablespace_priv = 'Y',
ssl_type = '',
ssl_cipher = '',
x509_issuer = '',
x509_subject = '',
max_questions = 0,
max_updates = 0,
max_connections = 0,
max_user_connections = 0
;
Then, restart mysql as usual.
See my post In place upgrade from MySQL 5.5 to 5.6.11 removes all users from user table
Besides not running mysql_upgrade
after upgrading MySQL from major subversion to major subversion (E.g 5.1 to 5.5), having the wrong permissions/ownership on the performance_schema
folder and files within the data directory can also cause this error.
For example, a fresh install of MySQL 5.5's RPM will set the ownership of that folder and files within it to root:root
. To fix this, you will need to change the ownership to mysql:mysql
.
E.g.
chown -R mysql:mysql /var/lib/mysql/performance_schema
ls -al /var/lib/mysql
drwx------. 2 mysql mysql 4096 Jun 10 10:29 performance_schema
Best Answer
As you are using the docker mariadb image the best way is to set the environment variables when running the image:
ref: https://hub.docker.com/_/mariadb/
These will case the database and user to be created with grants (ref)