Use sqldf in R - cannot specify correct username - mysql

I just downloaded R package called sqldf just for fun, but have not been able to run it correctly so far. When I try to do some query using iris datasets:
sqldf("select * from iris limit 5")
the error occurred saying Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Access denied for user 'myUserName'#'localhost' (using password: NO)
)
Error in !dbPreExists : invalid argument type
So I opened its help documentation and then run the following query:
sqldf("select * from iris limit 5", user="myUser")
the error message is the same as the above, which would mean that I failed to specify my user argument correctly, given that the error message doesn't change to Access denied for user 'myUser'#'localhost').
So how can I fix it and run it correctly?
For your information when I use RMySQL, I use the following arguments in order to make connection.
con <- dbConnect(dbDriver("MySQL"),username="myUser",password="myPass",host="myHost",unix.sock="/tmp/mysql.sock",dbname="myDB")
I'm on OS X 10.9.1 and use MySQL 5.6 installed via homebrew, and R version 3.0.2 and sqldf version 0.4-6.
Thanks.

I would start by making sure that sqldf works with SQLLite.
head(sqldf("select * from iris",drv='SQLite'))
Next, I would highly recommend that you always use the drv= param or explicitly set the sqldf.driver variable. Reling on the order of library(..) calls can cause bugs later.
If you are doing something simple, you can use SQLLite because it is fast and has few dependencies (so, for example, if you move your code, you dont have to install MySQL). If you are using Dates, SQLLite is not great so you might want to use MySQL.
As mentioned in the comments and in the notes at the bottom of ?sqldf, you need to setup a my.cnf for mysql with a [Client] section so that all clients including sqldf use a specific login. Also make sure the dbname is set like so:
options(sqldf.driver = "RMySQL")
options(RMySQL.dbname = "rtest")
head(sqldf("select * from iris"))
I havent found a reliable way to use MySQL without a my.cnf. Using an explicit connection for sqldf also doesnt work for me. I think PostreSQL is better because you can set the username,etc from inside R per the documentation.

I faced the first error because sqldf was using mysql by default in my case. Than, I switched it to SQLite and it worked using the following command.
options(sqldf.driver = "SQLite")

if you have loaded RMySQL Package try to detach both RMySQL and sqldf packages and load sqldf package.its works for me. please see my below code
detach(package:RMySQL)
detach(package:sqldf)
library(sqldf)
sqldf("select * from iris limit 5",user="sa", password = "root", host = "192.168.200.182", port=3377)

Related

STRATO & Python connection to mySQL DB is not possbile

my Domain/Webspace etc is hosted by Strato (https://www.strato.de) and I am able to execute python scripts.
The script is working perfectly fine in the development env (Windows 10) - I am able to connect to the DB without any issues.
But within the strato env I receive the following error:
2003: Can't connect to MySQL server on 'rdbms.strato.de:3306' (113 No route to host)
Relevant Code:
....
import mysql.connector
....
try:
db_link = mysql.connector.connect(
host = "rdbms.strato.de",
user = "xxxx",
passwd = "yyyy"
)
except Exception as e:
print("The error raised is: ", e)
return
....
But I am unable to establish the connection to the database (error see above)....
Python version used:
sys.version_info(major=3, minor=8, micro=8, releaselevel='final', serial=0)
Any thoughts?
Many thanks in advance...
after trying a lot of different approaches I found a solution (working for Strato).
Mistake was.
....
import mysql.connector
....
Working solution
....
import MySQLdb
....
try:
db_link = MySQLdb.connect(
<host>,
<user>,
<passwd>,
<database>
)
except Exception as e:
print("The error raised is: ", e)
return
....
Maybe this is helpful for someone...
Best regards....
I am stuck at the very same point but cannot use your solution:
Which package did you install for import MySQLdb to work?
"mysql-connector-python (8.0.30)" does not offer this and I always come back to "MySQL-python (1.2.5)" browsing several sites - which does not help as it supports only Python2. I'm trying several different MySQL packages, none worked so far...
Or did you have to make any special settings at Strato?
Thanks a lot, Tobi
Edit: From my point of view this simply does not work as long as you do not order an explicit own server... see: https://www.strato.de/faq/hosting/gibt-es-bei-strato-einschraenkungen-bei-den-mysql-funktionen/

Value for 'configPath' when running checkForServerUpgrade on AWS RDS

To prepare my upgrade from mysql 5.7 to mysql 8, I want to run the upgrade utility checker. Here's what I did so far:
installed mysqlsh on my machine
started mysqlsh
executed util.checkForServerUpgrade targeting the server that I want to upgrade
Here's the exact command that I used in step 3:
util.checkForServerUpgrade('root#my-remote-host:3306', { "password":"my-password" })
This runs fine but some checks are not executed because I don't provide the configPath parameter. For example, here's a warning that I get:
14) Removed system variables for error logging to the system log configuration
To run this check requires full path to MySQL server configuration file to be specified at 'configPath' key of options dictionary
More information:
https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-13.html#mysqld-8-0-13-logging
Anybody knows the value that I should provide for the configPath parameter?
I've tried to do the same using the command util.checkForServerUpgrade defining the configPath, without success. I then tried to run the same command directly from outside the mysqlsh shell, with success:
mysqlsh -- util check-for-server-upgrade root#localhost --target-version=8.0.13 --output-format=JSON --config-path=/etc/mysql/my.cnf
and it worked. To be noted that when I've tried to run from mysqlsh in the session root#localhost the command:
util.checkForServerUpgrade({"configPath":"/etc/mysql/my.cnf"})
mysqlsh replied with:
"Util.checkForServerUpgrade: Argument #1: Invalid values in connection options: configPath (ArgumentError)"
Try putting in the connection string, for example,
util.checkForServerUpgrade('root#localhost',{'configPath': '/etc/my.cnf'})
This worked for me, but without the connection string it doesn't.

How to connect to local MySQL Server 8.0 with DBIish in Perl6

I'm working on a Perl6 project, but having difficulty connecting to MySQL. Even when using the DBIish (or perl6.org tutorial) example code, the connection fails. Any suggestions or advice is appreciated! User credentials have been confirmed accurate too.
I'm running this on Windows 10 with MySQL Server 8.0 and standard Perl6 with Rakudo Star. I have tried modifying the connection string in numerous ways like :$password :password<> :password() etc. but can't get a connection established. Also should note that I have the ODBC, C, C++, and.Net connectors installed.
#!/usr/bin/perl6
use v6.c;
use lib 'lib';
use DBIish;
use Register::User;
# Windows support
%*ENV<DBIISH_MYSQL_LIB> = "C:/Program Files/MySQL/MySQL Server 8.0/liblibmysql.dll"
if $*DISTRO.is-win;
my $dbh = DBIish.connect('mysql', :host<localhost>, :port(3306), :database<dbNameHere>, :user<usernameHere>, :password<pwdIsHere>) or die "couldn't connect to database";
my $sth = $dbh.prepare(q:to/STATEMENT/);
SELECT *
FROM users
STATEMENT
$sth.execute();
my #rows = $sth.allrows();
for #rows { .print }
say #rows.elems;
$sth.finish;
$dbh.dispose;
This should be connecting to the DB. Then the app runs a query, followed by printing out each resulting row. What actually happens is the application hits the 'die' message every time.
This is more of a work around, but being unable to use use a DB is crippling. So even when trying to use the NativeLibs I couldn't get a connection via DBIish. Instead I have opted to using DB::MySQL which is proving to be quite helpful. With a few lines of code this module has your DB needs covered:
use DB::MySQL;
my $mysql = DB::MySQL.new(:database<databaseName>, :user<userName>, :password<passwordHere>);
my #users = $mysql.query('select * from users').arrays;
for #users { say "user #$_[0]: $_[1] $_[2]"; }
#Results would be:
#user #1: FirstName LastName
#user #2: FirstName LastName
#etc...
This will print out a line for each user formatted as shown above. It's not as familiar as DBIish, but this module gets the job done as needed. There's plenty more you can do with it to, so I highly recommend reading the docs.
According to this github DBIish issue 127
The environmental variable DBIISH_MYSQL_LIB was removed. I don't know if anyone brought it back.
However if you add the library's path, and the file is named mysql.dll, it will work. Not a good result for the scientific method.
So more testing is needed - and perhaps
C:\Program Files\MySQL\MySQL Server 8.0\lib>mklink mysql.dll .\libmysql.dll
Oviously you can create your own lib directory and add that to your path and then add this symlink to that directory.
Hope this helps. I've spent hours..
EDIT: Still spending time - accounting later.
Something very transitory is going on. I reset the machine (perhaps always do this from now on), and still got the missing mysql.dll errors. Tried going into the MySQL lib directory to execute raku from there.. worked. changed directories.. didn't work.
Launched administrator cmd - from home directory, tried the raku command. Worked. Ok - not good, but perhaps consistent. Launched non admin cmd, tried it from the MySQL lib directory, worked. And just for giggles, tried it outside of that directory.. worked.
Now I can't get it not to work. Will explore NativeLibs::Searcher as Valle Lukas suggested!
Maybe the example in the dbiish repository is not valid anymore.
The DBIISH_MYSQL_LIB Env seems to be replaced by NativeLibs::Searcher with commit 9bc4191
Looking at NativeLibs::Searcher may help to find the root cause of the problem.

How to pass secure_auth to MySQL login via SQLalchemy

I'm working on the front end of a webapp, and my co-developer is using Pyramid and SQAlchemy. We've just moved from SQLite to MySQL. I installed MySQL 5.6.15 (via Homebrew) on my OS X machine to get the Python MySQLdb install to work (via pip in a virtualenv).
Because in MySQL >= 5.6.5 secure_auth is now ON by default I can only connect to the remote database (pre 5.6.5) with the --skip-secure-auth flag, which works fine in a terminal.
However, in the Python Pyramid code, it only seems possible to add this flag as an argument to create_engine(), but I can't find create_engine() in my co-dev's code, only the connection string below in an initialisation config file. He's not available, this isn't my area of expertise, and we launch next week :(
sqlalchemy.url = mysql+mysqldb://gooddeeds:deeds808letme1now#146.227.24.38/gooddeeds_development?charset=utf8
I've tried appending various "secure auth" strings to the above with no success. Am I looking in the wrong place? Has MySQLdb set secure_auth to ON because I'm running MySQL 5.6.15? If so, how can I change that?
If you are forced to use the old passwords (bah!) when using MySQL 5.6, and using MySQLdb with SQLAlchemy, you'll have to add the --skip-secure-auth to an option file and use URL:
from sqlalchemy.engine.url import URL
..
dialect_options = {
'read_default_file': '/path/to/your/mysql.cnf',
}
engine = create_engine(URL(
'mysql',
username='..', password='..',
host='..', database='..',
query=dialect_options
))
The mysql.cnf would contain:
[client]
skip-secure-auth
For Pyramid, you can do the following. Add a line in your configuration ini-file that holds the connection arguments:
sqlalchemy.url = mysql://scott:tiger#localhost/test
sqlalchemy.connect_args = { 'read_default_file': '/path/to/foo' }
Now you need to change a bit the way the settings are read and used. In the file that launches your Pyramic app, do the following:
def main(global_config, **settings):
try:
settings['sqlalchemy.connect_args'] = eval(settings['sqlalchemy.connect_args'])
except KeyError:
settings['sqlalchemy.connect_args'] = {}
engine = engine_from_config(settings, 'sqlalchemy.')
# rest of code..
The trick is to evaluate the string in the ini file which contains a dictionary with the extra options for the SQLAlchemy dialect.

emacs and mysql on windows 7

I have been using emacs for writing programs and recently I want to execute SQL queries through it , I run the following commands and get the following error:
M-x sql-Mysql
then I just press return and then
user:
password:
database:
server:
and I get the error
" unable to locate sql program mysql "
I have searched for two continuous days and got the suggestions to edit .emacs file but of no use. I am using windows 7.
My init file looks like this :
(setq sql-connection-alist
'((pool-a
(sql-product 'mysql)
(sql-server "1.2.3.4")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3306))
(pool-b
(sql-product 'mysql)
(sql-server "1.2.3.4")
(sql-user "me")
(sql-password "mypassword")
(sql-database "thedb")
(sql-port 3307))))
(defun sql-connect-preset (name)
"Connect to a predefined SQL connection listed in `sql-connection-alist'"
(eval `(let ,(cdr (assoc name sql-connection-alist))
(flet ((sql-get-login (&rest what)))
(sql-product-interactive sql-product)))))
(defun sql-pool-a ()
(interactive)
(sql-connect-preset 'pool-a))
well,i think,it does not need to put that code in my init file so i erased all that.in fact i need not put any thing in the init file for mysql to run.i just filled in the appropriate details in the following fields:
user:root
password:which i have created during mysql installation
database:i left this field blank
server:localhost
then no problem occurred and i could run the sql commands as usual as i would in a normal mysql command window.what i made out from it that server as 'localhost' and user as 'root' are the main parameters and should work in probably all the cases(if you are running mysql server on your system through emacs. password to be filled is the password entered during the time of installation.