Migrating a satchmo store from sqlite3 to mysql - mysql

I have a fully functional satchmo store running on sqlite3. I need to change to mysql though. Whether I create a new database or port the old database, I get http error 500s in all sorts of places. For example if I have a new db, and I create a user account all seems to be fine. If I then attempt to update the user's profile (which is actually and extended user profile), immediately I get the server 500 error. Its like it just isn't interested in that URL any more (http://127.0.0.1:8000/accounts/update/)
The weird thing is that if I log in as an admin and go to the admin pages, then I can update that user's (extended) profile no problem at all. In fact I've never seen any errors on the admin pages.
Here is how I set it up for mysql:
apt-get install mysql-client
apt-get install mysql-server
apt-get install python-mysqldb
mysql -u root -p
mysql> USE mysql;
mysql> CREATE DATABASE mystore CHARACTER SET utf8;
mysql> GRANT ALL PRIVILEGES ON mystore.* TO storeuser#localhost IDENTIFIED BY 'secret';
mysql> FLUSH PRIVILEGES;
settings.py:
DATABASE_ENGINE = 'mysql'
DATABASE_NAME = 'mystore'
DATABASE_USER = 'storeuser'
DATABASE_PASSWORD = 'secret'
DATABASE_HOST = ''
DATABASE_PORT = ''
DATABASE_OPTIONS = { "init_command": 'SET NAMES "utf8"' , "init_command":'SET storage_engine=INNODB' , }
Then to create a new db I just did ths:
python manage.py syncdb
python manage.py runserver
Edit 1
Here is the code where it bombs out:
class myExtendedContactInfoForm(ExtendedContactInfoForm):
def __init__(self, *args, **kwargs):
# The following line is where it produces the error
super(myExtendedContactInfoForm, self).__init__(*args, **kwargs)
Edit 2
It seems like there is something missing from the db because if I go back to using a sqlite back-end, and then delete the sqlite db (and then run 'python manage.py syncdb') I get exactly the same symptoms. Seems like I need to do something more than just syncdb.
What am I missing here?

Related

Access a Remote SQL Server which on Colab

I am quite new to working on remote servers so apologize if this is a silly question, but I couldn't find any instructions anywhere.
I have access to a remote DataBase from University but I need to make a connection through Pulse Secure to access it. (And then I use mySQL workbench to do so). Now I need to get the data to work on Deep Learning, in Colab or anywhere else on the cloud. But I have no idea how to do this. I tried using mySQLdb but it didn't work to get a connection.
Any Help is appreciated.
Here's an example how to install MySQL in Colab and query it. You can change the connection to your university remote database.
# install, set connection
!apt-get install mysql-server > /dev/null
!service mysql start
!mysql -e "ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root'"
!pip -q install PyMySQL
%load_ext sql
%config SqlMagic.feedback=False
%config SqlMagic.autopandas=True
%sql mysql+pymysql://root:root#/
# query using %sql or %%sql
df = %sql SELECT Host, User, authentication_string FROM mysql.user
df

WSO2-Api Manager Database MySQL

I work with Api Manager version 3.0.0 and implement database, MySQL version 5.7.28 in Ubuntu 18.04.3 LTS in localhost with this steps:
In MySQL, create two databases, shared_db and apim_db.
Create a new user for MySQL and add privileges for the user a create in the databases, shared_db and apim_db.
In terminal execute this lines:
mysql -u **NEWUSER** -p -Dshared_db < '[API-MANAGER]/dbscripts/mysql.sql';
mysql -u **NEWUSER** -p -Dapim_db < '[API-MANAGER]/dbscripts/apimgt/mysql.sql';
Create a JAR with this:
apt-get install libmysql-java
In this path /usr/share/java execute:
cp mysql.jar [API-MANAGER]/repository/components/lib
In [API-MANAGER]/repository/conf, the file deployment.toml, edit this lines:
deployment.toml
[database.apim_db]
type = "mysql"
url = "jdbc:mysql://localhost:3306/apim_db?useSSL=false"
username = "NEWUSER"
password = "PASS"
[database.shared_db]
type = "mysql"
url = "jdbc:mysql://localhost:3306/shared_db?useSSL=false"
username = "NEWUSER"
password = ""
Restart to server and works, BUT necessarily use this ?useSSL=false.
So, my cuestion is: Why needs this ?useSSL=false? In production needs this lines?
Thanks.

Raspberry Pi + No privileges in PhpMyAdmin

Here are the steps I wrote for myself to follow, that I used to do my initial set-up.. (in case that was part of he problem some how?)
Used Etcher to flash latest Raspbian Stretch to micro SD card.
Added 'ssh' file to _root of SD card so I can connected via Putty/FTP..etc
Update Repositories:
-- sudo apt-get update
* Install Apache:
-- sudo apt install apache2 //new line
* Give permissions to Pi:
-- sudo chown -R pi:www-data /var/www/html/
-- sudo chmod -R 770 /var/www/html/
* Change Default Web Page:
-- ls -al
** I test if the server is working by opening the web browser and typing the Raspberry Pi's IP address.
** I find out the IP address by typing: ifconfig in the terminal (or my router)
* Install PHP:
-- sudo apt install php php-mbstring
Test PHP install:
//delete default apache index.html
-- sudo rm /var/www/html/index.html
//create new index.php file in its place, with the following content:
-- echo "" > /var/www/html/index.php
*** enabling PHP SHORT TAGS:
-- sudo nano /etc/php/7.0/apache2/php.ini //find short tags and edit/save config file (.ini)
* Install MySQL 7:
-- sudo apt install mysql-server php-mysql
** Type/Create root password when prompted //was never prompted to do so????
Check install:
-- sudo mysql --user=root
Add the wifi credentials:
Create file on the card called wpa_supplicant.conf, which has the following inside:
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
country=US
network={
ssid="Your network name/SSID"
psk="Your WPA/WPA2 security key"
key_mgmt=WPA-PSK
}
Edit country=, ssid= and psk= with your information and save the file.
Use the 2 letter country abbreviation in CAPS (without this you will have no WiFi).
Use a pure text editor, not a word processor, to edit the wpa_supplicant.conf file.
So all the above went fine..without much issue.. (followed an old wifi tutorial that disabled all DHCP ability..but thats been fixed now, and the steps updated above as well)
However.. there was ONE step I followed from this tutorials, how checking your MySQL install:
https://howtoraspberrypi.com/how-to-install-web-server-raspberry-pi-lamp/
"We will no delete the default mysql root user and create a new mysql root user, because the default one can only be used with Linux root account, and so not available for the webserver and php scripts.
To do so, once your connect to MySQL, simply run thoses commands (replace password with the password you want) :"
DROP USER 'root'#'localhost';
CREATE USER 'root'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost'
"So you now have a web server, connected to PHP and MySQL. That’s all it takes.
(On your next connections, you will be able to connect to mysql without using sudo, with the command:
"
# mysql --user=root --password=yourmysqlpassword
Now.. I can 'login' into PhpMyAdmin.. with the credentials above root/password...
but I have NO PRIVILEGES to create a database.. or add tables..etc
and when I'm logged into PhpMyAdmin.. I see these 'pink' alerts.. saying phpmyadmin#local host has no permissions/denied..etc..
So my question(s) are:
1.) What is the correct process after installing MySQL to set up a new user account? Are you supposed to be deleting the default account? Why didnt setting the new user account and privs work? (I get a denied, need an account with creation permissions...etc)
2.) How do I fix my current situation so I can continue with my project.. (which needs a database populated with data to display the site)
Do I need to un-install MySQL? and re-install it? Starting over? How will this affect everything else that is currently installed/working? Any backlash? Negative consequences here? I am new to Linux (Raspbian).. and CLI in general here..
Why didnt this line 'work'?
GRANT ALL PRIVILEGES ON . TO 'root'#'localhost'
And how can I fix this so I do have permissions to use PhpMyAdmin?
To update this, in cae anyone else stumbles upon it... it you look at the block...
its actually missing some syntax:
DROP USER 'root'#'localhost';
CREATE USER 'root'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'root'#'localhost'
The last line is missing the ';' (semi-colon).. which means the last line did NOT execute.. (DOH!).. and hence.. new user has no privs!!!
I would suggest to not only FIX that typo... but also NOT drop the original ROOT user.. and just create a NEW account all together
ie:
CREATE USER 'admin'#'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON *.* TO 'admin'#'localhost'

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

Creating a Mysql user within a Rails controller action

Working on an app that wil be used by admin only to create multiple instances of another app.
In other words, app A will be used to create clones of app B on server and create all the configurations needed to run that cloned app on a subdomain new_clone.domain.com.
Managed to clone the app, created the apache config file, unicorn server settings file too. Managed to run rake db:create; db:migrate but I did this with the root user of mysql.
At the point where I clone the app, I generate a database.yml file for the new cloned app that has at this moment username and password set as root, but I woould like to have a different user for each cloned app.
The app A has a model called Subdomain, in the subdomains_controller.rb at the create action I do all the things, clone, generate config files, running rake tasks, etc... and also in this create action I need to create a new Mysql user with privileges for a specific database.
What I have tried so far, can't say I did much, I tried to run the mysql command within controller create action:
def create
if #subdomain.save
....
system "mysql -u root -p root; create database new_database;..."
....
else
....
end
end
but this puts my create action on hold until I type in the password, and even if I'll find a way to go over it I am not sure the rest of mysql commands will work. Probably there is a better way to add a Mysql user with one command line without going into mysql console.
Thank you.
As far as I understood, your create method runs under Rails app with root mysql user.
Then you can simply execute mysql commands via AR adapter:
# Create DB
ActiveRecord::Base.connection.execute("CREATE DATABASE IF NOT EXISTS \
`#{ActiveRecord::Base.sanitize(db_name)}` CHARACTER SET = `utf8` \
COLLATE = `utf8_general_ci`")
# Create User
ActiveRecord::Base.connection.execute("REPLACE INTO mysql.user \
(Host, User, Password) VALUES(\"localhost\", \
\"#{ActiveRecord::Base.sanitize(db_user)}\", \
PASSWORD(\"#{ActiveRecord::Base.sanitize(db_password)}\"))")
# Grant access
ActiveRecord::Base.connection.execute("GRANT ALL PRIVILEGES ON \
`#{ActiveRecord::Base.sanitize(db_name)}`.* TO \
`#{ActiveRecord::Base.sanitize(db_user)}`")
# Apply
ActiveRecord::Base.connection.execute("FLUSH PRIVILEGES")
Thanks to Yuriy's answer I got it working in a shorter way:
def create_mysql_user
#db_name = "#{#subdomain.name}_domain_production"
#db_user = "#{#subdomain.name}_user"
#db_password = "#{#subdomain.name}domain_password"
ActiveRecord::Base.connection.execute("GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER,
INDEX ON `#{#db_name}`.* TO #{ActiveRecord::Base.sanitize(#db_user)}#localhost
IDENTIFIED BY #{ActiveRecord::Base.sanitize(#db_password)};")
# Apply
ActiveRecord::Base.connection.execute("FLUSH PRIVILEGES")
end