How can I use mysql as jupyterhub's database? - mysql

The default database for JupyterHub is a SQLite database. But I want to replace SQLite database with MySQL. I tried with the config variable in the config file.
c.JupyterHub.db_url = 'jdbc:mysql://localhost:3306/test'
But when I run jupyterhub then it yields the error as :
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'jdbc:mysql://localhost:3306/test'
I searched and researched the official docs of jupyterhub but didn't find any thing related to my issue. Any solution to this problem?? Anyone ???
Thank you !

Found the exact line that solve the above problem.
c.JupyterHub.db_url = 'mysql+mysqlconnector://{}:{}#{}/{}{}'.format("username","password","host","database","")

Its an addition to the correct answer above if you dont add that arguement when using mysql you will have timeout issues, as mentioned as per documentation (https://jupyterhub.readthedocs.io/en/stable/reference/database.html)

RFC1738 says that url syntax has to be defined as follows:
//<user>:<password>#<host>:<port>/<url-path>
RFC1738 is not the same syntax than JDBC specification. It looks like you're trying to parse a RFC1738-url (needed by JupyterHub) with JDBC syntax.

Make you also add the option for pool recycling in your JH config. As per documentation.
c.JupyterHub.db_kwargs = {"pool_recycle":300}

Related

SQLAlchemy mysql cannot get the correct charset

Python 3.8.8 programm with Flask 2.0.1 and Flask-SQLAlchemy 2.5.1
MySql database, collation of the tables: utf8_general_ci.
I'm using two other sqlserver DB with SQLALCHEMY_BINDS. Everything runs on Windows 10.
Some chars from select queries on the MySql DB comes wrong: "situazione è decisamente migliorata"
should be: "situazione è decisamente migliorata"
This would solve the problem:
mystring.encode('cp1252').decode('utf8')
but I need a solution at program level. I tried:
appending to SQLALCHEMY_DATABASE_URI connection string:
"?charset=utf8" or "?charset=cp1215" and others
setting app.config['MYSQL_CHARSET'] and
app.config['MYSQL_DATABASE_CHARSET'] to 'utf8', 'utf8mb4', 'latin1', 'cp1252'
...
passing a parameter to SQLAlchemy like db = SQLAlchemy(use_native_unicode="utf8"), many variations here too
No attemp worked. Please I need suggestions.
Are you looking for a way to specify per database connection encoding ?
For all connections try to use
app.config['SQLALCHEMY_ENGINE_OPTIONS'] = {'encoding': 'cp1252'}
For specific connections to different DBs you can also use engine_options:
engine = create_engine('mysql://user:password#hostname/dbname',
encoding='cp1252')
Got the solution.
The problem was not a problem.
The person who build the original database (that is quite old) coded wrong some characters.
Some of my approaches and the one suggested by olegsv, worked, I checked that debugging deep down into into sqlalchemy data structures, the driver accepted the characters encoding, but the very chars in data were themself worong.
This was unespected.
Maybe I should delete the whole question.

Django-MySQL is unable to recognise model.column in queryset extra?

I have SQLite and MySQL installed on my local and development machine respectively. Following is working fine on my local machine(with SQLite):
select_single = {'date': "strftime('%%Y-%%m-%%d',projectName_Modelname.created)"}
queryset.extra(select=select_single)
But since strftime doesn't work with MySQL(link), I tried using DATE_FORMAT() as suggested in given link and other places too.
Though now when I execute below:
select_single = {'date': "DATE_FORMAT(projectName_Modelname.created, '%%Y-%%m-%%d')"}
queryset.extra(select=select_single)
Following error comes:
DatabaseError: (1054, "Unknown column 'projectName_Modelname.created' in 'field list'")
where 'created' is Datetime field in Django model 'Modelname' of app 'projectName'
To debug when I replace projectName_Modelname.created with NOW() no error comes. I have also tried just Modelname.created instead of projectName_Modelname.created though with no benefit?
Note: I am using Django1.5.5
I think it should be something like:
date_raw_query = {'date': "date_format(created, '%%Y-%%m-%%d')"}
and then try
queryset.extra(select=date_raw_query)
Hope that works in your setup. I have tried this on Django 1.7 and MySQL and seems to be working.
Also remember that if SQL errors start coming up, you can always do a print queryset.extra(select=date_raw_query).query to see what might be going wrong.
And when it comes to writing compatible code between SQLite and MySQL like this one, writing a custom MySQL function has been suggested here
But I would suggest otherwise. It's better to have a similar dev environment with MySQL setup in local and also, upgrade Django as soon as possible. :P

Using "Point"-Datatype in Phinx-Migration (in CakePhp)

I'm creating an API for POIs and use the POINT-Type to store the coordinates.
As my company uses CakePHP I have to write a migration-script with Phinx.
But I don't have any Idea how to correctly create a column with the POINT-Type.
Sure, I just could make an "ALTER TABLE ..." in a handwritten Query, but maybe there is a better way?
Versions:
Cake: 3.4.7
Phinx: 0.6.5
MySQL: 5.7.18
Phinx Does not provide an adapter for POINT yet.
You should create your query manually.
See also Unable to seed data with POINT datatype #999
Just use "point" as you would use any other datatype as the second parameter of addColumn().
It's just not documented yet.
Credits for this solution are going to #ndm;
I just think it's worth putting this as answer instead as a comment.
Looks like Phinx supports point types for quite some time now (the docs are not up to date)... try to use \Phinx\Db\Adapter\AdapterInterface::PHINX_TYPE_POINT as the type

User defined variables using SET is not working

I am using node 5.0.0 along with the node module mysql2 - 1.0.0-rc.11. It is working perfectly fine except when I try to use the user defined variables.
Example :
SET #user := 123456;
SELECT * FROM users WHERE user = #user;
The above simple query is throwing syntax error at my end. I am sure that, the syntax is a valid one. So I am wondering what may be the cause for this issue?
Doesn't the node module mysql2 - 1.0.0-rc.11 have support for SET yet? I have scanned the [document1, document2] thoroughly, I couldn't able to find the syntax support list. Can anyone help me to understand what's going wrong with this?
Following up on Andrey Sidorov's comment, you will need to have multipleStatements: true in the options object you pass to createConnection(), createPool(), etc. This is false by default for security reasons, so I don't recommend using it in production code.
https://github.com/mysqljs/mysql#multiple-statement-queries

SubSonic, SQL Server and MySQL problems table names [ ClienteEndereco Clienteendereco ]

I was making tests with SubSonic with SQL Server and everything was fine.
I decided to install MySQL and made the same tests, but now I have a little problem
My table name is ClienteEndereco in SQL Server but when i make change in my web.config to work with MYSQL and recompile the program the table name comes Clienteendereco
SQL Server = Cliente**E**ndereco
MySql = Cliente**e**ndereco
I tried to use regexIgnoreCase="true" but nothing happens.
Can anyone help to solve that? How to make subsonic "ignores case" in SQL Server and MySql?
Thank you.
<add name="mssql"
type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="mssql"
fixPluralClassNames="false"
generatedNamespace="ModeloDados"
regexMatchExpression="A-Za-z"
regexIgnoreCase="true"
removeUnderscores="false"
setPropertyDefaultsFromDatabase="true"
generateNullableProperties="true"
useExtendedProperties="true"
useUtc="true"/>
As Pawel pointed out MySQL on Windows and OS X uses only lowercase names for tables by default. There is a configuration option that you can use to change this behaviour. The following blog posting gives a brief overview:
MySQL Case-sensitivity issues
I founded one way..I used the "regexDictionaryReplace" . This way work in mysql and mssql
<add name="mysql" type="SubSonic.MySqlDataProvider, SubSonic"
regexDictionaryReplace="Empresaendereco,EmpresaEndereco"
fixDatabaseObjectCasing="true" connectionStringName="mysql"
generateRelatedTablesAsProperties="true"
fixPluralClassNames="false"
generatedNamespace="ModeloDados"
regexIgnoreCase="true"
removeUnderscores="false"
setPropertyDefaultsFromDatabase="true"
generateNullableProperties="true"
useExtendedProperties="true"
useUtc="true" />
Thanks for all help!!!