Granting localhost privileges or creating another user with them - mysql

I connected to MySql by this command:
$ mysql -h localhost
mysql> CREATE USER 'alex1'#localhost IDENTIFIED BY 'password$';
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER privilege(s) for this operation
And now I can't anything (create a new user with CRUD privileges, for instance) because localhost doesn't have enough privileges. I can't connect to MySql with any other credentials because I don't remember them. How do I grant localhost privileges to do that or create another user or a schema?

The problem is the user you are logged in as is the default user that will be used to access MySQL. This user does not have access to create other users.
Its not about 'localhost'.
If you know the username of an account that has grant privileges, specify it manually at the prompt:
$ mysql -u root -p -h localhost
Now it will ask you for the password for the root account for MySQL. This is normally the main "superuser" account that has all permissions.
If you don't know the root credentials and you don't have another user that has these permissions, then you need to reset the root password. Follow the steps in the manual for your operating system.

Related

Mysql not listing all Databases for root user

I have a mysql database running on k8s cluster inside pod. it was previously listing all the databases when i login through mysql -u root -p and then entering password. But my application was not able to connect to that database and was showing 1045, "Access denied for user 'root'#'ipaddress' (using password: YES)" there was just one host which is % and user was root
i have updated secrets as well and restart deployment but still it was showing the above error.
then i ran this command to grant all privileges to root user
GRANT ALL ON root.* TO 'root'#'localhost' IDENTIFIED BY 'password';
it creates one more host for root which is localhost. Now when i try to login with
mysql -u root -p
it is not listing my databases and just showing
And now host is localhost. what should i do to get my database back.
In MySQL permissions are granted for "accounts" which consist of a user name and a host name [1]. So in terms of GRANTS:
myuser#127.0.0.1
myuser#192.168.1.1
The above are two different users. The wildcard in terms of permissions is %. However % and localhost are mutually exclusive as explained here.
So having that in mind you would need to run something close to:
CREATE USER 'root'#'%' IDENTIFIED BY 'changeme';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'root'#'%';
In order to enable connections coming from a different host. Please keep in mind that using the username root should be avoided. Instead, use a dedicated user for your needs.

Mariadb host works on “localhost” but not IP alternative - 127.0.0.1

I am trying to connect to my database through an ash tunnel using sequel pro but it is not working and forces me to use 127.0.0.1 when entering "localhost" which leads to the problem where if I run on the command line:
mysql --host "localhost"
It works
If I run:
mysql --host "127.0.0.1"
I get the access denied error:
ERROR 1698 (28000): Access denied for user 'root'#'localhost'
What is going on?
I have tried:
update user set host='%' where host='localhost'
but this does not work.
Many SQL servers have two or more different user entries for every user that might come in via either localhost or remote. (127.0.0.1 counts as remote).
For example, for the root user, you might have these three user entries.
CREATE USER 'root'#'localhost' IDENTIFIED BY REDACTED;
CREATE USER 'root'#'127.0.0.1' IDENTIFIED BY REDACTED;
CREATE USER 'root'#'%.example.com' IDENTIFIED BY REDACTED;
There's nothing much special about the name root except that it has been granted a lot of privileges when your MySQL was installed. You need to grant the same privileges to the other versions of root#whatever you create.
I created a new user:
CREATE USER 'non-root'#'localhost' IDENTIFIED BY '123';
and then granted them all the same privileges:
GRANT ALL PRIVILEGES ON * . * TO 'non-root'#'localhost';
and then deleted root and renamed non-root to root
and now it finally works.

Remote db connection issues

I am trying to log into mysql using SSH using this command:
mysql -u username -p -h hostname
Where 'username' and 'hostname' and placeholders for the real params. However, the above gives an error :
ERROR 1045 (28000): Access denied for user 'username'#'vps-xxxxxx.xxxx.com' (using password: YES)
Why is it appending the "#vps-xx...." with the remote server username?
Place this on root
GRANT ALL PRIVILEGES ON *.* TO 'USERNAME'#'IP' IDENTIFIED BY 'PASSWORD' with the grant options;
Privileges Provided by MySQL
Once you have finalized the permissions that you want to set up for your new users, always be sure to reload all the privileges.
FLUSH PRIVILEGES;
How To Grant Different User Permissions
Here is a short list of other common possible permissions that users can enjoy.
ALL PRIVILEGES- as we saw previously, this would allow a MySQL
user all access to a designated database (or if no database is
selected, across the system)
CREATE- allows them to create new tables or databases
DROP- allows them to them to delete tables or databases
DELETE- allows them to delete rows from tables
INSERT- allows them to insert rows into tables
SELECT- allows them to use the Select command to read through
databases
UPDATE- allow them to update table rows
GRANT OPTION- allows them to grant or remove other users'
privileges

Can't access MySQL on LAN server

MySql is installed on a Ubuntu server.
I am running Windows 8.
Logging in through Putty. I can log in to database with both root and webadmin user accounts.
I can also log in through my browser, using <server ip address>/phpmyadmin
My problem is when I try to use command line to log in. I am trying that approach because I am developing a webpage to access the database on that server. It fails to connect, so I thought if command line works, the webpage will also work.
commandline:
mysql -u webadmin -p
or
mysql -u root -p
error 1045 (28000): access denied for user 'webadmin'#'localhost'
(using password: yes)
I added an iptables entry to allow mysql and that didn't work.
Also, the firewall on server is inactive.
You need to grant access to the database. You can read the documentation here: https://dev.mysql.com/doc/refman/5.1/en/grant.html
You can run the following (but be careful as it will leave your DB open)
GRANT ALL ON *.* TO 'root'#'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
That will allow root to connect from any IP and do any operations to the DB. If that still doesn't work, you might also need to run the following before the 2 lines above
CREATE USER 'root'#'%' IDENTIFIED BY PASSWORD '[your password]';

MYSQL User Set Permissions host from SSH

How do you set a users host permission via SSH for MySql?
I've managed to accidentally set the wrong domain/ip for my root user in WebMin > MySQL Server permissions. Like I went into hosts to allow remote access and put the wrong address. Now even Webmin cannot get into he database with the credentials.
Login to server via SSH.
Use following command to enter mysql:
mysql --skip-grant-tables -u root -p
skip-grant-tables will cause mysql to abandon privilege system for this session.
Then use grant command to grant any privilege to your root user:
grant all privileges on *.* to 'root'#'my-host-or-ip'