Can you use Python mysql.connector on actual Server? - python

I have been using the mysql.connector module with Python 2.7 and testing locally using XAMPP. Whenever I upload my script to the server, I am getting an import error for the mysql.connector module. I am assuming this is because, unlike my local machine, I have not installed the mysql.connector module on the server.
My question is: can I somehow use the mysql.connector module on the server or is this something only for local development? I have looked into it, and apparently do not have SSH access for my server, only for the database. As well, if I cannot use the mysql.connector module, how do I connect to my MySQL database from my Python script on the server?

You can use mysql.connector on the server. However, you will have to install it first. Do you have root (admin) access? If no, you might need help from the server admin.

So you can use the mysql.connector on the server, and it can be done without SSH or any special access!
Download it from here: https://dev.mysql.com/downloads/connector/python/2.1.html
(Click on the "Development Releases" tab and download the zip archive)
After you unzip, take the entire "mysql" folder from the lib directory and upload it to the server (wherever your other Python files are, e.g. cgi-bin or similar).
Change the entire mysql directory permission to 755 recursively.
Now you can use it in any Python script on the server by using "import mysql.connector as conn" (or name it whatever you want to)

Related

jupyter-notebook eith oracle connection problem

I have a problem that I've been trying to solve for a very long time and it seems like I'm missing something very basic.
I use a Linux server with Anaconda, Oracle client, Pycharm and jupyter-notebook installed.
I use python scripts in which I write and read data to Oracle DB, and I use the Cx_oracle extension.
The server has several users with a personal username for each and each of them has sudo privileges.
I performed all the installations on the server with sudo privileges.
When I try to connect from the server to Oracle DB I connect properly.
When I connect to Python on the server itself, I connect properly to Oracle DB.
When I connect using Pycharm and I define ORACLE_HOME=/OracleTools/19.0.0/ in the environment variables, I connect properly to the Oracle DB.
My problem starts when I want to use jupyter-notebook
When I try to connect to the DB I get the error -
DatabaseError: Error while trying to retrieve text for error ORA-12715
I noticed when I execute os.environ I see that it is defined for me:
ORACLE_HOME: /OracleTools/19.0.0/bin
and should be
/OracleTools/19.0.0
So I changed using command os.environ['ORACLE_HOME'] = '/OracleTools/19.0.0'
Then I get an error:
DatabaseError: ORA-12715: invalid character set specified
And of course this change is not permanently saved ...
If on the server itself I execute the env command both in the private user and in sudo I see ORACLE_HOME: /OracleTools/19.0.0 and not ORACLE_HOME: /OracleTools/19.0.0/bin
My questions:
Where does the data I get in the os.environ command come from?
How can I edit them permanently ?
Is this even the problem I'm having or should I check something else?
I manage to import cx_oracle, which means that there is no problem of expansion
Thanks!

Connect to Oracle database using python from a Linux server (lxv)

In my local machine
I have created a script in python that retrieves data from an Oracle database.
The connection to the DB is done using cx_Oracle:
con = cx_Oracle.connect (username, password, dbService)
When using SQL developer the connection is established using custom JDBC.
Replicate procedure on a Linux server.
I have created a python virtual environment with cx-Oracle pip installed in it.
I have Oracle Client 19.3.0 installed in the server, and the folder instantclient is in place.
When I try to execute the python script as is I get the following error.
cx_Oracle.DatabaseError: DPI-1047: Cannot locate a 64-bit Oracle
Client library:
DPI-1047: Cannot locate a 64-bit Oracle Client library: "libclntsh.so: cannot open shared object file: No such file or directory". See https://cx-oracle.readthedocs.io/en/latest/user_guide/installation.html for help
I assumed that the problem was the Oracle path which is not the one that python expected. So, I added this extra line of code pin-pointing the path where the Oracle libraries are located.
cx_Oracle.init_oracle_client(lib_dir=r"/apps/oracle/product/19.3.0/lib")
This leads to a different error:
cx_Oracle.DatabaseError: Error while trying to retrieve text for error
ORA-01804
Any clues?
The answer to my question was indicated by the ORA-1804 message.
According to the Oracle initialization doc: https://cx-oracle.readthedocs.io/en/latest/user_guide/initialization.html#usinginitoracleclient
Note if you set lib_dir on Linux and related platforms, you must still
have configured the system library search path to include that
directory before starting Python.
On any operating system, if you set
lib_dir to the library directory of a full database or full client
installation, you will need to have previously set the Oracle
environment, for example by setting the ORACLE_HOME environment
variable. Otherwise you will get errors like ORA-1804. You should set
this, and other Oracle environment variables, before starting Python,
as shown in Oracle
Even though defining the ORACLE_HOME should be done before starting Python (according to Oracle documentation) it is possible to do so by modifying the python script itself. So before the oracle client initialization command the following commands had to be added:
import os
# Setup Oracle paths
os.environ["ORACLE_HOME"] = '/apps/oracle/product/19.3.0'
os.environ["ORACLE_BASE"] = '/apps/oracle'
os.environ["ORACLE_SID"] = 'orcl'
os.environ["LD_LIBRARY_PATH"] = '/apps/oracle/product/19.3.0/lib'
import cx_Oracle
# Initialize Oracle client
cx_Oracle.init_oracle_client(lib_dir=r"/apps/oracle/product/19.3.0/lib")
The cx_Oracle initialization doc points out that on Linux init_oracle_client() doesn't really do what you think it does. You must still set the system library search path to include the Oracle libraries before the Python process starts.
Do I understand correctly that the machine with Python has both the DB installed and Instant Client??
If you do want Python to use the Instant Client libraries, then set LD_LIBRARY_PATH to its location and do not set ORACLE_HOME.
If you have a full Oracle DB installation on the machine with Python, then you can delete Instant Client. You need to set ORACLE_HOME, LD_LIBRARY_PATH and whatever else is needed before starting Python - in general run source /usr/local/bin/oraenv. This should set the system library search path to include /apps/oracle/product/19.3.0/lib . A code snippet like this (untested) one may help: export ORACLE_SID=ORCLCDB;set ORAENV_ASK=NO; source /usr/local/bin/oraenv. Make sure the Python process has read access to the ORACLE_HOME directory.
The cx_Oracle installation guide discusses all this.

Is it possible to use SQL Server in python without external libs?

I'm developing on an environment which I'm not allowed to install anything. It's a monitoring server and I'm making a script to work with logs and etc.
So, I need to connect to a SQL Server with Python 2.7 without any lib like pyodbc installed. Is it possible to make this? I've found nothing I could use to connect to that database.
There are certain things you can do to run sql from the command line from python:
import subprocess
x = subprocess.check_output('sqlcmd -Q "SELECT * FROM db.table"')
print x

IntelliJ IDEA - how to map remote PYTHONPATH to local environment?

I'm using python remote interpreter in IntelliJ(13.1), and using "composes" modules which are installed on server.
By importing the module like follwing, I can use the module without any problem, but I get warn "No module named composes".
import composes
And I can't get the auto complete of the module in editor.
Do I need to map the remote PYTHONPATH to local?
If so, please tell me how to do that.
I found some documentation for this:
http://www.jetbrains.com/pycharm/quickstart/configuring_interpreter.html
I think best way is remote SSH interpreter. Check this out.
Edit: But don't forget. If you choose remote interpreter, you can't use your local modules.
Edit2:
1) Add deployment server from Tool->Deployment->Configuration
2) Add remote interpreter from File->Settings->Project Interpreter->Add remote And select the Deployment Configuration for FTP connection and can send to server your local files
3) And now you can upload your files to server from Pycharm. For this Right click to project folder->Upload to xxx. If all configuration is okay, now your files will upload to server and you can use auto-completion for your local files.
If it doesn't work, please try File->Invalidate cache. And let it delete all cache and download over it again.

Install MYSQLdb python module without MYSQL local install

I'm trying to install MYSQLdb on a windows client. The goal is, from the Windows client, run a python script that connects to a MySQL server on a LINUX client. Looking at the setup code (and based on the errors I am getting when I try to run setup.py for mysqldb, it appears that I have to have my own version of MySQL on the windows box. Is there a way (perhaps another module) that will let me accomplish this? I need to have people on multiple boxes run a script that will interact with a MySQL database on a central server.
you could use a pure python implementation of the mysql client like
pymysql
(can be used as a dropin-replacement for MySQLdb by calling pymysql.install_as_MySQLdb())
MySql-Connector
You don't need the entire MySQL database server, only the MySQL client libraries.
It's been a long time since I wrote python db code for windows...but I think something like this should still work.
If you're running the client only on windows machines, install the pywin32 package. This should have an odbc module in it.
Using the windows control / management tools, create an odbc entry for either the user or the system. In that entry, you'll give the connection parameter set a unique name, then select the driver (in this case MySQL), and populate the connection parameters (e.g. host name, etc.) See PyWin32 Documentation for some notes on the odbc module in pywin32.
Also, see this post: Common ways to connect to odbc from python on windows.

Categories

Resources