emacs and mysql on windows 7 - mysql

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.

Related

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.

haskell `hdbc` ODBC connection gets disposed of immediately for remote MySQL instance

I am trying to connect to a MySQL database and run an SQL query using hdbc and hdbc-odbc
main :: IO ()
main = do
mysqlSettings <- readMySQLSettings
putStr "Connecting to MySQL database..."
mysqlConn <- connectODBC $ buildMySQLConnectionString mysqlSettings
putStrLn "Connected"
_ <- run mysqlConn "USE np" []
putStrLn " Done."
The database connects fine but subsequently when it runs an SQL query (_ <- run mysqlConn "USE np" []) I get the following error.
SqlError {seState = "", seNativeError = -1, seErrorMsg = "Tried to use a disposed ODBC Connection handle"}
To my understanding it seems like the error says that the connection gets immediately freed as soon as it's created. This problem only happens when connecting to a remote database (Amazon RDS in this case) and does not for my local MySQL instance.
I guess this happens when you're using an ODBC driver which internally uses libmysqlclient. The thing is, libmysqlclient doesn't restart system calls interrupted by signals, and GHC runtime uses signals internally.
You can see how you can avoid this by looking at Database.HDBC.MySQL.withRTSSignalsBlocked. Simply put, this function blocks SIGALRM and SIGVTALRM which are used by GHC runtime while executing your code block. You may directly use this function, or you can just copy it if you don't want to depend on HDBC-mysql.
Turns out the problem is in the cpp-options I added blindly to the cabal file.
...
cpp-options: -DDCABAL_BUILD_DEVELOPER
build-depends:
base >=4.14.2.0
...
I have no idea what DDCABAL_BUILD_DEVELOPER is used for. Disabling the cpp-options fixed the issue.

DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)

I am designing an application in Python and trying to write to a CSV file, but I am getting this error:
DatabaseError: 1 (HY000): Can't create/write to file '2015-04-06 20:48:33.418000'.csv (Errcode: 13 - Permission denied)
The Code:
def generate_report(self):
conn=mysql.connector.connect(user='root',password='',host='localhost',database='mydatabase')
exe2 = conn.cursor()
exe2.execute("""SELECT tbl_site.Site_name, State_Code, Country_Code,Street_Address, instrum_start_date, instrum_end_date, Comment INTO OUTFILE %s FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n'FROM tbl_site JOIN tbl_site_monit_invent ON site_id = tbl_Site_site_id """, (str(datetime.datetime.now()),))
I can run this code without any errors on a Mac, but I need it to work on Windows.
How can I resolve this error?
Simple really. A colon character is not a valid character in a filename on Windows. It's not allowed.
Reference: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx
The colon character is in the list of "reserved characters", along with several others. (NOTE: One use of the colon character is as a separator for an Alternate Data Stream on NTFS. Ref: http://blogs.technet.com/b/askcore/archive/2013/03/24/alternate-data-streams-in-ntfs.aspx
Followup
The question has been significantly edited since my previous answer was provided. Some notes:
I'm not very familiar with running MySQL on Windows OS. Most of my work with MySQL server is on Linux.
The SELECT ... INTO OUTFILE statement will cause the MySQL server to attempt to write a file on the server host.
The MySQL user (the user logged in to MySQL) must have the FILE privilege in order to use the SELECT ... INTO OUTFILE statement.
Also, the OS account that is running MySQL server must have OS permissions to write a file to the specified directory, and the file to be written must not already exist. Also, the filename must conform to the naming rules for filenames on OS filesystem.
Ref: https://dev.mysql.com/doc/refman/5.5/en/select-into.html
For debugging this type of issue, I strongly recommend you echo out the actual SQL text that is going to be sent to the MySQL server. And then take that SQL text and run it from a different client, like the mysql command line client.
For debugging a privileges issues, you can use a much simpler statement. Test writing a file to a directory that is known to exist, that is known the mysql server has permissions to write files to, and with a filename that does not exist and that conforms to the rules for the OS and filesystem.
For example, on a normal Linux box, we could test with something like this:
mysql> SELECT 'bar' AS foo INTO OUTFILE '/tmp/mysql_foo.csv'
Before we run that, we can easily verify that the /tmp directory exists, that it is writable by the OS account that is running the mysql server, and that the filename conforms to the rules for the filesystem, and that the filename doesn't exist, e.g.
$ su - mysql
$ ls -l /tmp/mysql_foo.csv
$ echo "foo" >/tmp/mysql_foo.csv
$ cat /tmp/mysql_foo.csv
$ rm /tmp/mysql_foo.csv
$ ls -l /tmp/mysql_foo.csv
Once we get over that hurdle, we can move on to testing writing a file to a different directory, a file with a more more complex filename. Once we get that plumbing working, we can work on getting actual data, into a usable csv format.
The original question seems to indicate that the MySQL server is running on Windows OS, and it seems to indicate that the filename attempting to be written contains semicolon characters. Windows does not allow semicolon as part a filename.
It was simply permission error.

Use sqldf in R - cannot specify correct username

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)

SOURCE error 2?

When i try to source an sql file i get the error:
mysql> source C:/Users/tom/Documents/insert.sql
ERROR:
Failed to open file 'C:/Users/tom/Documents/insert.sql', error: 2
I have checked the file path, which looks fine to me. I have also tried \. C:/Users/etc
I am trying to source the sql file which holds insert statements for particular tables. All the statements in the file work when entered manually. What else could i be doing wrong?
Have tried using both backslash and forward slash when using this command
Probably a problem of access right on the file (the file is being accessed by the mysqld server process, not yourself). Try placing the file into the data folder of MySQL, then import it from this location. The location of data folder depends on your distribution and on your own configuration.
Alternatively, feed the SQL script directly to your mysql client's stdin:
mysql [all relevant options] your_database < C:\path\to\your\script.sql
I am using Ubuntu 14.04 version.
I too faced below error 2.
mysql> SOURCE home/loc/Downloads/AllTables.sql;
Failed to open file 'home/loc/Downloads/AllTables.sql', error: 2
Solution :
mysql> SOURCE /home/loc/Downloads/AllTables.sql;
Just added a '/' in front of home
Hope this helps some one.
Have you checked if the file exits? I have had this problem before.
This:
this:
and this works: