I am trying to write a dataframe to a MySQL table. Here is my code:
import pandas as pd
import sqlalchemy
connectString="""mysql+pymysql://userName:passWord@hostIP/schema"""
engine = sqlalchemy.create_engine(connnectString)
connection=engine.connect()
myDataFrame.loc[:,['col1','col2']].to_sql('myTable',con=engine,index=True,if_exists='fail')
The code fails with what seems like a permissions error.
OperationalError: (pymysql.err.OperationalError) (1142, "CREATE command denied to user 'userName'@'hostIP' for table 'myTable'") [SQL: '\nCREATE TABLE `myTable` (\n\ID BIGINT, \n\t`col1` BIGINT, \n\t`col2` BIGINT\n)\n\n']
To test the permissions issue, I am able to successfully create and write a table when I run the following commands:
import pandas as pd
import sqlalchemy
connectString="""mysql+pymysql://userName:passWord@hostIP/schema"""
engine = sqlalchemy.create_engine(connnectString)
connection=engine.connect()
command="""create table myTable as
SELECT * FROM anotherTable"""
engine.execute(command)
connection.close()
Also, the "show grants" command on the database I am trying to write to shows me that I have write permissions on it. The database is installed on a Red Hat Enterprise Linux Server, and I am running the commands from a jupyter-notebook opened on the same OS, but on a different machine.
Any ideas what's going on? Do you need any other info? Thanks.