Executing events in MySQL without SUPER privelege (?) - mysql

I have created events in my MySQL. They are working, but in order for them to execute, I need to
SET event_scheduler = ON;.
In order to do that, I need to get SUPER privileges. I get the following error:
Access denied; you need (at least one of) the SUPER privilege(s) for this operation
I try to grant super: GRANT SUPER ON *.* TO user#'localhost' IDENTIFIED BY 'password'; and get another error:
ERROR 1045 (28000): Access denied for user
I have contacted my host, and they said that since their host is shared, they were unable to add super privileges for my account.
Can anyone help me to find another way, so the events would begin to run? Account doesn't belong to me, so I can't switch hosts.

Cron jobs are likely the best on shared hosts. If you have access to cPanel, it will make your life a lot easier.
First things, first: Create your script. This can access the database, send emails, pretty much do anything that needs to be done. Please note that $_GET, $_POST, $_REQUEST, and $_COOKIE will be unavailable for the duration of the script, as this isn't a web request. You wont be able to set cookies, or view the information about the remote user (As there is none). You can however, pass variables through command line though, using $argv. See this post for more information.
This sample script will send out a newsletter to all users in the to_be_mailed table:
<?php
include('db.cfg.php');
include('newsletter.php');
$body = get_newsletter();
$result = $db->query("SELECT * FROM to_be_mailed");
while ( $row = $result->fetch_row() )
{
mail($row['email'], 'Nightly news update!', $body);
}
//Probably want to update the database again and set them all to sent.
echo 'Done sending emails!';
Debugging scripts can be difficult, as you usually don't have access to the output. Have a look at this answer and be sure to enable error_logs so that you can make sure your script executed sucessfully.
When using cPanel's Cron Jobs it makes your life easier by emailing the output to you at the end of execution. This makes it easier for you to track down bugs, and ensure your cron job executes successfully.
Next adding your cron job:
See cPanel's Cron Jobs for info on how to add a cron job for cPanel enabled hosts.
Non cpanel hosts: See this post. The gist is, you use crontab -e to add the task (This will open up an editor, and you would add in the following line):
# crontab -e
00 * * * * /usr/local/bin/php /home/USERNAME/myscript.php
If you're just getting started with cron syntax, this site is absolutely wonderful. It will give you examples, help with syntax, and help you figure out the schedule you want to run.

Related

The user specified as a definer does not exist - GRANT doesn't fix

After years of no problems with my PHP webapp running MySQL 5.5 on the backend, suddenly today I'm having permissions problems.
Whenever I try to run an INSERT statement (either from PHP or from Workbench or Heidi), I get this error message: The user specified as a definer ('my_user'#'1.2.3.%') does not exist
One symptom is that INSERT statements cannot be executed by my PHP nor by remote clients such as Workbench or Heidi. I have tried various solutions suggested by:
MySQL error 1449: The user specified as a definer does not exist
Error: 1449, "The user specified as a definer ('root'#'localhost') does not exist"
Everything points to running a GRANT statement. I have tried various GRANTs as suggested, but I always get this error:
Access denied for user
It seems like my permissions got corrupted or something. So I created a brand new DB user inside BlueHost control panel on my VPS and gave full privileges. I get the same exact errors on this brand new user.
BlueHost support has no idea what to do.
Some Stack articles say to go mess around with TRIGGERS or STORED PROCS permissions - but I don't have any of these types of objects. Simple INSERT statements cause this error, and perhaps UPDATEs as well, but I'm not sure of that at this moment.
The user has FULL PRIVILEGES, as always.
What else can I try? Is there some way to fix corrupted permissions? I can run SELECT statements with no problems.
I tried connecting to the DB with root (using the same pwd as I use when connecting to WHM), but it didn't like my password or maybe the user in general. I never set up root as a specific user against this DB and I'm not sure that's a good idea. I have always used a specific user created just for this DB. Again, past 5 years no problems at all. The DB has not been upgraded, no DB changes, no user changes, nothing.
I sort of solved it. I ended up creating a new database with a different name, and created a new admin user to go with it. Then I ran my db backup/dump script against it. Everything works perfectly again, with this new DB. The old DB is still jacked.
I'm thinking that permissions got corrupted in the old database.

Why does SQL foribben to execute SELECT query?

I try to execute query in phpmyadmin and get error:
#1142 - SELECT command denied to user 'cpses_tkdpmnyjWW'#'localhost' for table 'user'
So, user cpses_tkdpmnyjWW'#'localhost is created dynamically and I can not set privileges for this user.
How to fix this?
Use SHOW GRANTS to show your current user privileges. It sounds as though the output may be similar to:
GRANT USAGE ON *.* TO 'Unnamed'#'localhost'
This would mean the account could sign into the server but do little else. This page gives a more detailed breakdown, as you'll see there are quite a few permutations.
The solution is you need to either find an account with more privileges or create/update one.
If the above is not an option, one quick trick I may try is connecting to '127.0.0.1' instead of 'localhost'. In MySQL the source of the connection can form part of the username so it's plausible that connecting on an IP instead of socket if you are on Unix flavoured OS.
Additionally, if you have admin/root access to the server, it is possible to create users when MySQL starts which is very useful in some scenarios.

MySQL User with select privileges can still update

I have an OpenShift app with a MySQL database that I configured an ODBC connection for, such that users can use Access as a read only front end to the tables. I created such a user, but they are able to update data from the Access front end and it is reflected in the database. Here's what I did:
I created a user named 'reports', to be given read only access to only one schema, 'reviews'.
GRANT SELECT ON reviews.* TO reports#'%' IDENTIFIED BY `password`;
When I run
SELECT * FROM mysql.user WHERE user = 'reports';
I get all N's and 0's. I understand there's no 'Y' for "Select_priv" because it's not a global select priv, only on one schema, so I figured it was okay.
When I run
SHOW GRANTS FOR 'reports'#'%';
I get
GRANT USAGE ON . TO 'reports'#'%' IDENTIFIED BY PASSWORD 'stringstringstring'
GRANT SELECT ON 'reviews'.* TO 'reports'#'%';
Which I expected.
SELECT * FROM db where User = 'reports';
|Host|Db |User |Select_priv|Insert_priv|Update_priv|...
|% |reviews|reports|Y |N |N |...
But when I go into Access, edit some data, then ssh into the database and view the database, the UPDATES are working.
I am using rhc port-forward -a applicationname before I connect in Access, and I use rhc ssh applicationname to view the MySQL from the server.
There is a bug in Access where if you feed it a Data Source, then try to change it, it won't actually let go of your initial Data Source even though it has appeared to. I had initially set my Access DB up with an All Privileges account, then changed the Data Source to a read only account, but it continued to use my All Access account behind the scenes. Deleting the Data Source using odbcad32.exe proved this, as I just got connection errors, even though it said I was connected via my valid Read Only account. I created a new Access DB using the Read Only data source from the beginning and it worked as expected.

MYSQL Events not Running, Unable to turn on event_scheduler

My database (mysql) server is hosted on Mochahost.com . I have created an Event on my database, but its not running. I found that the event scheduler is turned off. I queried the following command
SET GLOBAL event_scheduler = ON
but it returned this error
"" #1227 - Access denied; you need (at least one of) the SUPER privilege(s) for this operation
I checked and found that I dont have the privilege option available to create a user with Super privileges
Please help me how to fix this, do I need to contact the host support for this ?
As André said, this is something your host needs to allow (and probably won't in a shared hosting situation). Switching to a dedicated host is probably the only way to get full access to the database.

MySQL 5.1 / phpMyAdmin - logging CREATE/ALTER statements

Is it possible to log CREATE / ALTER statements issued on a MySQL server through phpMyAdmin? I heard that it could be done with a trigger, but I can't seem to find suitable code anywhere. I would like to log these statements to a table, preferably with the timestamp of when they were issued. Can someone provide me with a sample trigger that would enable me to accomplish this?
I would like to log these statements so I can easily synchronize the changes with another MySQL server.
There is a patch for phpMyAdmin which provides configurable logging with only some simple code modifications.
We did this at my work and then i tweaked it further to log into folders by day, log IP addresses and a couple other things and it works great.
Thanks #Unreason for the link, i couldn't recall where i found it.
Here is a script that would do what you want for mysql-proxy (check the link on official docs how to install the proxy).
To actually log the queries you can use something as simple as
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
function read_query( packet )
if string.byte(packet) == proxy.COM_QUERY then
local query = string.lower(string.sub(packet, 2))
if string.starts(query, "alter") or string.starts(query, "create") then
-- give your logfile a name, absolute path worked for me
local log_file = '/var/log/mysql-proxy-ddl.log'
local fh = io.open(log_file, "a+")
fh:write( string.format("%s %6d -- %s \n",
os.date('%Y-%m-%d %H:%M:%S'),
proxy.connection.server["thread_id"],
query))
fh:flush()
end
end
end
The script was adopted from here, search for 'simple logging'.
This does not care about results - even if the query returned an error it would be logged (there is 'more customized logging' example, which is a better candidate for production logging).
Also, you might take another approach if it is applicable for you - define different users in your database and give DDL rights only to a certain user, then you could log everything for that user and you don't have to worry about details (for example - proxy recognizes the following server commands, out of which it inspects only Query)
Installing the proxy is straight forward, when you test it you can run it with
mysql-proxy --proxy-lua-script=/path/to/script.lua
It runs on port 4040 by default so test it with
mysql -u user -p -h 127.0.0.1 -P 4040
(make sure you don't bypass the proxy; for example on my distro mysql -u user -p -h localhost -P 4040 completely ignored the port and connected over socket, which left me puzzled for a few minutes)
The answer to your question will fall into one of the listed in MySQL Server logs
If you just want to get the CREATE/ALTER statements, I would go with the general query log. But you will have to parse the file manually. Be aware of the security issues this approach raises.
In your scenario, replication seems to be an overkill.
Triggers are not a valid option since they are only supported at SELECT, UPDATE and INSERT level and not ALTER/CREATE.
Edit 1:
The query log would be the best choice but as you mentioned on busy servers the logs would cause a considerable efficiency penalty. The only additional alternative I know of is MySQL Proxy.
I think that your best bet would be to look at the use of stored procedures and functions here to make changes to your DB. That way you could look at manually logging data.