I locked my root user out from our database. I need to get all privileges back to the root user. I have my password and I can log in to MySQL. But the root user has no all privileges.
I had the same problem as the title of this question, so incase anyone else googles upon this question and wants to start MySql in 'skip-grant-tables' mode on Windows, here is what I did.
Stop the MySQL service through Administrator tools, Services.
Modify the my.ini configuration file (assuming default paths)
C:\Program Files\MySQL\MySQL Server 5.5\my.ini
or for MySQL version >= 5.6
C:\ProgramData\MySQL\MySQL Server 5.6\my.ini
In the SERVER SECTION, under [mysqld], add the following line:
skip-grant-tables
so that you have
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
[mysqld]
skip-grant-tables
Start the service again and you should be able to log into your database without a password.
How to re-take control of the root user in MySQL.
DANGER: RISKY OPERATTION
Start session ssh (using root if possible).
Edit my.cnf file using.
sudo vi /etc/my.cnf
Add line to mysqld block.*
skip-grant-tables
Save and exit.
Restart MySQL service.
service mysql restart
Check service status.
service mysql status
Connect to mysql.
mysql
Using main database.
use mysql;
Redefine user root password.
UPDATE user SET `authentication_string` = PASSWORD('myNuevoPassword') WHERE `User` = 'root';
Edit file my.cnf.
sudo vi /etc/my.cnf
Erase line.
skip-grant-tables
Save and exit.
Restart MySQL service.
service mysqld restart
Check service status.
service mysql status
Connect to database.
mysql -u root -p
Type new password when prompted.
This action is very dangerous, it allows anyone to connect to all databases with no restriction without a user and password. It must be used carefully and must be reverted quickly to avoid risks.
After trying lots of things, this is what worked for me:
sudo mysql -u root
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
What that does is first we use sudo to log in mysql as root without needing a password. Then we just update root's password.
After that, I restarted mysqld:
sudo service mysql restart
And the newpassword logged root in!
On the Linux system you can do following (Should be similar for other OS)
Check if mysql process is running:
sudo service mysql status
If runnning then stop the process:
(Make sure you close all mysql tool)
sudo service mysql stop
If you have issue stopping then do following
Search for process: ps aux | grep mysqld
Kill the process: kill -9 process_id
Now start mysql in safe mode with skip grant
sudo mysqld_safe --skip-grant-tables &
I'm in windows 10, using WAMP64 server. Searched for my.cnf and my.ini. Found my.ini in C:\wamp64\bin\mariadb\mariadb10.2.14.
Following the instructions from the colleagues:
Opened the quick start menu from Wampserver, selected 'Stop All Services'
Opened my.ini in a text editor, searched for [mysqld]
Added 'skip-grant-tables' at the end of the [mysqld] section (but within it)
Save the file, leave the editor open
In the Wampserver menu, select "Restart Services'. There will be a warning about the skip-grant-tables option
In the Wampserver menu select MySQL to open the prompt
It asked for a password, just press enter
Paste the command ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'newpassword';
It must report that the operation was successful (no tables affected)
In the my.ini file, erase the 'skip-grant-tables' line, save the file
In the WampServer menu, select once more Restart Service
Now you can enter with the new password. Thanks to all answers here.
If you use mysql 5.6 server and have problems with C:\Program Files\MySQL\MySQL Server 5.6\my.ini:
You should go to C:\ProgramData\MySQL\MySQL Server 5.6\my.ini.
You should add skip-grant-tables and then you do not need a password.
# SERVER SECTION
# ----------------------------------------------------------------------
#
# The following options will be read by the MySQL Server. Make sure that
# you have installed the server correctly (see above) so it reads this
# file.
#
# server_type=3
[mysqld]
skip-grant-tables
Note: after you are done with your work on skip-grant-tables, you should restore your file of C:\ProgramData\MySQL\MySQL Server 5.6\my.ini.
Use the following command (notice the "d"): mysqld --skip-grant-tables
if you are running on Apple MacBook OSX then:
Stop your MySQL server (if it is already running).
Find your MySQL configuration file, my.cnf. (For me it was placed #
/Applications/XAMPP/xamppfiles/etc. You can just search if you
can't find it).
Open my.cnf file in any text editor.
Add "skip-grant-tables" (without quotes) at the end of [mysqld] section and save the file.
Now start your MySQL server. It'll start with skip-grant-tables option.
Do what you want now!!
PS: Please remove skip-grant-tables from my.cnf file once you are done with whatsoever you want to do ELSE MySQL server will always run without access grants.
Please run this below command from the console to skip the user table verification while launching mysql database from command prompt
mysqld -skip-grant-tables
Edit my.ini file and add skip-grant-tables and restart your mysql server :
[mysqld]
port= 3306
socket = "C:/xampp/mysql/mysql.sock"
basedir = "C:/xampp/mysql"
tmpdir = "C:/xampp/tmp"
datadir = "C:/xampp/mysql/data"
pid_file = "mysql.pid"
# enable-named-pipe
key_buffer = 16M
max_allowed_packet = 1M
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
log_error = "mysql_error.log"
skip-grant-tables
# Change here for bind listening
# bind-address="127.0.0.1"
# bind-address = ::1
if this is a windows box, the simplest thing to do is to stop the servers, add skip-grant-tables to the mysql configuration file, and restart the server.
once you've fixed your permission problems, repeat the above but remove the skip-grant-tables option.
if you don't know where your configuration file is, then log in to mysql send SHOW VARIABLES LIKE '%config%' and one of the rows returned will tell you where your configuration file is.
This is how to do it on Ubuntu 20.4. This worked for me.
Go to /etc/mysql/mysql.conf.d/
You can write into terminal
cd /etc/mysql/mysql.conf.d/,
then you need to edit the file which is named mysqld.cnf.
On my PC, that file was a read-only file, so I needed to first change the permissions.
I wrote
sudo chmod +rw mysqld.cnf in the terminal.
After that, I edited the file by typing sudo gedit mysqld.cnf
in the terminal.
In the file, you will see [mysqld] somewhere, below [mysqld] add
skip-grant-tables in a new line, so that it looks like this
[mysqld]
skip-grant-tables
Restart the mysql service by writting sudo service mysql restart in terminal.
If your server wasn't running then write sudo service mysql start in terminal.
Another thing worth mentioning here is that
I also had another problem which I fixed in almost the exact same manner.
My server wasn't listening at the port 3306,
so I also had to add port = 3306 in that mysqld.cnf file.
Now I have
[mysqld]
skip-grant-tables
port = 3306
in the mysqld.cnf file.
I see that the question is old, but maybe my configuration will help someone. I use this configuration in scripts:
sed -i 's/^#skip-grant-tables.*/skip-grant-tables/g' /etc/my.cnf
service mysql restart
mysql -e "UPDATE mysql.user SET authentication_string='' WHERE user='root';"
sed -i 's/^skip-grant-tables.*/#skip-grant-tables/g' /etc/my.cnf
service mysql restart
Related
I am attempting to move a mysql db onto an upgraded server with a newer version of ubuntu running (15.04), and after installing LAMP, I went to edit the bind address in the my.cnf file. The below is all I see in /etc/mysql/my.cnf. I added [mysqld] and the bind address = new address but nothing seems to allow my remote client to connect to this db. It is online and the ip address should be correct. What am i missing? Why is this my.cnf so bare?
#
# The MySQL database server configuration file.
#
# You can copy this to one of:
# - "/etc/mysql/my.cnf" to set global options,
# - "~/.my.cnf" to set user-specific options.
#
# One can use all long options that the program supports.
# Run program with --help to get a list of available options and with
# --print-defaults to see which it would actually understand and use.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html
#
# * IMPORTANT: Additional settings that can override those from this file!
# The files must end with '.cnf', otherwise they'll be ignored.
#
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
stupid me, apparently in this version it is the mysqld.cnf under /etc/mysql/mysql.conf.d/. Oh well. Posting this in case anyone else has problems.....
In ubuntu 15.04 the MySQL Server configure file is in:
/etc/mysql/mysql.conf.d/mysqld.cnf
You can find bind-address here.Comment it, and restart your MySQL Server use :
service mysql restart
Then you can access your MysqlServer from other computer. If you don't, perhaps you need grant some right to the user you use.
You can see MySQL Server bind-address use:
netstat -tap | grep mysql
So you know whether the configure take effect.
Depending on your linux distro your my.cnf (as well as the rest of your data directory) may be in /var/lib/mysql folder.
You could also run the find command to look for the file: find -name 'my.cnf'.
I suggest running this command as root, in your root directory.
Once you have located the correct my.cnf file there will be a parameter under [mysqld] section that looks like this:
bind-address = 127.0.0.1
Comment this parameter out with a # and save the edited my.cnf file. Restart the Mysql server and you should be able to connect remotely from any IP as long as the user you are trying to connect with as the correct permissions and host(%).
You can check this by running:
select user, host from mysql,user;
and show grants for 'youruser'#'yourhost';
grants and privileges for Mysql.
I have mysq 5.6.14 and centos 6.4. In /etc/my.cnf I have:
[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
bind-address=127.0.0.1
character-set-server=utf8
init-connect="SET NAMES utf8"
skip-character-set-client-handshake
general_log=1
general_log_file=/var/log/mysql
log-output=file
When I make a mistake in my.cnf I cant restart mysql (service mysql restart). It means that mysql reads this file. However I don't have /var/log/mysql file. What is my mistake?
I found the answer. This may seem like a clugy band-aid, but try the following for now...
service mysql stop
touch /var/log/mysql.log
chown mysql:mysql /var/log/mysql.log
service mysql start
This should work.
Please look back at the database home folder (run SHOW VARIABLES LIKE 'datadir';)
Go to the folder and see if a default general log appeared.
You could also try changing it to
general_log = 1
general_log_file = mysql.log
and see if the mysql.log appears in your datadir folder after restarting mysql
I want to change max_allowed_packet on server using WHM vps.
but I am not getting at where it located, so please help me
I have tried
SET GLOBAL max_allowed_packet =1073741824;
but its not working its required super admin.
how to edit mysql.ini in WHM vps
same with httpd.conf, how to edit setting of apache in WHM ?
Ahoy,
You can not edit the servers my.cnf file from inside WHM, you will need to edit this file using she ssh command line. To learn how to connect to your server using ssh please see:
http://docs.cpanel.net/twiki/bin/view/AllDocumentation/CpanelDocs/ShellAccess
Once you are connected to your server with the root login using ssh, you will want to issue the following command to edit my.cnf:
# nano -w /etc/my.cnf
In this file you will want to add a line under the [mysqld] section with the following contents:
max_allowed_packet=500M
You will now want to press Ctrl + O to save, and then Ctrl + X to exit. You will now want to restart the MySQL server through WHM or on the command line with:
# /etc/init.d/mysql restart
This will update the max_allowed_packet for cPanel/WHM's mysql.
Change in the my.ini/my.cnf file. Include the single line under [mysqld] in your file
max_allowed_packet=500M
now restart the MySQL service once you are done. You can see it's current value in mysql like this:
SHOW VARIABLES LIKE 'max_allowed_packet'
You can read about it here http://dev.mysql.com/doc/refman/5.1/en/packet-too-large.html
I am trying to enable logging on MySQL on my Mac, OS X version 10.8.3.
Everywhere I searched, I get the same answer, i.e. to add the following to my.cnf:
[mysqld]
general_log=1
log=/var/log/mysql-query.log
and then restart mysql.
Permissions on the log file are correct and owner is _mysql like all other MySQL files.
However, doesn't matter how much I try, once my.cnf has been modified, MySQL won't restart. It would only shutdown and that's it. Via command line or via Preferences Pane, it won't start again.
I tried enabling log vie Workbench too, but as soon as log=... entry goes into my.cnf, MySQL refuses to start. I have to manually delete this entry to start MySQL.
Can anybody please guiding me on how to enable logging for MySQL on OS X 10.8.3?
Ok, finally wasting more than a day, what worked for me is this:
general-log
general_log_file = /var/log/mysql-query.log
For almost 10 years, in all the Linux systems I have ever used and installed, which must be at least 100 if not more, it has always been a simple entry like log=< path to log file > under [mysqld] section. Apparently it was the same on Macs too what I read from various blogs etc, however on this particular setup which I am doing, this is the first time that I am setting it up like above.
So my current working /etc/my.cnf file is as follows:
[client]
socket=/var/mysql/mysql.sock
[mysqld]
socket=/var/mysql/mysql.sock
general-log
general_log_file = /var/log/mysqld.log
I had to do:
touch /etc/my.cnf
chown _mysql /etc/my.cnf
to create one.
Also I had to do:
touch /var/log.mysqld.log
chown _mysql /var/log/mysqld.log
followed by restarting mysql via Workbench. Also tried restart via command line as follows:
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown
sudo /usr/local/mysql/bin/mysqld_safe &
Main thing is, finally it is working and I can move ahead with my day.
I'm running MySQL Workbench 5.2.47 on Mac Mountain Lion. The above steps (create a /etc/my.cnf, create a dummy log file for mysql to populate, etc) could all be accomplished in MySQL Workbench.
1) Go to Server Admin -> Options File(Configuration).
2) Set your options, including location of 'general-log' under the 'Logging' tab. Click 'Apply'.
3) Startup / Shutdown -> Stop Server
4) Startup / Shutdown -> Start Server
Logging should now be turned on for all your statements.
#zeeshan's answer was pointed me to the write direction the most important thing is to make sure the permissions are right just as zeehan has mentioned.
/etc/my.cnf
[mysqld]
general_log = 1
general_log_file = /var/log/mysql-query.log
Then just chown the log and my.cnf files to be owned by "_mysql".
sudo chown _mysql /var/log/mysql-query.log
sudo chown _mysql /etc/my.cnf
Should be able to restart the mysqld using system_preferences.
I've read that Mysql server creates a log file where it keeps a record of all activities - like when and what queries execute.
Can anybody tell me where it exists in my system? How can I read it?
Basically, I need to back up the database with different input [backup between two dates] so I think I need to use log file here, that's why I want to do it...
I think this log must be secured somehow because sensitive information such as usernames and password may be logged [if any query require this]; so may it be secured, not easily able to be seen?
I have root access to the system, how can I see the log?
When I try to open /var/log/mysql.log it is empty.
This is my config file:
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
log = /var/log/mysql/mysql.log
binlog-do-db=zero
user = mysql
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
skip-external-locking
bind-address = 127.0.0.1
#
# * Fine Tuning
#
key_buffer = 16M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
general_log_file = /var/log/mysql/mysql.log
general_log = 1
Here is a simple way to enable them. In mysql we need to see often 3 logs which are mostly needed during any project development.
The Error Log. It contains information about errors that occur while
the server is running (also server start and stop)
The General Query Log. This is a general record of what mysqld is
doing (connect, disconnect, queries)
The Slow Query Log. Ιt consists of "slow" SQL statements (as
indicated by its name).
By default no log files are enabled in MYSQL. All errors will be shown in the syslog (/var/log/syslog).
To Enable them just follow below steps:
step1: Go to this file (/etc/mysql/conf.d/mysqld_safe_syslog.cnf) and remove or comment those line.
step2: Go to mysql conf file (/etc/mysql/my.cnf) and add following lines
To enable error log add following
[mysqld_safe]
log_error=/var/log/mysql/mysql_error.log
[mysqld]
log_error=/var/log/mysql/mysql_error.log
To enable general query log add following
general_log_file = /var/log/mysql/mysql.log
general_log = 1
To enable Slow Query Log add following
log_slow_queries = /var/log/mysql/mysql-slow.log
long_query_time = 2
log-queries-not-using-indexes
step3: save the file and restart mysql using following commands
service mysql restart
To enable logs at runtime, login to mysql client (mysql -u root -p) and give:
SET GLOBAL general_log = 'ON';
SET GLOBAL slow_query_log = 'ON';
Finally one thing I would like to mention here is I read this from a blog. Thanks. It works for me.
Click here to visit the blog
The MySQL logs are determined by the global variables such as:
log_error for the error message log;
general_log_file for the general query log file (if enabled by general_log);
slow_query_log_file for the slow query log file (if enabled by slow_query_log);
To see the settings and their location, run this shell command:
mysql -se "SHOW VARIABLES" | grep -e log_error -e general_log -e slow_query_log
To print the value of error log, run this command in the terminal:
mysql -e "SELECT ##GLOBAL.log_error"
To read content of the error log file in real time, run:
sudo tail -f $(mysql -Nse "SELECT ##GLOBAL.log_error")
Note: Hit Control-C when finish
When general log is enabled, try:
sudo tail -f $(mysql -Nse "SELECT CONCAT(##datadir, ##general_log_file)")
To use mysql with the password access, add -p or -pMYPASS parameter. To to keep it remembered, you can configure it in your ~/.my.cnf, e.g.
[client]
user=root
password=root
So it'll be remembered for the next time.
You have to activate the query logging in mysql.
edit /etc/my.cnf
[mysqld]
log=/tmp/mysql.log
restart the computer or the mysqld service
service mysqld restart
open phpmyadmin/any application that uses mysql/mysql console and run a query
cat /tmp/mysql.log ( you should see the query )
From the MySQL reference manual:
By default, all log files are created in the data directory.
Check /var/lib/mysql folder.
In my (I have LAMP installed) /etc/mysql/my.cnf file I found following, commented lines in [mysqld] section:
general_log_file = /var/log/mysql/mysql.log
general_log = 1
I had to open this file as superuser, with terminal:
sudo geany /etc/mysql/my.cnf
(I prefer to use Geany instead of gedit or VI, it doesn't matter)
I just uncommented them & save the file then restart MySQL with
sudo service MySQL restart
Run several queries, open the above file (/var/log/mysql/mysql.log) and the log was there :)
Enter MySQL/MariaDB server command-line tool as root
Set file path (you can replace general.log with the file name of your choice).
SET GLOBAL general_log_file='/var/log/mysql/general.log';
Set log file format
SET GLOBAL log_output = 'FILE';
Enable the server general log
SET GLOBAL general_log = 'ON';
Check your configurations in global configuration variables.
SHOW VARIABLES LIKE "general_log%";
Enter exit to leave MySQL command-line and Tail your queries by
tail -f /var/log/mysql/general.log
or
less /var/log/mysql/general.log
To disable the general server log
SET GLOBAL general_log = 'OFF';
To complement loyola's answer it is worth mentioning that as of MySQL 5.1 log_slow_queries is deprecated and is replaced with slow-query-log
Using log_slow_queries will cause your service mysql restart or service mysql start to fail
In addition to the answers above you can pass in command line parameters to the mysqld process for logging options instead of manually editing your conf file. For example, to enable general logging and specifiy a file:
mysqld --general-log --general-log-file=/var/log/mysql.general.log
Confirming other answers above, mysqld --help --verbose gives you the values from the conf file (so running with command line options general-log is FALSE); whereas mysql -se "SHOW VARIABLES" | grep -e log_error -e general_log gives:
general_log ON
general_log_file /var/log/mysql.general.log
Use slightly more compact syntax for the error log:
mysqld --general-log --general-log-file=/var/log/mysql.general.log --log-error=/var/log/mysql.error.log
shell> mysqladmin flush-logs
shell> mv host_name.err-old backup-directory