Unable to connect to MySQL from Ballerina.io on Mac OS X - mysql

I want to build a simple app that connects to remote MySQL server. However, I can't make it work.
import ballerina/io;
import ballerina/jdbc;
import ballerina/mysql;
endpoint jdbc:Client jiraDB {
host: "jdbc:mysql://DB-SERVER:3306/jira",
username: "jira",
password: "PWD",
poolOptions: { maximumPoolSize: 5 }
};
type Domain record {
string domain,
string jira,
};
function main(string... args) {
var ret = jiraDB->select("SELECT * FROM `domains`", ());
table domainTable;
match ret {
table tableReturned => domainTable = tableReturned;
error e => io:println("Select data from domains table failed: " + e.message);
}
while(domainTable.hasNext()) {
var domain = <Domain>domainTable.getNext();
match domain {
Domain d => io:println("Domain: " + d.domain);
error e => io:println("Error in get employee from table: "
+ e.message);
}
}
}
The structure of MySQL is not really important. I think it has to do with missing / wrongly used JDBC/MySQL library.
Do you please have any ideas how to make it work on Mac OS X ?
$ ballerina run hello.bal
error: ballerina/runtime:CallFailedException, message: call failed
at ..<stop>(hello.bal:5)
caused by error
at ballerina/jdbc:stop(endpoint.bal:66)
I'm using latest Mac OS X with:
$ ballerina --version
Ballerina 0.980.1

First, the latest ballerina version is 0.981.0. It would be great if you could use the latest version since it would include latest bug fixes and improvements.
In Ballerina, there is a generic jdbc client which can be used to connect to any database which has a jdbc driver. In addition, for mysql and h2 there are two clients implemented specifically for those two databases.
When connecting to mysql, you could either use the generic jdbc client or the mysql specific client. The recommendation is to use the mysql specific client.
In your code snippet, I can see you are using jdbc client. As Anoukh mentioned above, the endpoint configuration is incorrect.
Following is a sample configuration for generic jdbc client endpoint.
endpoint jdbc:Client testDB {
url: "jdbc:mysql://localhost:3306/testdb",
username: "user1",
password: "pass1",
poolOptions: { maximumPoolSize: 5 }
};
And following is a sample configuration of mysql client endpoint.
endpoint mysql:Client testDB {
host: "localhost",
port: 3306,
name: "testDB",
username: "user1",
password: "pass1",
poolOptions: { maximumPoolSize: 5 }
};
In order to use either of the clients, you need to copy the mysql jdbc driver to ${BALLERINA_HOME}/bre/lib.
Even after correcting your configuration and copying the driver, if you still face the issue, please check whether file named ballerina-internal.log is created where you are running your bal file and share. Also please share the mysql database and driver version you are using.

Have you copied the MySQL JDBC driver to the BALLERINA_HOME/bre/lib folder?
You can find the ballerina home using which ballerina command.
You can download the mysql jdbc driver from http://central.maven.org/maven2/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar

The issue might be in the jiraDB endpoint configurations. As per the API docs, the config for the URL of the database is to be given as url instead of host.

I was not able to connect to Mysql and I faced a driver instance error. I solved it! I'm not sure to post my answer at the good place but I think it will be a good resource to fix some problems with Mysql connections issues in Ballerina.
In my terminal : echo $BALLERINA_HOME
/Library/Ballerina/ballerina-0.990.2
Copy the good jar in the right place !
Go to : http://central.maven.org/maven2/mysql/mysql-connector-java/
I have downloaded the latest stable version (at the time of writing 8.0.15).
Copy the jar in $BALLERINA_HOME/bre/lib/
I had an error with a prior version.
Be careful that your jar have the right extension (the .jar not the repository with the same name).
Also be sure to have fulfilled the recommandations (see the doc of Oracle when installing a jar, i.e setting the classpath)
In your terminal, set the class path :
export CLASSPATH=$CLASSPATH:/Library/Ballerina/ballerina-0.990.2/bre/lib/mysql-connector-java-8.0.15
Then it will work !

Related

Can angular 7 connecto to mysql using nodejs 'mysql' module

I have some angularjs 1.x experience, and expreimenting angular 7 recently. I know nodejs has a mysql module which can create connection to mysql server and do query. I tried put following code to anguar 7 js file
var mysql = require('mysql');
var connection = mysql.createPool({
host: 'mydbhost',
user: 'mydbuser',
password : 'mydbpasswd',
database : 'mydb'
});
connection.query("select * from mydbtable", function (error, results, fields) {
results.forEach(result => {
console.log(result);
});
});
But angular give me the following compile error:
error TS2580: Cannot find name 'require'. Do you need to install type
definitions for node? Try npm i #types/node and then add node to
the types field in your tsconfig
I want to do crud all by angular 7 without any rest api. Is it possible?
Thanks.
Possible - yes, recommended - absolutely not!
Connecting to the DB from the FE exposes your DB for any malicious user to simply copy your code including credentials to the DB and do whatever they want with it, including corrupting your DB and destroying it!

Initialize Ghost database on new install

I am trying to set up a brand new Ghost blog on a Centos 7 server. I have Nginx, Node and Ghost installed and have written all of the necessary configuration files. It's pretty close to working, but I wanted to use MySQL instead of SQLite, so I created a new (blank) MySQL database called "ghost_db", set up a MySQL user called "ghost", gave the user permission for the database, and added these lines to config.js:
database: {
client: 'mysql',
connection: {
host: 'localhost',
user: 'ghost',
password: 'mypassword',
database: 'ghost_db'
charset: 'utf8'
filename: path.join(__dirname, '/content/data/ghost-dev.db')
},
debug: false
}, ...
When I try to start it, I get an error that suggests I use knex-migrator to initialize the database.
[john#a ghost]$ npm start
> ghost#1.18.4 start /var/www/ghost
> node index
[2017-12-10 00:08:00] ERROR
NAME: DatabaseIsNotOkError
CODE: MIGRATION_TABLE_IS_MISSING
MESSAGE: Please run knex-migrator init ...
However, some comments on Stackexchange suggest that using knex-migrate may be unnecessary for this version of Ghost, and when I run knex-migrator, it also fails:
[john#a ghost]$ knex-migrator init
[2017-12-09 16:21:33] ERROR
NAME: RollbackError
CODE: SQLITE_ERROR
MESSAGE: delete from "migrations" where "name" = '2-create-fixtures.js' and "version" = 'init' and "currentVersion" = '1.18' - SQLITE_ERROR: no such table: migrations
...[omitted]
Error: SQLITE_ERROR: no such table: migrations
I think the problem may be that the "ghost_db" database I initially created is blank. The "ghost-dev.db" file that is pointed to in the config.js seems to be for SQLite, but I get the same error message if I switch config.js back to using an SQLite database. I don't know what the "migrations" table is. I found the schema that I think Ghost expects at [https://github.com/TryGhost/Ghost/blob/1.16.2/core/server/data/schema/schema.js], but I'm not sure how to use that to initialize the tables, etc., except for doing it very laboriously by hand. I'm stumped!
Knex-migrator is new in Ghost 1.0, which also uses a config.<env>.json file for configuration.
It sounds like you added your database config into a file called config.js which was correct <1.0, however it seems you were installing Ghost 1.0 and therefore your new connection details would have needed to live in config.production.json.
You are correct that Ghost-CLI isn't intended for use on CentOS (it's for Ubuntu), but I'd be very surprised if it failed to install Ghost correctly. The issues with other OSs are mainly in the subtle differences between systemd i.e. keeping Ghost running.
The answer for me was just to not create the database at all and let Ghost do it as part of ghost install.
I took an alternate approach which proved successful, which was to install Ghost as an NPM module. The official Ghost instructions label this as an "advanced" process, but it wasn't too difficult to follow the instructions in the excellent nehalist.io and Stickleback blogs. There was also some useful guidance on the HugeServer knowledgebase. I think ultimately the problem was that the Ghost commandline interface (ghost-cli) wasn't designed for Centos 7.

How to configure neo4j server to use bolt

I am currently running awebapp with an embedded neo4j. Now I want to change to a standalone neo4j server using bolt. Neo4j has been loaded onto a standalone and port 7474 work as expected.
Using the following code works as expected:
var authority = neo4j.v1.auth.basic("neo4j", "XXXXXXXX");
_driver = neo4j.v1.driver("bolt://localhost ", authority, {encrypted:false});
However
var authority = neo4j.v1.auth.basic("neo4j", "XXXXXXXX");
_driver = neo4j.v1.driver("bolt://somesite.com/ ", authority, {encrypted:false});
Fails with:
neo4j-web.js:27568 WebSocket connection to 'ws://somesite.com:7687/' failed: Error during WebSocket handshake: net::ERR_CONNECTION_RESET
The port 7687 has been enabled. The neo4j version 3.0.4 and the server operating system is Centos 7.
What am I missing?
Thanks for the help
you need to enable remote connections by adding the following line to conf/neo4j.conf:
dbms.connector.bolt.address=0.0.0.0:7687
Stefan's answer works for Neo4j 3.0 (see this KB article).
For those that are having an issue like Maulik, you are probably using a more recent version of Neo4j (3.5, 4.x), in which case you need to use the following instead:
dbms.connector.bolt.advertised_address=localhost:7687
dbms.connector.bolt.listen_address=0.0.0.0:7687

Error in MySQL library for Node.js

In my Node.js app, I am trying to connect to a MySQL database hosted on Amazon.
$ npm install mysql
My code looks something like this:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'my amazon sql db',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
I can connect to my MySQL DB using Workbench--therefore, I am pretty sure my credentials are okay.
When I attempt to connect I get the following error:
Connection.js:91 Uncaught TypeError: Net.createConnection is not a function
Debugging the code from the npm library--this is where the error is thrown in connection.js:
this._socket = (this.config.socketPath)
? Net.createConnection(this.config.socketPath)
: Net.createConnection(this.config.port, this.config.host);
The connection.js has a dependency :
var Net = require('net');
I am running Node.js locally on my Windows computer.
Can anyone tell me what could be causing this error?
Created a separate ticket:
Error thrown calling Node.js net.createConnection
The net module required and used in the MySQL node module is a core part of Node.js itself. The error you're getting about Net.createConnection not being a function means it's coming up as an empty object and the error is related to one of your comment to the question:
I am testing my code within a browser.
You must run this particular module on Node.js only, you can't run it in a web browser.
One could think a possibility would be to run your code through a packer like browserify or webpack so you can easily require('mysql') in your browser but it won't work. The net module which is a core dependency of the mysql module will be transformed into an empty object {}.
That's not a bug, it's how it's supposed to work. Browsers don't have generic tcp implementations so it can't be emulated. The empty object is intended to prevent require('net') from failing on modules that otherwise work in the browser.
To avoid this error, you need to run this code in a pure Node.js environment, not in a browser. A simple server could serve this purpose since this code in your client in a browser can't work and would add a security hole as everything client-side is manipulative and as such not secure. You don't want to expose your database on the client-side but only consumes it.

Configuring MySql inWildFly

I followed the steps trying to configure MySQL in WildFly. I have two questions for your help with:
1) I downloaded the mysql-connector-java-5.1.33-bin.jar and placed it under modules/system/layers/base/com/mysql/main/. Do I need to download the actual MySql? Or the connector jar is sufficient?
2) In creating a new data source in WildFly console, I was not able to create a new data source. Part of the information I need to fill in is a pair of user name and password to access the database. Where should I create this user name and password first? I am guessing this is where I got the problem from.
I got this error message when testing the connection in wildfly console:
Unexpected HTTP response: 500
Request
{
"address" => [
("subsystem" => "datasources"),
("data-source" => "mysqlDSPool")
],
"operation" => "test-connection-in-pool"
}
Response
Internal Server Error
{
"outcome" => "failed",
"failure-description" => "JBAS010440: failed to invoke operation: JBAS010447: Connection is not valid",
"rolled-back" => true
}
first you need to install Mysql server and a JDBC 4-compliant driver, normally all new JDBCs provided by Mysql.org are JDBC 4-compliant, find a platform independant one here, then you need to add a datasource here in this file standalone/configuration/standalone.xml or using this command
data-source add --name=myDataSource--jndi-name="java:jboss/datasources/myDataSource" \
--connection-url="jdbc:mysql://localhost:3306/myDB" \
--driver-name=h2 --user-name="myDB_Username" --password="myPassword"
username and password are those used to connect to Mysql database.
1) You need to download the jdbc-driver jar which, I think, is the connector jar. But please don't place it under modules/system/... but directly under modules since the system folder is reserved for internal modules that are delivered with the server.
2) Here is an example (configures an Oracle datasource):
/subsystem=datasources/jdbc-driver=OracleJdbcDriver:add(driver-module-name=oracle.jdbcaq,driver-name=OracleJdbcDriver)
/subsystem=datasources/data-source=OracleDS:add(jndi-name=java:jboss/datasources/OracleDS,enabled=true,jta=true,use-java-context=true,connection-url=jdbc:oracle:oci:#dbms:1523/DEV,driver-name=OracleJdbcDriver,min-pool-size=5,max-pool-size=100,user-name=username,password=password,prepared-statements-cache-size=100,exception-sorter-class-name=org.jboss.jca.adapters.jdbc.extensions.oracle.OracleExceptionSorter)