I'm trying to make Django work under Windows 10 but connecting with my (often used) off-site Mysql server I get
django.db.utils.OperationalError: (2006, "Can't connect to MySQL server on 'localhost' (10061)")
While (like I used to do) state in my config file (mysql.conf) to which is pointed to by my settings.py like I used a thousand times before:
[client]
host = 10.8.0.1
database = sitesv
user = niels
password = xxx
port = 3306
default-character-set = utf8
Whether I use the ip within '' or I use a name which is known in my hosts file I still get the same stupid error.... I'm not trying localhost!
In site/settings.py I (still) have:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': BASE_DIR+'\sv\mysql.conf',
},
}
}
With 'still' I mean that I am not frustrated but just halted by some minor mistake which I don't see. ;)
Related
I already have and database in MySQL for my one Django project.
I need to make two separate Django projects share the same database.
project1/settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS' : {
'read_default_file': '/etc/mysql/my.cnf',
},
}
}
project1/etc/mysql/my.cnf:
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
[client]
database = project1
user = project_user
password = Password
port = 3307
default-character-set = utf8
Here, can I have a different database (database = project2) for my second project?
I am willing to use same user and same password.
How can I do that?
CREATE DATABASE project2;
You can run the above query from MySQL client console. That will create new database "project2" in MySQL.
And change the value to "database = project2" in your second project's my.cnf. That should work. You just need to have a different my.cnf file for second project. First project path is "project1/etc/mysql/my.cnf" as mentioned above. You should have something similar "project2/etc/mysql/my.cnf" as second project my.cnf path.
I have deployed my Django app to Google Cloud. It worked fine when I hosted it locally and throughout the steps outlined in this post.
It raises a server Error(500) when I try to view the live link.
When I enable Debug in the settings.py, this is the full traceback. (Torque is the name of my project), and showroom is my app.
The traceback refers to a views attribute (num_manufactureres) which I never had a problem with when hosting it locally.
OperationalError at /showroom/
(2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Request Method: GET
Request URL: https://torque-256805.appspot.com/showroom/
Django Version: 2.2.5
Exception Type: OperationalError
Exception Value:
(2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
Exception Location: /env/lib/python3.7/site-packages/MySQLdb/connections.py in __init__, line 166
Python Executable: /env/bin/python3.7
Python Version: 3.7.4
Python Path:
['/srv',
'/env/bin',
'/opt/python3.7/lib/python37.zip',
'/opt/python3.7/lib/python3.7',
'/opt/python3.7/lib/python3.7/lib-dynload',
'/env/lib/python3.7/site-packages']
Server time: Thu, 24 Oct 2019 09:45:29 +0300
...
/env/lib/python3.7/site-packages/MySQLdb/__init__.py in Connect
return Connection(*args, **kwargs) …
▶ Local vars
/env/lib/python3.7/site-packages/MySQLdb/connections.py in __init__
super(Connection, self).__init__(*args, **kwargs2) …
▶ Local vars
...
num_manufacturers = Manufacturer.objects.all().count() …
▶ Local vars
I'm new to Google Cloud, so I don't know how to start debugging this.
Here are some possible issues:
The server instance I created on the cloud.google.com uses europe-west3 as a region. But when I was deploying, I thought that it created a completely new server and chose europe-west6 as a better option. (Close proximity, better reliability etc...)
I changed my project settings.py for better security according to the check --deploy Django command.
Otherwise, I can't think of anything else. Can anyone help?
Check out this Django example on App Engine settings.py file:
if os.getenv('GAE_APPLICATION', None):
# Running on production App Engine, so connect to Google Cloud SQL using
# the unix socket at /cloudsql/<your-cloudsql-connection string>
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '/cloudsql/[YOUR-CONNECTION-NAME]',
'USER': '[YOUR-USERNAME]',
'PASSWORD': '[YOUR-PASSWORD]',
'NAME': '[YOUR-DATABASE]',
}
}
else:
# Running locally so connect to either a local MySQL instance or connect to
# Cloud SQL via the proxy. To start the proxy via command line:
#
# $ cloud_sql_proxy -instances=[INSTANCE_CONNECTION_NAME]=tcp:3306
#
# See https://cloud.google.com/sql/docs/mysql-connect-proxy
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'HOST': '127.0.0.1',
'PORT': '3306',
'NAME': '[YOUR-DATABASE]',
'USER': '[YOUR-USERNAME]',
'PASSWORD': '[YOUR-PASSWORD]',
}
}
Last but not least, if you're running your app locally, make sure you have ALLOWED_HOSTS= ['localhost'] if your database host is HOST='localhost', or else you can just use 'HOST': '127.0.0.1', as shown in the code sample above.
I have problem with character encoding when pulling data from MySQL database that seems to be specific to Windows computer.
The easy solution that's working on the Linux computer:
# establish connection
db = src_mysql(user = "user", password = "pass", dbname = "training_db", host = "127.0.0.1", port = 3306)
# change/force the character encoding
dbGetQuery(db$con, 'SET NAMES utf8')
If I pull the table with this settings on Linux, everything looks fine, if I do it on Windows computer, I get messy variables from Czech characters in the database.
When I inspect the databases in MySQL Workbench on Win pc, everything seems to be allright.
Does anyone stumbled on the same problem and found a solution?
Found a solution in using dbConnect. Thus in this particular example:
db = dbConnect(drv = RMariaDB::MariaDB(),
username = "user",
password = "pass",
dbname = "training_db",
host = "127.0.0.1",
port = 3306
)
Interestingly, I remember trying to use dbConnect on Linux, but after some issues gravitating to aforementioned src_mysql. Anyway, dbConnect is recommended even in the oficial guidelines nowadays dplyr_guidelines .
How would I connect to my VPS based MySQL database remotely (from a cloud based app) using the Ruby Net::SSH or Net::SSH::Gateway gems and key, not password, authentication?
And then connect to the database with Sequel or DataMapper. I'm assuming that after I manage to get the SSH connection working, I would just setup a Sequel/DM connection to 'sql_user#localhost:3306/database'.
I did locate a couple of similar question here, but they all use password authentication, not keys, and only demonstrate executing raw commands to query the database.
UPDATE: I just cannot seem to get this (Net::SSH with key manager) to work.
UPDATE2: Alright I have managed to get authorization when logging in from a computer that has authorized keys stored in the users local .ssh folder, with the following (port is my custom SQL port on the VPS):
sql_gate = Net::SSH::Gateway.new('192.xxx.xxx.xx','sqluser', port: 26000)
However, I will not be able to create a .ssh folder in the app's VM, so I need to somehow pass the path and filename (I will be creating a public key just for SQL access for specified user) as an option ... but haven't been able to figure out how.
UPDATE: Just need to figure out DataMapper access now. Current code being tested (remote_user_sql is my Ubuntu user, sql_user is the MySQL database user with localhost/127.0.0.1 privileges):
require 'net/ssh/gateway'
require 'data_mapper'
require 'dm-mysql-adapter'
class User
include DataMapp......
.
.
end
ssh_gate = Net::SSH::Gateway.new('192.n.n.n','remote_user_sql', {port: 25000, keys: ["sql_rsa"], keys_only: true})
port = ssh_gate.open('localhost',3306,3307)
child = fork do
DataMapper.setup(:default, {
adapter: 'mysql',
database: 'sql_test',
username: 'sql_user',
password: 'passwd',
host: 'localhost',
port: port})
DataMapper.auto_upgrade!
exit
end
puts "child: #{child}"
Process.wait
ssh_gate.close(port)
My solution, in two parts:
Well I have figured how to make the Net::SSH::Gateway gem using a specified keyfile, and then connect to the VPS through ssh via a port other than 22:
Part 1: Net::SSH::Gateway key authentication
First you must generate the keyfiles you want to use, copy the .pub to the remove server and append it to the ~/.ssh/authorized_keys file (cat sql_rsa.pub >> authorized_keys), and then make sure user_sql (the user I created on the VPS to be used only for this purpose) has been added to AllowUsers list in sshd_config. Make note of port used for ssh (25000 for this example) and use the following code to establish the connection:
ssh_gate = Net::SSH::Gateway.new('192.n.n.n','user_sql', {port: 25000, keys: ["sql_rsa"], keys_only: true})
That will read the keyfile sql_rsa in the same directory as script file, then create a new ssh gateway for 'user_sql'#'192.n.n.n' on port 25000.
I can successfully execute raw shell commands on the remove VPS with:
ssh_gate.exec("ls -la")
To close:
ssh_gate.shutdown!
Unfortunately I am still having problems using DataMapper (do-mysql-adapter) to use the gateway. I will update this answer if I figure that part out, but at least the first half of the problem has been solved.
These are the errors that DataMapper::Logger has reported:
When 127.0.0.1 was used:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) (code: 2002, sql state: HY000, query: , uri: )
When localhost was used:
Access denied for user 'user_sql'#'localhost' (using password: YES) (code: 1045, sql state: 28000, query: , uri: )
When the VPS hostname was used:
Unknown MySQL server host 'hostname' (25) (code: 2005, sql state: HY000, query: , uri: )
UPDATE (No success yet): So far the only way I can access the remote MySQL database is by using Net::SSH::Gateway to establish a gateway, and then use the .sshmethod to open a new Net::SSH connection over that gateway, like so:
ssh_gate.ssh('192.n.n.n','user_sql',{port: 25000, keys: ["sql_rsa"], keys_only: true}) do |ssh|
ssh.exec("mysql -u sql_user -p'passwd' -h localhost -P 3306 -e 'SELECT DATABASE();'")
end
In other words, I can only execute SQL commands using the mysql command line. I cannot figure out how to get Sequel or DataMapper to use the gateway to connect.
Part 2: DataMapper/Sequel/mysql2 connection through Net::SSH::Gateway
Make sure your MySQL server is bound to 127.0.0.1 in /etc/mysql/my.cnf, setup your connection - DataMapper example:
DataMapper.setup(:default, {
adapter: 'mysql',
database: 'DATABASE',
username: 'username',
password: 'passwd',
host: '127.0.0.1',
port: 3307}) # local port being forwarded via Net::SSH:Gateway
Followed by any class table definitions and DataMapper.finalize if required. Note that DataMapper doesn't actually connect to the remote MySQL server until either an auto_upgrade!, auto_migrate!, or query is executed, so no need to create the forwarded port yet.
Then create a new Net::SSH::Gateway, and then whenever you need DataMapper/Sequel to access the remote database, just open a port for the process, like so:
port = ssh_gate.open('127.0.0.1',3306,3307)
child = fork do
DataMapper.auto_upgrade! # DM call that accesses MySQL server
exit
end
Process.wait
ssh_gate.close(port)
You may want to put the Net::SSH::Gateway/.open code in a begin..ensure..end block, ensure'ing the port closure and gateway shutdown.
I had to use a fork and Process.wait to establish the connection, without it the method just hangs.
I know there are tones of questions like that but sadly none of them resolved my problems and as I am new to django I don't know what is wrong.
So I installed django and mysql successfully, then the django-mysql package. When try to import the MySQLdb in python (running in the terminal) it works.
But my question is: how do you start mysql and create a database? And how to "link" it to django?
I tried running mysql but it doesn't work. So I tried /usr/local/mysql/bin/mysql which throws an error ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2).
What do I have to do?
Check your system preferences - there should be a MySQL icon on the bottom. There you can start it.
Also I recommend to install MySQLWorkbench to manage your db
I think that #init3 already answered the question regarding how to start mysql. How do you "link" your database to a django project? Well, in your settings.py file you find something like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'my_database_name', # Or path to database file if using sqlite3.
'USER': 'my_mysql_user', # Not used with sqlite3.
'PASSWORD': 'my_mysql_password', # Not used with sqlite3.
'HOST': '', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
}
}
So change NAME, USER and PASSWORD accordingly and Django will use that database to create tables and populate fields.