In a script, I want to make sure, a mysql user has no privileges at all on a specific database. So I do:
revoke all privileges on `testdb`.* from 'testuser'#'%'
But if the user does not have any privileges on this database I get this error:
There is no such grant defined for user 'testuser' on host '%'
What is absolutely correct, but my script throws an error now and stops. I do not want to make my script ignoring all errors from this statement.
So is there something like
revoke all privileges if exist ...
I could not find anything about that in the mysql manual.
The mysql command has an -f option which prevents the script from aborting on errors, so you might want to try this:
mysql -u myuser -f < script.sql
Of course this will also ignore other errors which you might not want to be ignored...
Also this unfortunately does not work in combination with the -e option. However if you run mysql from a bash script anyway this can easily be circumvented by using a heredoc:
mysql -u myuser -f <<EOF
REVOKE ALL PRIVILEGES ...
EOF
Related
I just had to change [dedicated CentOs] servers at Godaddy.
After I uploaded my .sql database backup file to the site using FTP, I connected to my site using SSH.
After changing to super user using the su - command, i tried using the following code to restore my database:
[root#sXXX.XXX.XXX.XXX ~]# mysql -u alaskasl_destiny -p alaskasl_freakout < /home/alaskasl/backup/databases/alaskasl_freakout.sql
I get the following ERROR 1044 (42000): Access denied for user 'alaskasl_destiny'#'localhost' to database 'alaskasl_freakout'
I can't figure out what I am doing wrong. This command has always worked for me in the past
First and foremost, if alasas1... is your real username, I would replace it with 'username'. Never give that info out, especially on a public place like Stackoverflow.
That said, a few things to check
1. You need to know the root user's login credentials for this new mysql instance. If this is a new setup the user is root with no password, so you should be able to use the following command:
$ mysql -u root
Once you do get in to mysql, you will see a prompt similar to:
mysql>
at that prompt you can type
mysql> show databases;
and a list of databases will display. Does the database you expect to be in there in there?
if not, here is what you need to do:
mysql> CREATE DATABASE 'database_name';
mysql> CREATE USER 'username' IDENTIFIED BY 'password';
mysql> GRANT ALL PRIVILEGES ON *.database_name TO 'username';
mysql> exit
At this point what you did was create the database, create a user with a password in mysql, and granted access to that database for that user. Once you type exit, you should now be back at the command prompt.
Now you can run mysql -u username -p database_name < /path/to/sql_file.sql
I use this command to create .sql files of all tables in a huge mysql database:
mysqldump -h localhost --user=username -p --tab=/var/tmp database_name
The command above works perfectly but is prompting for the password.
Before i run this command i need to run:
GRANT FILE ON *.* TO 'username'#'%'
otherwise i get the error:
Got error: 1086: "File '/var/tmp/a_ab_text.txt' already exists" when executing 'SELECT INTO OUTFILE'
a_ab_text is the first table of the database.
Is there a way to get around this as I need to create a script that can be daily run as a cronjob.
To be clear a need a way to have the password in the script but typing the password after the --password '********' does not work.
And i need a way to overwrite the tables every day as we export all tables daily to a local server giving timestamp to the back-up file in order to have a dayly back-up version.
And of course i need a way to get around the command GRANT FILE ON *.* TO 'username'#'%' and restart the mysql server.
Thank you
Could anyone help me please? I wolud like to know how to change user in MySql. That is, I created another user besides the default user root but i want to be logged in to the user account i created instead of being logged in to root automatically. Is there anyway I can do that? I've tried using MySql Console but its not working, maybe I don't have the right statements to do that. thanks
In command prompt
mysql -h host -u username -p
Form mysql documentation:
shell> mysql --user=user_name --password=your_password db_name
Then type an SQL statement, end it with “;”, \g, or \G and press Enter.
you can create different user and add different privileges to user as below:
CREATE USER 'username'#'%' IDENTIFIED BY
PASSWORD '*AAB3E285149C0135D51A520E1940DD3263DC008C';
GRANT SELECT ON database_name.table_name TO 'username'#'%';
And use below command to log in:
mysql -uusername -ppassword databasename;
I want to begin writing queries in MySQL.
show grants shows:
+--------------------------------------+
| Grants for #localhost |
+--------------------------------------+
| GRANT USAGE ON *.* TO ''#'localhost' |
+--------------------------------------+
I do not have any user-id but when I want to make a user I don't have privilleges, also I don't know how to make privileges when even I don't have one user!
mysql> CREATE USER 'parsa'#'localhost' IDENTIFIED BY 'parsa';
ERROR 1227 (42000): Access denied; you need (at least one of) the CREATE USER pr
ivilege(s) for this operation
I tried to sign in as root:
mysql> mysql -u root -p;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
-u root -p' at line 1
mysql> mysql -u root -p root;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'mysql
-u root -p root' at line 1
No, you should run mysql -u root -p in bash, not at the MySQL command-line.
If you are in mysql, you can exit by typing exit.
You may need to set up a root account for your MySQL database:
In the terminal type:
mysqladmin -u root password 'root password goes here'
And then to invoke the MySQL client:
mysql -h localhost -u root -p
I was brought here by a different problem.
Whenever I tried to login, i got that message because instead of authenticating correctly I logged in as anonymous user. The solution to my problem was:
To see which user you are, and whose permissions you have:
select user(), current_user();
To delete the pesky anonymous user:
drop user ''#'localhost';
This is something to do with user permissions. Giving proper grants will solve this issue.
Step [1]: Open terminal and run this command
$ mysql -uroot -p
Output [1]:
This should give you mysql prompt shown below
Step [2]:
mysql> CREATE USER 'parsa'#'localhost' IDENTIFIED BY 'your_password';
mysql> grant all privileges on *.* to 'parsa'#'localhost';
Syntax:
mysql> grant all privileges on `database_name`.`table_name` to 'user_name'#'hostname';
Note:
hostname can be IP address, localhost, 127.0.0.1
In database_name/table_name, * means all databases
In hostname, to specify all hosts use '%'
Step [3]: Get out of current mysql prompt by either entering quit / exit command or press Ctrl+D.
Step [4]: Login to your new user
$ mysql -uparsa -pyour_password
Step [5]: Create the database
mysql> create database `database_name`;
You might want to try the full login command:
mysql -h host -u root -p
where host would be 127.0.0.1.
Do this just to make sure cooperation exists.
Using mysql -u root -p allows me to do a a lot of database searching, but refuses any database creation due to a path setting.
If you are in a MySQL shell, exit it by typing exit, which will return you to the command prompt.
Now start MySQL by using exactly the following command:
sudo mysql -u root -p
If your username is something other than root, replace 'root' in the above command with your username:
sudo mysql -u <your-user-name> -p
It will then ask you the MySQL account/password, and your MySQL won't show any access privilege issue then on.
First, if you are unfamiliar with the command line, try using phpmyadmin from your webbrowser. This will make sure you actually have a mysql database created and a username.
This is how you connect from the command line (bash):
mysql -h hostname -u username -p database_name
For example:
fabio#crunchbang ~ $ mysql -h 127.0.0.1 -u fabio -p fabiodb
connect mysql with sudo & gives permission for the necessary user using,
sudo mysql -u user;
GRANT ALL PRIVILEGES ON database_name.* TO 'user'#'localhost';
#Nickparsa … you have 2 issues:
1). mysql -uroot -p
should be typed in bash (also known as your terminal) not in MySQL command-line. You fix this error by typing
exit
in your MySQL command-line. Now you are back in your bash/terminal command-line.
2). You have a syntax error:
mysql -uroot -p;
the semicolon in front of -p needs to go. The correct syntax is:
mysql -uroot -p
type the correct syntax in your bash commandline. Enter a password if you have one set up; else just hit the enter button. You should get a response that is similar to this:
Hope this helps!
Most Developers log-in to server(I assume you r having user-name and password for mysql database) then from Bash they switch to mysql> prompt then use the command below(which doesn’t work
mysql -h localhost -u root -p
What needs to be done is use the above command in the bash prompt--> on doing so it will ask for password if given it will take directly to mysql prompt and
then database, table can be created one by one
I faced similar deadlock so sharing the experience
I had the command correct per above answers, what I missed on was on the Workbench, where we mention 'Limit Connectivity from Host' for the user, it defaults to "%" - change this to "localhost" and it connects fine thereafter!
I'm using roles to confer least privilege on my database application users. I kept getting 'ERROR 1044 (42000): Access denied for user...' until I RTFM and discovered I had to give each user a default role(s) in order their account could be authenticated when they logged in.
#create a role
CREATE ROLE 'rolename';
#give necessary privileges to role
GRANT INSERT, UPDATE, DELETE, SELECT ON database.table TO 'rolename';
#create user
CREATE USER 'username'#'host' IDENTIFIED BY 'password';
#give the user a role(s)
GRANT 'rolename' TO 'username'#'host';
#set the user's default otherwise it's ERROR 1044
SET DEFAULT ROLE 'rolename' FOR 'username'#'host';
I'm running MySQL 5.x on my local Windows box and, using MySQL administrator, I can't connect to the databases I created using the root account. The error I get is:
MySQL Error number 1045 Access denied
for user 'root'#'localhost' (using
password: YES)
I can't recall changing root account credentials, but I know I tried to give other computers on the LAN access to the db by adding their IPs. One thing I did for one of the IPs was to specify access to the account 'root' instead of root, i.e. I surrounded root with single quotation chars. All using MySQL administrator. Could this be the reason why i can't login using root?
Also, is there a way to create a new or reset the root account? As previously mentioned, I have full access to my box.
See these questions
How to change MySQL root password to default?
How do I retrieve my MySQL username and password?
How do I change the password of the root user in MySQL?
You can use the init files. Check the MySQL official documentation on How to Reset the Root Password (including comments for alternative solutions).
So basically using init files, you can add any SQL queries that you need for fixing your access (such as GRAND, CREATE, FLUSH PRIVILEGES, etc.) into init file (any file).
Here is my example of recovering root account:
echo "CREATE USER 'root'#'localhost' IDENTIFIED BY 'root';" > your_init_file.sql
echo "GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost' WITH GRANT OPTION;" >> your_init_file.sql
echo "FLUSH PRIVILEGES;" >> your_init_file.sql
and after you've created your file, you can run:
killall mysqld
mysqld_safe --init-file=$PWD/your_init_file.sql
then to check if this worked, press Ctrl+Z and type: bg to run the process from the foreground into the background, then verify your access by:
mysql -u root -proot
mysql> show grants;
+-------------------------------------------------------------------------------------------------------------+
| Grants for root#localhost |
+-------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'root'#'localhost' IDENTIFIED BY PASSWORD '*81F5E21E35407D884A6CD4A731AEBFB6AF209E1B' |
See also:
No Password – No Problem at Everything MySQL
Bug #28331: Unclear error message when CREATE USER fails due to duplicate key
This worked for me:
https://blog.dotkam.com/2007/04/10/mysql-reset-lost-root-password/
Step 1: Stop MySQL daemon if it is currently running
ps -ef | grep mysql - checks if mysql/mysqld is one of the running processes.
pkill mysqld - kills the daemon, if it is running.
Step 2: Run MySQL safe daemon with skipping grant tables
mysqld_safe --skip-grant-tables &
mysql -u root mysql
Step 3: Login to MySQL as root with no password
mysql -u root mysql
Step 4: Run UPDATE query to reset the root password
UPDATE user SET password=PASSWORD("value=42") WHERE user="root";
FLUSH PRIVILEGES;
In MySQL 5.7, the 'password' field was removed, now the field name is 'authentication_string':
UPDATE user SET authentication_string=PASSWORD("42") WHERE
user="root";
FLUSH PRIVILEGES;
Step 5: Stop MySQL safe daemon
Step 6: Start MySQL daemon
There is a section in the MySQL manual on how to reset the root password which will solve your problem.
I got the same problem when accessing mysql with root. The problem I found is that some database files does not have permission by the mysql user, which is the user that started the mysql server daemon.
We can check this with ls -l /var/lib/mysql command, if the mysql user does not have permission of reading or writing on some files or directories, that might cause problem. We can change the owner or mode of those files or directories with chown/chmod commands.
After these changes, restart the mysqld daemon and login with root with command:
mysql -u root
Then change passwords or create other users for logging into mysql.
HTH
This code is solution for me
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'pWKQiSdwMBZDJfWZEnxgEHCPl4GgkJ';