I'm starting learning to use MySQL and, more specifically, I'm trying to learn how to connect and make it interact with Access and R.
I recently create an odbc connecting Access to R. I successfully used R libraries to retrieve it and use sql queries. So I moved to R + Mysql.
Here starts my problem!
After installing RODBC I installed RMySQL and tried to use "dbConnect". Here follows my code:
con <- dbConnect(dbDriver("MySQL"), user = "root", password = "mypwd", dbname = "mydbname")
where "mypwd" it's the password I inserted when created my MySQL ODBC Unicode Driver and "mydbname" it's the database name I connected. Just for sake of information, I already tested this odbc by transfering tables from Access and Mysql. So my doubts are related only to connection between Mysql and R.
So, let's see my error when running the code line above:
Error in .local(drv, ...) :
Failed to connect to database: Error: Plugin caching_sha2_password could not be loaded
I already read other posts about the need to change the Preferred Authentication Plugin.
I found this line code as solution:
ALTER USER user
IDENTIFIED WITH mysql_native_password
BY 'password';
So....please, tell me if I should change this code line into:
ALTER USER root
IDENTIFIED WITH mysql_native_password
BY 'mypwd';
Is it correct? Or I completely missed the point.
Sorry for my question but as I said, I'm a completely newbie. Thanks for your patience
Where is your host address ?
Try the code something like this
con <- dbConnect(dbDriver("MySQL"), host="192.100.XX.XX",db="df", user="root", password="mypwd")
Ok it seems I found out a workaround for my problem.
Instead of using "dbConnect" I used "odbcConnect". Here follows my working code:
channel <- odbcConnect("Myodbcname", uid="root")
df <- sqlQuery(channel, "SELECT * from mytable", as.is = TRUE)
"Myodbcname" is the name I saved for my DSN Name and DSN System.
So, now I can use my sql queries on my table and work on it.
At this point I need to understand the difference between the function "odbcConnect" and "dbConnect". Because if I don't need the latter I can ignore for the moment. Maybe I have to open a new thread with a link to this one. Not sure what Stackoverflow prefers me to do.
Related
I've got WampServer running a local instance of WordPress, and I have SSRS running in native mode.
I've written a report that queries the MySQL database, and it works and runs just fine within Visual Studio.
If I launch it though, it first had issues with my ODBC shared data source, which was as follows:
Name of shared data source: localhost.rds
ODBC Connection string: Dsn=localhostuserDSN
That DSN is a user DSN (as you might've guessed), and it uses the MySQL ODBC 5.3 Unicode driver. It's configured as follows:
TCP/IP server: localhost
Port: 3306
User: root
Password: <blank>
Database: wp
When tested, the connection succeeds, and the report works just fine.
When deployed though, I got the following error:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
After much research and hair-pulling, I finally found out that if I changed the data source connection string to...
Dsn=localhostUserDSN;Driver=MySQL ODBC 5.3 Unicode Driver
...that also works, and at least then, the connection also succeed when in the Data Sources configuration screen for the report after it's deployed. But then, just when I thought I was finally out of the woods, I run the report and get the following error message:
ERROR [3D000] [MySQL][ODBC 5.3(w) Driver][mysqld-5.7.9]No database selected
The thing is, I can't specify a database, or at least I don't know a way to.
If I try changing the connection string to...
Dsn=localhostUserDSN;Driver=MySQL ODBC 5.3 Unicode Driver;database=wp
...it says the (blank) password isn't valid, before I've even pressed the 'Test' button.
If I tack on 'USE [wp]' to the query in Visual Studio, it abruptly protests.
I thought maybe I'd get around that by putting it in a stored procedure, but since the data is being queried directly from the MySQL database, I can't do that either.
(Before attempting any of this, I already tried syncing/converting the MySQL db to the SQL db, but I kept running into issues with the data conversion.)
Thinking it might be a permission issue, I've also tried running...
GRANT ALL ON [wp] TO root#localhost;
...and...
GRANT ALL PRIVILEGES ON [wp] TO root#localhost;
in SSMS, but in both cases, it says:
Cannot find the object 'wp', because it does not exist or you do not have permission.
And I'm the only admin on a local machine. I installed SQL and it uses Windows Authentication, so I don't see how I wouldn't have permission, and I have to assume it's an issue with the syntax.
I saw elsewhere here that someone suggested '[wp].*', but it doesn't like that either.
I've been at this literally all day, and I'm at my whits end, so any constructive feedback would be greatly appreciated!
For anyone stumbling across this in the future, I think we resolved the original issue, although I've run into other issues which may be specific to me.
Three things, not sure if the first one is related:
1) In the query for the dataset of the report, I specified the table like so "SELECT wp_posts.ID FROM wp_posts" instead of just "SELECT ID FROM wp_posts".
2) I went back to adding on "database=wp" to the ODBC connection string.
3) Even though both work in Visual Studio, I switched to a System DSN instead of the User DSN.
I think that should get you back in action.
I'm trying to open a connection to a db via the DriverManager. When I use the getConnection(url, properties) function it works fine, however when I try to append the username and password (something like jdbc:mysql:address;user=user;password=password) to the url and call getConnection(url) I get the following error
Access denied for user ''#'localhost' to database 'db_aux_linux64_mysql;user=user;password=password'
So while it was able to find the db, it is trying to pass in an empty string as the user argument (and probably the password as well) for some reason. Any ideas on why this might be?
You're using the wrong url syntax, try this:
Connection conn = DriverManager.getConnection("jdbc:mysql://hostname/dbname?user=user&password=password");
Note: Don't forget to add your database name after your hostname -- the error message you are getting is indicating that the jdbc driver is looking for a database that is literally named "db_aux_linux64_mysql;user=user;password=password".
Alternatively, you can just use the DriverManager.getConnection(url, uname, pass) as suggested in the comments, then you don't need to remember the url syntax for your jdbc driver (and your code will be more portable since each jdbc driver is subject to having different url syntax).
Source: Mysql doc seciton 6.1
I just created my first SQL database using MAMP. (It is simple- just a list of pets.) I would like to load it into R. Here is what I wrote:
install.packages("dbConnect")
library(dbConnect)
mypets=dbConnect(MySQL(),user="root",
host="localhost1234/DatabaseGrace")
This error is returned:
Error in mysqlNewConnection(drv, ...) :
RS-DBI driver: (Failed to connect to database: Error: Unknown MySQL server host 'localhost1234/DatabaseGrace' (2))
Any idea what this means or how I can solve it?
Thank you #duffymo and #Lorenz.
Summary:
The database is stored on my computer, so host did need to be localhost as you suggested.
Here is what ended up working.
install.packages("RMySQL")
install.packages("dbConnect")
library(dbConnect)
dbGrace=dbConnect(MySQL(),user="root",
host="localhost",
dbname="DatabaseGrace",
password="root",
unix.sock="/Applications/MAMP/tmp/mysql/mysql.sock")
Thanks, all!
I think the host name should be "localhost", without the 1234.
If you intended that to mean the port, I'd check to make sure that MySQL is indeed listening on that port. The default value is 3306; the admin would have to make a conscious decision to change that.
The usual notation is "localhost:3306" in Java. If you must include the port number, try separating it from the host name with a colon.
Can you connect to MySQL using the admin shell?
Have you GRANTed permission to localhost to connect to database named DatabaseGrace? If not, MySQL won't allow you to connect.
http://dev.mysql.com/doc/refman/5.1/en/grant.html
localhost suggests to me that the database is running on your local machine. Is that true? If not, localhost isn't the right host name. It might also mean that you have a firewall between your machine and the database which prevents you from connecting.
I get a lot of mileage out of cutting & pasting any errors I get into Google to see if anyone else has ever experienced my problem. Here's the first hit.
The host is the computer name. So put simply localhost.
Select the correct database in a second step.
this works for me with XAMPP on MAC:
library(RMySQL)
con=dbConnect(MySQL(),
host='localhost',
unix.sock='/Applications/XAMPP/xamppfiles/var/mysql/mysql.sock',
user='root',
password='...'
)
df=dbGetQuery(con,'SHOW DATABASES')
print(df)
I'm trying to connect to a mySQL database at http://bluesql.net, but when I try to connect, it gives this error:
Connect Error (2000) mysqlnd cannot connect to MySQL 4.1+ using old authentication
I've looked into this, and it has to do with some old password scheme used before MySQL 4.1. Newer versions have the option to use old passwords, which I've read may cause this problem.
I'm running php 5.3, and connecting with mySQLi (new mysqli(...)). I'm hoping I can do something in the code to connect to the DB at bluesql.net - clearly I don't control how their database is set up. Downgrading php versions isn't an option.
Anyone have any ideas?
edit: This only applies if you are in control of the MySQL server... if you're not take a look at Mysql password hashing method old vs new
First check with the SQL query
SHOW VARIABLES LIKE 'old_passwords'
(in the MySQL command line client, HeidiSQL or whatever front end you like) whether the server is set to use the old password schema by default. If this returns old_passwords,Off you just happen to have old password entries in the user table. The MySQL server will use the old authentication routine for these accounts. You can simply set a new password for the account and the new routine will be used.
You can check which routine will be used by taking a look at the mysql.user table (with an account that has access to that table)
SELECT `User`, `Host`, Length(`Password`) FROM mysql.user
This will return 16 for accounts with old passwords and 41 for accounts with new passwords (and 0 for accounts with no password at all, you might want to take care of those as well).
Either use the user management tools of the MySQL front end (if there are any) or
SET PASSWORD FOR 'User'#'Host'=PASSWORD('yourpassword');
FLUSH Privileges;
(replace User and Host with the values you got from the previous query.) Then check the length of the password again. It should be 41 now and your client (e.g. mysqlnd) should be able to connect to the server.
see also the MySQL documentation:
* http://dev.mysql.com/doc/refman/5.0/en/old-client.html
* http://dev.mysql.com/doc/refman/5.0/en/password-hashing.html
* http://dev.mysql.com/doc/refman/5.0/en/set-password.html
If you do not have control of the server
I just had this issue, and was able to work around it.
First, connect to the MySQL database with an older client that doesn't mind old_passwords. Connect using the user that your script will be using.
Run these queries:
SET SESSION old_passwords=FALSE;
SET PASSWORD = PASSWORD('[your password]');
In your PHP script, change your mysql_connect function to include the client flag 1:
define('CLIENT_LONG_PASSWORD', 1);
mysql_connect('[your server]', '[your username]', '[your password]', false, CLIENT_LONG_PASSWORD);
This allowed me to connect successfully.
Edit: as per Garland Pope's comment, it may not be necessary to set CLIENT_LONG_PASSWORD manually any more in your PHP code as of PHP 5.4!
Edit: courtesy of Antonio Bonifati, a PHP script to run the queries for you:
<?php const DB = [ 'host' => '...', # localhost may not work on some hosting
'user' => '...',
'pwd' => '...', ];
if (!mysql_connect(DB['host'], DB['user'], DB['pwd'])) {
die(mysql_error());
} if (!mysql_query($query = 'SET SESSION old_passwords=FALSE')) {
die($query);
} if (!mysql_query($query = "SET PASSWORD = PASSWORD('" . DB['pwd'] . "')")) {
die($query);
}
echo "Excellent, mysqli will now work";
?>
you can do these line on your mysql query browser or something
SET old_passwords = 0;
UPDATE mysql.user SET Password = PASSWORD('testpass') WHERE User = 'testuser' limit 1;
SELECT LENGTH(Password) FROM mysql.user WHERE User = 'testuser';
FLUSH PRIVILEGES;
note:your username and password
after that it should able to work. I just solved mine too
On OSX, I used MacPorts to address the same problem when connecting to my siteground database. Siteground appears to be using 5.0.77mm0.1-log, but creating a new user account didn't fix the problem. This is what did
sudo port install php5-mysql -mysqlnd +mysql5
This downgrades the mysql driver that php will use.
Had the same issue, but executing the queries alone will not help. To fix this I did the following,
Set old_passwords=0 in my.cnf file
Restart mysql
Login to mysql as root user
Execute FLUSH PRIVILEGES;
If you do not have Administrator access to the MySQL Server configuration (i.e. you are using a hosting service), then there are 2 options to get this to work:
1) Request that the old_passwords option be set to false on the MySQL server
2) Downgrade PHP to 5.2.2 until option 1 occurs.
From what I've been able to find, the issue seems to be with how the MySQL account passwords are stored and if the 'old_passwords' setting is set to true. This causes a compatibility issue between MySQL and newer versions of PHP (5.3+) where PHP attempts to connect using a 41-character hash but the MySQL server is still storing account passwords using a 16-character hash.
This incompatibility was brought about by the changing of the hashing method used in MySQL 4.1 which allows for both short and long hash lengths (Scenario 2 on this page from the MySQL site: http://dev.mysql.com/doc/refman/5.5/en/password-hashing.html) and the inclusion of the MySQL Native Driver in PHP 5.3 (backwards compatibility issue documented on bullet 7 of this page from the PHP documentation: http://www.php.net/manual/en/migration53.incompatible.php).
IF,
You are using a shared hosting, and don't have root access.
you are getting the said error while connecting to a remote database ie: not localhost.
and your using Xampp.
and the code is running fine on live server, but the issue is only on your development machine running xampp.
Then,
It is highly recommended that you install xampp 1.7.0 . Download Link
Note: This is not a solution to the above problem, but a FIX which would allow you to continue with your development.
I have installed a local server Xampp, which is running mySQL database in Windows. I created a Database on it with one table. The thing is I cannot get a connection to the database when I use the dbExpress TSQLConnection component. When I set the properties as follows:
ConnectionName = MYSQLConnection
Driver = MySQL
Database = databaseName
HostName = localhost
password =
UserName = root
When I change the connected property to true, I get the following error:
Borland.Data.TDBXError: DBX Error: Driver could not be properly initialized. Client library may be missing, not installed properly, of the wrong version, or the driver may be missing from the system path
I have tried making a connection to the database using the Data Explorer, but I still get the above error. I do not know what I'm missing or doing wrong.
Im using Delphi-XE2.
with mySQL on the server: MySQL client version: mysqlnd 5.0.7-dev - 091210 - $Revision: 304625 $.
I have also tried using an ADO connection, but I do not know how to set the connection string.
I'm still a noob and just want to learn how to make a connection to a mySQL database running on a web server. I cannot afford to buy any components.
Try this!
Did some google Fu - and stumpled upon this link : http://wiltonsoftware.com/posts/view/getting-embarcadero-dbexpress-mysql-working-dbx-error-driver-not-initialized
That seems to fit your needs.
My previous answer was no help .. hope the new one is better.
Old answer:
Make sure you have Data.DBXMySQL in your uses clause.
OK. I'll try a different approach.
Is it working if you setup the connection in the DataExplorer?
If not - then it's not a problem with the uses clause.
(and you obviously have tried that - sry . must be tired :-))
Otherwise a unit could be like this.
unit Unit1;
interface
uses // <-- Uses normally goes right after interface .... (you probably already have one)
Data.DBXMySql;
implementation
end.