Update and or fix the schema of an AppFog Database - javascript

I created an app in NodeJS using ExpressJS, I hosted my app on AppFog and it works, however, I made a few update on my local version, now I'd like to update my App on AppFog but I got a problem, because I updated my models in local and now, I can't update my AppFog database...
What am I suppose to do? Delete the App and Update again? I'll lost all my data in my database...
Logs:
Error: ER_BAD_FIELD_ERROR: Unknown column 'tags' in 'field list'

First export the database to keep a backup just in case this does not go as expected.
af apps to get the db service name for the app
af export-service [service-name] to get a download url of a backup
Next tunnel the service to your local box. Note this can be a little finicky so give it a few tries if needed.
af tunnel [service-name]
Pick the connection option you need. Option "none" will output credentials that can be used with your favorite db tool. Setup a db connection to localhost port 10000 and use the dbname, username, and password provided. Option "mysql" will automatically connect to the db with the mysql cli. Closing the terminal window or exiting af tunnel command will drop the local tunnel so keep it running while making changes. More on af tunnel here.
1: none <- this will let you connect using sequel pro
2: mysql <- to make manual tweeks
3: mysqldump
Finally repair the database structure as needed to get your app working and then update the app.
Additionally, in the future you might what to use an ORM like Sequelize which has non-destructive db migrations that can be run when the app starts up after an af update
Somewhere early in your startup file:
// Sudo code. see af online docs on how to get the bound service creds
if (process.env.NODE_ENV == "production") {
var sequelize = new Sequelize(appfog.dbname, appfog.username, appfog.password, ...)
var migratorOptions = { path: process.cwd() + '/migrations' };
var migrator = sequelize.getMigrator(migratorOptions);
migrator.migrate();
}

Related

Connect to Firestore Emulator locally and production at the same time

I have the need to connect to my production database in my Firebase Emulator project.
In my case it's to access renewed tokens that I have stored in the production database.
I've tried launching a second instance as stated in the Firestore documentation, and i get the console message:
Non-default "firebase-admin" instance created! - This instance will not be mocked and will access production resources.
However it still connects to my local database.
I've also set:
const settings = { host: 'xxxxxxxxxxx.firebaseapp.com'
}
let db = admin.firestore()
db.settings(settings)
But when inspected, the instance is still set to localhost.
I'm thinking it might have to do with something with process.env.FUNCTIONS_EMULATOR that overrides the settings?
Any help appreciated!

ER_NOT_SUPPORTED_AUTH_MODE - MySQL server

Failed at Connecting Node.js Server to MySQL-Database
       
I had MariaDB" installed on a "Node.js Server", but decided that I wanted to use a SQL Database instead. I uninstalled, and completely removed "MariaDB", after which, I proceeded to install the "Community Ed." "MySQL Database". After going through the entire *'MySQL Setup Process'**, I made several attempts to connect to the database via a JavaScript document that implemented the de facto code snippet for a JS DB Connection — my DB-connection document is posted as a code snippet bellow — shown in the code snippet bellow. Disappointingly, the JS/SQL connection failed at each attempt.
Here is the Failed Connection Error Message that I received:
"ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication
protocol requested by server. Consider upgrading MariaDB client."
JS/SQL Connection Snippet that I am using:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '********',
database : 'foobarDb'
});
For MySQL v8.0 use the following:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
You need to reconfigure the Quick Action Settings by clicking the "Reconfigure Link" as shown in the screenshot below. From there, select "Legacy password" for v5.1.
The cause of the error:
You installed the latest "MySQL Version", v8.0. The latest version has a different encryption plugin for authenticating users at login. 5.6, and 5.1 revert to the prior encryption algorithms. Please note, 5.6 & 5.1 have several security vulnerabilities reported by oracle.
The top-rated answers in this Q/A thread are for the most part valid, but they are unorganized, which is to say the least. A solution is here, however, the solution is bits and pieces amongst three other answers. To offer an answer that is a single solution, more helpfull, and is a time saver, I'll make an attempt to write an answer myself in a way that is clear, concise, and orderly. I will cover the whole problem that Ubuntu users experiance, and in addition, I will add information that's helpfull, and not included in any other answer, that will help readers understand the issue that persist for them.
To Start: The Issue is not a SQL Problem, it is an Ubuntu Problem
The issue that persist for you, has to do with the fact (a fact most software developers/I.T. professionals are probably all already aware of) the 'ROOT' user doesn't have a password in Ubuntu, and is accessible by anyone with $ sudo privileges. To offer clarity for anyone experiencing this issue who might be new to some of the semantics that I am throwing out there; Ubuntu users use the sudo -i command to register as the Root-user, whereas, every other Linux distribution in existence uses a User-ID w/ a Password. In truth, I cannot remember ever needing to be a ROOT user for anything other than Database Management, and always only when I am first installing a Database to a server, though my experience is probably far limited in comparison to some IT professionals out there. My point is, typically using sudo for everything does the Job, but in this case it is problematic, so the important thing to note is the following:
PROBLEM:
Ubuntu lacks a 'ROOT PASSWORD' and this is why everyone experiencing the issue that we are discussing runs a Distribution of the Ubuntu OS/SHELL. And unless we rewrite the Ubuntu kernel, and the practically everything else in the operating system, we cannot give Ubuntu SHELL a "root password".
SOLUTION:
We may not be able to give the Ubuntu SHELL a root password, but we can, and we will, give MySQL a 'ROOT PASSWORD'.
TO EXECUTE THE SOLUTION YOU NEED TO HAVE THE FOLLOWING:
Node.js v12+
NPM (Probably need v5+ but don't quote me on that)
MySQL v8.0+ (obviously)
The MySQL Driver (from npm)
CONFIRMATION:
If you don't already have everything on the list you honestly can't say that this is the issue your dealing with.
If you do have everything on the list
and you are running an Ubuntu distro, then you should be getting an error message that probably looks somthing like the one I got when I had to fix this issue.
My error message read:
ERROR: (28000): Access denied for user 'ajc'#'localhost'
ERROR: ERROR_NOT_SUPPORTED_AUTH_MODE: Client does not support
authentication protocol requested by server; consider upgrading
MySQL client
'Client does not support authentication protocol requested by server; consider upgrading MySQL client
If your still reading then your likely in the right place.
To start fixing the problem create an empty Node.js project, and install the MySQL driver as a dependency to it using NPM (you should know how to do this, as you had to do that to have this issue). Add a JavaScript .js file. Call the file, sqltest.js or whatever something like that.
Add the code below to the file you just created.
let mysql = require('mysql');
let connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '********',
database : 'DB_App_00',
});
connection.connect(function(err) {
if (err) {
return console.error('error: ' + err.message);
}
console.log('Connected to the MySQL server.');
});
In the method called 'createConnection' is a JSON OBJ parameter holding the credential values to make a valid connection to the MySQL database server. The user has to equal to 'root', and the database has to exist. Also for a later test add a testing table to the database, with some BS data."
Now open a terminal window, and do your typical updates & upgrades, this is important, which is why every tutorial asks you to do them.
~$: sudo apt update
~$: sudo apt upgrade
After you do your upgrades enter the following command into your terminal:
~$: sudo mysql -u root
It should prompt you for your Ubuntu Password, type it and [ENTER].
The next step is critically important:
Now here is the step that could be considered the medicine and/or the cure to the problem. Your terminal should be open, and you should be inside of the MYSQL Server, under the user 'root'. The terminal should have the cursor flashing at a blank mysql command-line. Within the CMDL copy & paste this:
mysql> ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'ChoosePassword';
mysql> FLUSH PRIVILEGES;
The next part is obvious, change 'ChoosePassword' to a password you will remember while leaving the password within single quotation marks. Change absolutely nothing else, press [ENTER]
If you followed the steps correctly, you now have a MySQL 'ROOT USER' with its own password now. Test it by copy and paste the following at the Ubuntu CMDL:
~$: mysql -u root -p
It will prompt you for your new password, type it and [ENTER]
...you should be in, now exit.
mysql>exit
Back to your 'testsql.js' file, alter the credentials to root for the user, password to your password, a valid database, and host to localhost, unless you have a unique need for a different hostname.
let connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '********',
database : 'DB_App_00',
});
now use node to run the node test file
~$: node testsql.js
Final Thought:
If it doesn't say connected you did something wrong, but if all went well, you should connect. It took some effort before I got it to work, but this answer should save you some time from reading all the other half written answers.
You can use the package mysql2 instead of mysql. I ran into the same issue and using mysql2 worked for me.
You can install this package using npm i mysql2
You can either alter an existing user to use mysql_native_password, or create a new user,
CREATE USER 'new_user'#'%' IDENTIFIED WITH mysql_native_password BY '***';
GRANT USAGE ON *.* TO 'new_user'#'%';
ALTER USER 'new_user'#'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `new_user`.* TO 'new_user'#'%';
FLUSH PRIVILEGES;
replace new_user with your new user name, and set your password.
Now you can access mysql from node using the mysql package,
npm install mysql
recommended to use pool connection for this package.
I figure that some MySQL versions have the authentication for the establishment of a connection a bit messed up. All I had to do was add the line "insecureAuth" : true to the CreateConnection(...) credentials.
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '********',
database : 'vod_bill_database',
insecureAuth : true
});
The error is gone now, and the client is successfully connecting.
1st run this code ->
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
2nd
flush privileges;
try the older version of mysql like 5.6.40, it uses by default SHA256_password auth where new version 8.0. uses by default sha2_password auth which is more secure and throw this authentication protocol error.
MYSQL installer 5.6.40
yellow yow bros !
// mysql.ts
const pool = mysql.createPool({
connectionLimit: 10,
host: "localhost",
user: "root",
password: "password",
database: "rest-resume-api",
});
and then I have a docker-compose file such as
# docker-compose.yml
version: '3.3'
services:
db:
image: mysql
restart: always
command: --default-authentication-plugin=mysql_native_password
environment:
MYSQL_DATABASE: 'rest-resume-api'
MYSQL_USER: 'root'
MYSQL_ROOT_PASSWORD: 'password'
ports:
- '3306:3306'
expose:
- '3306'
volumes:
- my-db:/var/lib/mysql2
volumes:
my-db:
you can do it
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
ALTER USER 'root'#'%' IDENTIFIED WITH mysql_native_password BY 'password'
FLUSH PRIVILEGES;
check mysql root user IDENTIFIED
select user,host,plugin from user;
+------------------+-----------+-----------------------+
| user | host | plugin |
+------------------+-----------+-----------------------+
| root | % | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session | localhost | caching_sha2_password |
| mysql.sys | localhost | caching_sha2_password |
| root | localhost | mysql_native_password |
+------------------+-----------+-----------------------+
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '********',
database : 'vod_bill_database',
port : 3308
});
I had the same error and since i changed my port in phpmyadmin from 3306 to 3308 therefore here also i had to write port: 3308 and it started working.
in order to overcome this error use the following code:
var connectionString = 'mysql://*root:*password#*localhost/*database?charset=utf8_general_ci&timezone=-0700';
var connection= mysql.createConnection(connectionString);
but make sure that you changed the * marks in the connectionString based on your setup.
just create a new user on MySQL
CREATE USER 'foo'#'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';

How do I allow the node pg module communicate with my Windows postgresql server?

I have a node server on localhost. In it is the pg module:
//above is express requires
var pg = require('pg');
var client = new pg.Client();
client.connect((err)=>{
if(err) throw err;
client.end((err)=>{
if(err) throw err;
});
})
//below is basic express server stuff
I have, I believe, a postgresql server running on windows. I have pgadmin open, there is a localhost:5432 section, and inside is a "test" database I have created.
My node server currently throws:
error: password authentication fails for user 'myname'
I do not understand how I can, either in postgresql's Windows client, set default username/password that is then added to environment variables, or, in Windows add the proper environment variable key/value pairs that the node pg module expects so it can login, or, tell the node pg module how to login to postgresql, or, whether I am even going down the right path at all.
How can I allow my node pg module to connect to my localhost postgresql server?
EDIT: I tried to follow the instructions in this SO answer, however I would always recieve "access denied" errors no matter where I pointed the db creation to, whether I used cmd or powershell in admin or no, and no matter the permissions I set for files. I'm now primarily interacting with postgresql through the pgadmin III GUI.
EDIT2: I tried modifying the pg initialization by doing:
var connectstring = 'postgres://me:password#localhost/dbname';
var client = new pg.Client(connectstring);
I received the same error, using the same exact login details I was prompted with by pgadmin III.
EDIT3: According to these docs, I should possibly have that list of environment variables? If not, where can I find this information so I can manually set these environment variables? Is this the right path to go down?
Use:
var pg = require('pg');
var config = {
user: 'foo',
database: 'my_db',
password: 'secret',
port: 5432
};
var pool = new pg.Pool(config);

MONGO_URL for running multiple Meteor apps on one server

I have one Meteor application running on my Ubuntu server (Digital Ocean). I use Meteor Up (MUP) to deploy and keep the app running. Everything works fine.
However, when I try to deploy a second app on the same server, something goes wrong in connecting to the MongoDB. I get a long and unreadable error message that starts "Invoking deployment process: FAILED" and then ends with
Waiting for MongoDB to initialize. (5 minutes)
connected
myapp start/running, process 25053
Waiting for 15 seconds while app is booting up
Checking is app booted or not?
myapp stop/waiting
myapp start/running, process 25114
And the app refuses to run. I have tried a number of things to fix this and will edit this post if more info is requested, but I'm not sure what's relevant. Essentially I don't understand the Error message, so I need to know what the heck is going on?
EDIT:
I want to add that my app runs fine if I go into the project folder and use the "meteor" command. Everything runs as expected. It is only when I try to deploy it for long-term production mode with MUP that I get this error.
EDIT:
I moved on to trying mupx instead of mup. This time I can't even get past the installation process, I get the following error message:
[Neal] x Installing MongoDB: FAILED
-----------------------------------STDERR-----------------------------------
Error response from daemon: no such id: mongodb
Error: failed to remove containers: [mongodb]
Error response from daemon: Cannot start container c2c538d34c15103d1d07bcc60b56a54bd3d23e50ae7a8e4f9f7831df0d77dc56: failed to create endpoint mongodb on network bridge: Error starting userland proxy: listen tcp 127.0.0.1:27017: bind: address already in use
But I don't understand why! Mongod is clearly already running on port 27017 and a second application should just add a new database to that instance, correct? I don't know what I'm missing here, why MUP can't access MongoDB.
It's tricky without your mup.json to see what's going on here. Given what you said, it looks like your 2nd app deployment tries to override/boot mongodb over the 1st one which is locked, the mongodb environment fails to boot, causing then the fail. You should tackle this different ways:
If your objective is to share your mongoDB, point the MONGO_URL from your 2nd mup.jon on your first mongodb instance. It's generally something along the 2701X ports. As it's a shared DB, changes in one database could affect the other.
meteor-up oversees the deployment of your app from a meteor-nice-to-test thing to a node+mongodb environment. You can spawn another mongod instance with :
mongod --port 2701X --dbpath /your/dbpath --fork --logpath /log/path on your DO server and then point MONGO_URL there.
Last but not least, mupx having docker under the hood. Using mupx for your deployments should isolate both apps from each other.

Permanent session storage in Node

I have a node server running right now, I use npm forever to keep it running when I'm not developing and I use npm nodemon for when I edit (restarts app on edit/upload).
I noticed that whenever I restart my app, session data is lost and my players have to re-login to their accounts. It's not a huuuge deal, but I was wondering if there was a way to edit my server.js page without restarting the app, and logging everybody out?
(Note that this only applies for server.js or module edits. .html and .js pages that are served don't require a restart)
(Second note: I am using mysql, nodejs, angularjs, express.io for all this, just in case anybody asks)
There isn't a way to edit a file (and have the changes loaded) without rebooting the server and reloading the file. Instead, store your session data somewhere other than memory. I know you're using MySQL, so use the connect-mysql package (there are other packages like this for redis, MongoDB, etc).
It's as simple as putting this in your app.js file:
var MySQLStore = require('connect-mysql')(express)
, options = {
config: {
host : 'place.stuff',
user: 'RUJordan',
password: 'hunter2',
database: 'SomeKittensIsGreat'
}
};
app.use(express.session({
secret: 'UpvoteThisAnswer',
store: new MySQLStore(options),
cookie: { maxAge: 2592000000 } // 30 days
}));
This will place a HTTP-only (i.e. the user can't see/use it) cookie on each user's computer. Even after a server reboot, connect-mysql will be able to like a user with their session data in MySQL via this cookie.

Categories