connecting Canopy to mysql - mysql

I am new to Python and using Enthought Canopy Express for learning purpose. As part of that I am looking for an option to connect Canopy to mysql. I did not find any materials. Please share if you know any method that I can use mysql in Canopy Express.
I am using Mac OS X version 10.9
Path for canopy is:
/Users/mz/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages
Mysql is version 5.6 with the following path:
mysql is /usr/local/mysql/bin/mysql

You can use the Canopy Package Manager to install pymysql:
Once installed, access pymysql according to the Python PEP249 DB API (create a connection, get a cursor, etc.):
In [1]: import pymysql
In [2]: connection = pymysql.connect?
Signature: pymysql.connect(*args, **kwargs)
Docstring:
Establish a connection to the MySQL database. Accepts several
arguments:
host: Host where the database server is located
user: Username to log in as
password: Password to use.
database: Database to use, None to not use a particular one.
port: MySQL port to use, default is usually OK.
...

Related

Access a Remote SQL Server which on Colab

I am quite new to working on remote servers so apologize if this is a silly question, but I couldn't find any instructions anywhere.
I have access to a remote DataBase from University but I need to make a connection through Pulse Secure to access it. (And then I use mySQL workbench to do so). Now I need to get the data to work on Deep Learning, in Colab or anywhere else on the cloud. But I have no idea how to do this. I tried using mySQLdb but it didn't work to get a connection.
Any Help is appreciated.
Here's an example how to install MySQL in Colab and query it. You can change the connection to your university remote database.
# install, set connection
!apt-get install mysql-server > /dev/null
!service mysql start
!mysql -e "ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root'"
!pip -q install PyMySQL
%load_ext sql
%config SqlMagic.feedback=False
%config SqlMagic.autopandas=True
%sql mysql+pymysql://root:root#/
# query using %sql or %%sql
df = %sql SELECT Host, User, authentication_string FROM mysql.user
df

(2059,“Authentication Plugin 'caching_sha2_password'”) when running server connected with MYSQL database on Django

I want to configure my django project in order to connect it with database in MYSQL I created with workbench 8.0,
and then I want to run the server by running
python manage.py runserver
from anaconda command prompt,
so that I can use the Django interface to visualize and alter data.
Please note that I don’t want to downgrade workbench 8.0.
These are the steps I have made:
From anaconda prompt:
pip install mysqlclient
In my project folder, in settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'schema_meta',
'USER': 'root',
'PASSWORD': '<mypassword>',
'HOST': '127.0.0.1',
'PORT': '3306',
},
}
Inside the directory of mysql server, I open cnf.ini and insert a the [client] section:
[client]
database=schema_meta
host=127.0.0.1
user=root
password=<mypassword>
port=3306
default-character-set = utf8
Then from anaconda prompt I run
Python manage.py runserver
And I obtain error
django.db.utils.OperationalError: (2059, "Authentication plugin
'caching_sha2_password' cannot be loaded: Impossibile trovare il
modulo specificato.\r\n")
So I try to solve it by following this thread: django.db.utils.operationalError: (2059,"Authentication Plugin 'caching_sha2_password'")
I open mysql workbench and I run this query:
delete from mysql.user
where user='root'
and host = '127.0.0.1';
flush privileges;
CREATE USER 'root'#'127.0.0.1' IDENTIFIED WITH mysql_native_password BY '<mypassword>';
And then, in the my.ini file I change
default-authentication-plugin= caching_sha2_password
With
default-authentication-plugin=mysql_native_password
Finally from anaconda prompt:
python manage.py runserver
But again I get
django.db.utils.OperationalError: (2059, "Authentication plugin
'caching_sha2_password' cannot be loaded: Impossibile trovare il
modulo specificato.\r\n")
Now, what’s wrong? Why it did not get the changes into the authentication method?
In order to check that there are no other errors, from mysql workbench, from first “home” view, I right click on my database, I open “edit connection”, I click on “test connection”, and the software says that the connection is successfull.
mysql workbench saying connection successfully established
Moreover, I wanted to check if the problem was in my Django settings.
So from anaconda prompt I run
pip install pymysql
Then in the project folders I created a “connect_to_mysql.py” script, with the following code inside:
import pymysql.cursors
mydb = pymysql.connect(host='127.0.0.1',
user='root',
password='<mypassword>',
db='schema_meta',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
print(mydb)
and this seems to work fine, since when I run
connect_to_mysql.py
from anaconda, I get
pymysql.connections.Connection object at 0x000002013F2851D0
That I guess it means “connection successfully established”.
And just to be sure that the problem is into mysql (mysql connector I guess), I create a file “connect_to_mysql_2.py” with this code inside:
import mysql.connector
mydb = mysql.connector.connect(user='root', password='<mypassword>',
host='127.0.0.1', database='meta_schema')
print(mydb)
And when I run it from anaconda, again I get
"Authentication plugin '{0}' is not supported".format(plugin_name))
mysql.connector.errors.NotSupportedError: Authentication plugin
'caching_sha2_password' is not supported
That means that I fixed nothing by working on mysql workbench and in my.ini file.
How can I get my Django connected with my mysql database and my server running?
Is there a way to establish the server connector using pymysql connector instead that mysql connector?
This is probably not a problem in your python code, but in the python connector. The caching_sha2_password plugin is now the default auth plugin and clients have to support it in order to connect. So, the best solution is to update your python connector. An alternative way is to disable this plugin, but that's something I don't recommend as it lowers your server's security.
PyMySQL added support for caching_sha2_password in 0.9.0, though there was a Py2 error fixed in 0.9.1.
Also noted in the install instructions, for caching_sha2_password there is the additional requirement:
python3 -m pip install PyMySQL[rsa]
I think I solved it.
By following this thread
https://stackoverflow.com/a/21740692/7658051
In settings.py, at the DATABASES entry, I substituted
'ENGINE': 'django.db.backends.mysql'
with
'ENGINE': 'mysql.connector.django',
and again, as specified here,
https://django-mysql.readthedocs.io/en/latest/checks.html#django-mysql-w003-utf8mb4
i added also
'OPTIONS': {
# Tell MySQLdb to connect with 'utf8mb4' character set
'charset': 'utf8mb4',
},
# Tell Django to build the test database with the 'utf8mb4' character set
'TEST': {
'CHARSET': 'utf8mb4',
'COLLATION': 'utf8mb4_unicode_ci',
}
and then if I run
python manage.py runserver
It seems to work, even if I cannot access http://127.0.0.1:8000/admin , because I am connected to a legacy database, so I still have to inspect db and stuff I still have to learn.
As second proof that the connection is now working, when I run
connect_to_mysql_2.py
(See (2059,“Authentication Plugin 'caching_sha2_password'”) when running server connected with MYSQL database on Django)
I finally get
mysql.connector.connection_cext.CMySQLConnection object at 0x000001DFB7521550
So I really think this time the connection is on.

mysql error 2049 connection using old (pre-4-1-1) authentication from mac

I have been performing a mysql migration from 4.0.24 to 5.6.12 and was actually done with a python script to transfer the data when a lovely update to mysql broke my authentication to the old server.
I am using a mac os x 10.8.5. previous versions of mysql via brew were 5.5.27, 5.5.28, 5.5.29, & 5.6.12. I had issues with a previous use permissions and ended up blowing all those away (after making a copy of /usr/local). Now all I have is mysql 5.6.13... I was able to re-install 5.6.12 via commit, but the 5.5.* versions(files) are no longer available via mysql.com through brew.
So the error I am getting is this:
machine:folder user$ python migrate.py
Traceback (most recent call last):
......
return DBH( params )
File "dbh.py", line 32, in __init__
db=self.params.get('db')
File "/Volumes/Data/Users/user/.virtualenvs/migrate/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
return Connection(*args, **kwargs)
File "/Volumes/Data/Users/user/.virtualenvs/migrate/lib/python2.7/site-packages/MySQLdb/connections.py", line 187, in __init__
super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (2049, "Connection using old (pre-4.1.1) authentication protocol refused (client option 'secure_auth' enabled)")
Keep in mind I did install mysql-python via pip; and have tried both 1.2.3 and 1.2.4 (no other version available)
and I am using virtualenvwrapper with site packages included... also tried to use pip with different versions over mysql-python
I am not that familiar with the mac OS yet, so i am not exactly sure how one of these packages exactly plays off of each other... my best guess is that brew install mysql is both the server and client libraries, and services; and that the local pip install mysql-python only extends off of the brew install... then the virtualenv would overwrite the local mysql-python install. If this is incorrect please let me know and explain if possible, might help me out too.
I did find the following links, but ultimately did not help:
mysql-error-1064 (this looked like it was heading in the right direction, but did not explain how to implement it specifically.
any clues? Thank you very much in advance.
Versions of MySQL since 5.6.7 have secure_auth enabled by default, which means that a 5.6.7+ client won't allow you to connect if your MySQL user password is hashed using the pre-4.1 method. You said you are migrating from a 4.0.24 installation, so your MySQL user password is definitely hashed using the pre-4.1 method.
When using the mysql command-line tool, you can get around this and connect to your pre-4.1 database by using the --skip-secure-auth command line option. For example:
mysql -h 127.0.0.1 -u username -p --skip-secure-auth
Unfortunately there's no way to disable secure_auth in mysql-python.

app can't connect to local MySQL - osx

I am running some rails app on osx, but when I launch a rails generate command type, I get this message :
/$root/vendor/bundle/ruby/2.0.0/gems/mysql2-0.3.11/lib/mysql2/client.rb:44:in `connect': Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (Mysql2::Error)
For info, my mamp server is running, and the connection must pass through /Applications/MAMP/Library/bin/mysql, so I aliased it by setting in my bash profile :
alias mysql="/Applications/MAMP/Library/bin/mysql"
Btw, what is weird, is that my rails s command works perfectly, so my app can connect to the apache server in that case
So the trick was to simply /tmp/mysql.sock to /Applications/MAMP/tmp/mysql/mysql.sock
ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock
The error indicates that "mysql is not started/running". From what you describe, looks like the new location pointed to the alias has no mysql or its mysql is not started.
Look my.cnf file with the correct parameters in the new location:
/Applications/MAMP/Library/bin/mysql
Another trick is to list your running processes and look for mysql.
Also, are you sure, "rails c" is not using the sqlite3 vs mysql?
Updated:
*Courtesy of #bfavaretto
MySQL my.cnf location on OS X?
By default, the OS X installation does not use a my.cnf, and MySQL just uses the default values. To set up your own my.cnf, you could just create a file straight in /etc.
OS X provides example configuration files at /usr/local/mysql/support-files/
Update:
Take a look at this:
https://stackoverflow.com/questions/4788381/getting-cant-connect-through-socket-tmp-mysql-when-installing-mysql-on-m

connecting mysql through command prompt

I have query regarding connecting mysql to comand prompt.
I did:
open cmd prompt
telnet localhost com 3306
I RECEIVED REPLY as---
some instructions mentioning
telnet [-a][-e escape char][-f log file][-1 user][-t term][host [port]]
-a attempt automatic logon. same as -1 option except uses the currently logged on user's name.
-e Escape char to enter telnet client prompt.
and some more...
but is it right?? or i am lost???
kindly help.
do not telnet to your mysql database.
Instead use the mysql command, a UI like db visualizer (they have a pay version and a free version), or the free ui that comes with maria db (a drop in replacement for mysql).
more more info on the mysql command, try running mysql --help or find it in the mysql reference manual
Edit: more info added here.
Telnet is not a "command prompt", it is a communication protocol (check out telnet protocol on wikipedia) and a program (that uses the communication protocol to communicate). You can not connect to mySql with telnet because mySql does not use the telnet protocol for communication.
I have only accessed mySql for jdbc, so I'm not sure how to solve your problem. I know there is a c api interface for mySql as well. Sections "20 Connectors and APIs," and "15 Replication" in the mysql reference might be helpful.