How to access ClearDB via terminal like Postgres - mysql

So I used to use Postgres for Heroku and you could access it with a command like:
heroku pg:psql
And it would take you to an interface that looked similar to the local MySQL database that you access with:
mysql -u admin -p *********
Now I want to use ClearDB (my tables are for MySQL), but how do I actually insert the tables via terminal, is there a command like heroku cleardb:something that will open the interface, or is that not possible?
Should I just convert them back to Postgres?

Nevermind, I clearly just had a stroke.
This is the CLEARDB_DATABASE_URL you get in heroku config
CLEARDB_DATABASE_URL => mysql://[username]:[password]#[host]/[database name]?reconnect=true
And then you just stick it into a command like you do when accessing your local DB
mysql --host=[host] --user=[user] --password=[password] --reconnect [database]
Or alternatively into your onboard config file
module.exports = {
'connectionLimit': 100,
'host': [host],
'user': [user],
'password': [password],
'database': [database],
}
Easy peasy, don't know why I didn't catch that.

Related

(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.

How to re-setup Database configurations in Icinga2 server?

I am using ubuntu 16.04 and mysql DB ,i configured DB for icinga2 server and i created separate user in database.
somedays back i deleted that user from db now i am not able access icingaweb2 UI,
Getting below error while trying to access to the link.
All configured authentication methods failed. Please check the system log or Icinga Web 2 log for more information.
I followed the below link to install icinga2 server
Please Help.
I resolved the issue by re-creatinging a setup token in in the Icinga Web 2 by using bellow command
sudo icingacli setup token create
i got some error's while creating setup token and it is easily resolved by using [1]: https://monitoring-portal.org/index.php?thread/40111-icingacli-does-not-work/
If you still have access to the MySQL DB you should do a backup first of your icinga2 DB.
mysqldump -u [username] -p [password] [databasename] > [backupfile.sql]
Then purge MySQL or follow the guide on how to reset your root password
After you have access to the DB again if you purged MySQL use this to bring your data back in.
mysqldump -u [username] -p [password] [databasename] < [backupfile.sql]
If you only lost your password to IcingaWeb2 or now have access to MySQL then create a MD5 based BSD password algorithm:
openssl passwd -1 "password"
Note: The switch to openssl passwd is the number one (-1) for using the MD5 based BSD password algorithm.
Insert the user into the database using the generated password hash:
````
INSERT INTO icingaweb_user (name, active, password_hash) VALUES ('icingaadmin', 1, 'hash from openssl');
Source

How to pull mysql database from heroku to local machine

Hi I have a ruby on rails app hosted on heroku and it is using mysql as database.
Now I have to take backup of the database to my local machine. But I am getting issues while taking backup.
For this I installed taps gem and I am using following commands for it
heroku pg:pull mysql2://username#hostname.cleardb.com/heroku_database local_database --app my_app
but it is giving error as !Your app has no databases.
Can any one guide me how to pull mysql database from heroku to local machine.
EDIT
I have used following syntax for the command
heroku pg:pull <REMOTE_SOURCE_DATABASE> <LOCAL_TARGET_DATABASE>
and for getting REMOTE_SOURCE_DATABASE I have used following command
heroku config:get DATABASE_URL --app my_app
I refer this link1 and link2 for more detailed heroku documentation.
The pg:pull command only works with Postgres databases in your Heroku app. But, you are using a third-party MySQL provider. Your database is hosted on the ClearDB servers and it's available to anyone with the right credentials, including both your app server on Heroku and your dev machine.
Even though there aren't special commands to pull the database, you don't need any - plain mysqldump should do.
mysqldump -h hostname.cleardb.com -u username heroku_database | mysql local_database
Running $heroku config | grep ^DATABASE will give you something like this:
DATABASE_URL: mysql2://username:password#host/dbname?reconnect=true`
From there you can construct your db dump command:
mysqldump -h host -p -u username dbname | mysql local_database
This will prompt you for the password which you received from the previous command. If you wanted to create a script that would automatically include the password from the heroku command you could do something like this:
mysqldump -u username --password=`heroku config | grep ^DATABASE | sed 's/.*[a-z0-9][a-z0-9]*:\([a-z][a-z0-9]*\).*/\1/'` -h host dbname | mysql cedric
In this way you can have a script that will import the database without requiring any user input but also does not expose the password to your database.
(IMPORTANT DISCLAIMER: You MUST have your database.yml configured correctly in order for this to work. I am not responsible for any data you lose as a result of running the below script.)
For Ruby on Rails users ... you could consider writing a Rake task like these db:clone tasks below.
I find myself using this script constantly to clone down from production to development. It's way easier than remembering the mysqldump syntax, much less all of the usernames and passwords involved ...
To clone from production to development:
rake db:clone:production
To clone from staging to development:
rake db:clone:staging
To clone from production to staging:
rake db:clone:production_to_staging
And here's the code enjoy (and be careful in setting up your database.yml):
namespace :db do
namespace :clone do
class << self
%w(development test staging production).each do |env|
define_method("#{env}_db") do
Rails.configuration.database_configuration[env]
end
end
end
def clone_db(from_db, to_db)
start_time = Time.now
puts "Cloning Remote DB...."
system("mysqldump -h#{from_db['host']} -u#{from_db['username']} -p#{from_db['password']} #{from_db['database']} | mysql #{to_db['database']} -u#{to_db['username']} -p#{to_db['password']}")
puts "Import Successful"
end_time = Time.now
puts "===================="
puts "Job Completed: #{end_time - start_time} Seconds"
end
task :staging => :environment do
clone_db(staging_db, development_db)
end
task :production => :environment do
clone_db(production_db, development_db)
end
task :production_to_staging => :environment do
clone_db(production_db, staging_db) if Rails.env.staging?
end
end
end

Ruby: How do I return a MySQL query up an SSH tunnel?

//update: when feeding mysql passwords, -ppassword works. -p password does -not- work. Problem solved.//
We have hundreds of databases on our work servers, and I'd like to write a ruby script that automates the process of creating duplicate versions of them on my local machine for development purposes.
I've been trying to use net-ssh to create the tunnel, but (host, user, pass, etc. are censored for obvious reasons):
require 'net/ssh'
HOST = 'xxx'
USER = 'yyy'
PASS = 'ppp'
Net::SSH.start( HOST, USER, :password => PASS ) do|ssh|
puts "inside ssh tunnel"
puts ssh.exec!('ruby -v')
puts ssh.exec!('mysql -u zzz -p pswrd -h c3 will_il_raw -e "select * from deeds limit 1"')
end
results in the output:
inside ssh tunnel
ruby 1.8.7 (2010-08-16 patchlevel 302) [x86_64-linux]
followed by an indefinite hang. Interestingly, if I ssh into the same computer via the console, 'ruby -v' returns 1.9.3. If I enter that mysql query there, it successfully returns one line of the named table.
I assume the solution has something to do with port forwarding, but here my limited knowledge begins to fail me utterly.
The machine running the mysql server is not the same machine as I am accessing it from, which is not, in turn, the machine I am actually sitting at. I need to connect the dots and apparently have no idea how to go about this properly.
Any protips would be much appreciated.
In MySQL syntax, when feeding a password using -p[password], there is no space between -p and the password.
Because the script's version of the syntax had a space, the result of the query was a prompt requesting the password, which caused the hang on the far end of the SSH tunnel.

Ruby SSH MySQL Sequel (or DataMapper) remote connection with keys using Net::SSH gem

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.