Access MySQL server from Grails - mysql

I like to setup a dedicated MySQL server in a LAN accessible from other computers of this network. How can I setup the database server and the clients?
How can a Grails application can access the MySQL from the same LAN?

If your MySQL server is going to run on Windows then you can configure it with the installation program. For example, I downloaded the install file mysql-installer-community-5.6.20.0.msi (versions change quickly) and it offers the option of installing just the server:
Just follow the screen prompts and take all the default values (strongly recommended). The main values to remember are:
The default network port number 3306.
The server's ip address.
The username(s) and password(s) that you created that have access to
MySQL.
Then I suggest you download HeidiSQL and configure a connection to your new MySQL server, that way you can manage your database server remotely:
Hope that helps.

You are asking 2 differents questions.
To set up mysql connection in your grails app, did you at least try to read the doc ?
http://grails.org/doc/latest/guide/single.html#dataSource
It's just a jdbc connection string :
https://www.google.fr/?gws_rd=ssl#q=jdbc+mysql+connection+string+example

It's more than just the JDBC connect string. You won't get far without a driver, so uncomment the sample entry in BuildConfig.groovy in the dependencies section and update the version to the most recent:
dependencies {
...
runtime 'mysql:mysql-connector-java:5.1.34'
}
Set the driverClassName in DataSource.groovy, along with the correct JDBC url for your database. Replace <server> with the server name or IP address, and <dbname> with the correct database name. You will likely also want to add parameters at the end of the url in the querystring. And if the port is non-standard (3306 is the default) then add that in also. To ensure that you use INNODB tables (older versions of MySQL default to MyISAM), specify the MySQL5InnoDBDialect (or a subclass):
dataSource {
...
driverClassName = 'com.mysql.jdbc.Driver'
dialect = org.hibernate.dialect.MySQL5InnoDBDialect
url = 'jdbc:mysql://<server>/<dbname>'
}
Run grails compile to download the driver jar and setup the classpath.

Related

jdbc sqlserver url string

I've found this in a software developed by another with j2ee spring_hibernate tomcat_v7 and jre7.
the dbURL is written as follow
dburl = "jdbc:sqlserver://remoteip\\local\\local:1433;database=dbname"
what does the double local means? when removed them the link to sql server 2008 doesn't work anymore.
simple answer to this is in the "integratedSecurity=false;"
when you want to connect to the database installed in the same machine and by the windows authentication then use "integratedSecurity=true" without login or password while you have to put the dll existant in the auth file in the jre lib directory
however if you want to use another machine (remote) this time the integratedSecurity will never work so you will have to choose the sql server connection and then absolutly use "integratedSecurity=false"
String connectionUrl = "jdbc:sqlserver://ServerName\\databaseInstance:1433;databaseName=DBSS;integratedSecurity=false;user=user;password=pass";
you may also open the sql server config manager and set TCP/IP to enabled, the ip adress (IPALL) dynamic to nothing (delete the 0) and the port to 1433 (same as the string connection) and restart the sql server service
you'll probably also need to create a firewall unbound rule for that port.
hope this will help others.

MySQL connection works for Workbench but not Tomcat Application using same credentials

I am working on a remote Redhat system over SSH. Tomcat 7 and MySQL 5.5 server are both installed and appear to be running correctly.
I can connect to MySQL from a local install of Workbench over SSH tunneling using localhost and a defined MySQL user. I can run queries and navigate tables without incident.
When I try to connect to the same MySQL Server with a Tomcat application running in the same environment using the MySQL defined user (same credentials) that Workbench is connecting with successfully, it fails.
At this point I don't know the exact error message that gets generated (I am not an admin) so any guidance here is appreciated.
My question is why can I connect with Workbench (which is effectively connecting locally), but not with my Tomcat application, which actually is local to the server?
Might it be a permissions issue with the context Tomcat is running under?
References:
Connection string example (credentials have been altered):
jdbc:mysql://localhost/mysql_database_name?user=ApplicationUsername&password=ApplicationPassword"
Furthermore, I have verified that
mysql-connector-java-5.1.27-bin.jar
is in the WEB-INF folder, and:
Class.forName("com.mysql.jdbc.Driver");
resolves.
This web application works in both dev and test on another server connecting using respective credentials.
Be sure the port number of mysql server exists in connection string. Normally if port is not specified, default value would be used as 3306. Maybe your mysql runs on different port?
jdbc:mysql://[host][,failoverhost...][:port]/[database] »
[?propertyName1][=propertyValue1][&propertyName2][=propertyValue2]...
If the host name is not specified, it defaults to 127.0.0.1. If the port is not specified, it defaults to 3306, the default port number for MySQL servers.

Does MySQL Connector/J read ~/.my.cnf?

I have a Java application that uses MySQL with the Connector/J MySQL connector. I would like to specify the default client charset in the $HOME/.my.cnf file of the application user, so that it does not affect other applications on the same server.
To test whether Connector/J uses $HOME/.my.cnf, I created the file with the following content:
[client]
socket=/tmp/inexisting-mysql.sock
I expected that the Java application fails to connect to the database, because the /tmp/inexisting-mysql.sock does not exist. However, the application can still connect to the database successfully. It looks like Connector/J is not reading this configuration file at all.
How can I make Connector/J read the $HOME/.my.cnf? Or how can I specify a [client] section option for just my Java application, but not for other applications that use the same database?
Regards, Benedikt
To set the default client charset for a connection you can give that as a parameter to the connection itself:
jdbc:mysql://localhost:3306/sakila?useUnicode=true&characterEncoding=UTF-8
I don't think there is a way to make Connector/J read your $HOME/.my.cnf out of the box. You could read that file in the java application and set the url parameter accordingly though.

Connecting to SQL process with Zend

I'm trying to connect my Zend application to a MySQL process running on a shared server. The basic config should be fine, as it was working with a LAMP server.
The problem is, I need to specify the host as being the an sql process: myprocess.db, rather than localhost:
resources.db.adapter = PDO_MYSQL
resources.db.params.charset = "utf8"
resources.db.params.host = mysqlprocess.db
resources.db.params.username = username
resources.db.params.password = password
resources.db.params.dbname = dbname
However, when I do, I get this:
Warning: mysql_real_escape_string() [function.mysql-real-escape-string]:
Can't connect to local MySQL server through socket 'please_see_the_faq' (2)
in /f5/metamusic/protected/application/controllers/SearchController.php on line 418
The host I'm using is NearlyFreeSpeech, and this message is apparently triggered when attempting to connect to SQL without specifying the process you're interested in:
http://faq.nearlyfreespeech.net/section/mysql/mysqllocalhost#mysqllocalhost
Using the same details and mysql_connect($server, $user) works without issue, so it looks like Zend is somehow not using the correct host parameter.
Any ideas what's going wrong? Any help would be much appreciated.
try using
resources.db.params.host = myprocess.db
The host in the db config has to point to a database server. localhost or 127.0.0.1 are references for the database being on the same server as the application. In a hosting environment you usually have the server on a remote server so the host has to be either an IP address or a DNS name for the host.
Check the second question in the FAQ.
Update
My bad, that is about DSN and not DNS. Still, that's where the problem is. The resources.db.params.host directive in the config expects a reference to the database server and myprocess.db is neither a DNS name nor a IP address. You probably need localhost for that but then you will still be missing the DSN. I currently don't see how you set a DSN in PHP for MySQL and therefore Zend. Have a further look at this MYSQL DSN.
Update 2
You are correct with the socket and that this is related. I think the problem is the Zend PDO_MYSQL adapter. Zend funnels this directly to PDO(). There are this additional config options I mentioned above (MYSQL DSN) which is missing in the Zend implementation. Although the PDO_MYSQL adapter overrides the connect() method it does not look for this options.
However, there is another adapter mysqli which connects directly to MySQL and actually the same way as your test with mysql_connect(). It uses mysqli_real_connect() instead and that connection might understand the process name for the socket. So, you can try the following in your config:
resources.db.adapter = "mysqli"
I'm posting my eventual solution here for future reference:
It turns out, the database connection was already working. However, my call to mysql_real_escape_string() was failing, and the resulting error message suggested that the entire database connection had failed.
The solution was simply to replace the above call with Zend_DB_Adapter's quote(), and suddenly everything works.
Why this works on a LAMP machine and not a shared server, I have no idea. For now though, this is a good enough solution!

How do you connect to a MySQL database using Oracle SQL Developer?

I have Oracle SQL Developer already installed and am able to connect to and query Oracle databases.
Using Help -> Check for Updates I was able to install the Oracle MySQL Browser extension but there are no connection options for MySQL databases.
Under Tools > Preferences > Databases there is a third party JDBC driver path that must be setup. Once the driver path is setup a separate 'MySQL' tab should appear on the New Connections dialog.
Note: This is the same jdbc connector that is available as a JAR download from the MySQL website.
In fact you should do both :
Add driver
Download driver https://maven.atlassian.com/content/groups/public/mysql/mysql-connector-java/5.1.29/
To add this driver :
In Oracle SQL Developper > Tools > Preferences... > Database > Third Party JDBC Drivers > Add Entry...
Select previously downloaded mysql connector jar file.
Add Oracle SQL developper connector
In Oracle SQL Developper > Help > Check for updates > Next
Check All > Next
Filter on "mysql"
Check All > Finish
Next time you will add a connection, MySQL new tab is available !
Here's another extremely detailed walkthrough that also shows you the entire process, including what values to put in the connection dialogue after the JDBC driver is installed: http://rpbouman.blogspot.com/2007/01/oracle-sql-developer-11-supports-mysql.html
My experience with windows client and linux/mysql server:
When sqldev is used in a windows client and mysql is installed in a linux server meaning, sqldev network access to mysql.
Assuming mysql is already up and running and the databases to be accessed are up and functional:
• Ensure the version of sqldev (32 or 64). If 64 and to avoid dealing with path access copy a valid 64 version of msvcr100.dll into directory ~\sqldeveloper\jdev\bin.
a. Open the file msvcr100.dll in notepad and search for first occurrence of “PE “
i. “PE d” it is 64.
ii. “PE L” it is 32.
b. Note: if sqldev is 64 and msvcr100.dll is 32, the application gets stuck at startup.
• For sqldev to work with mysql there is need of the JDBC jar driver. Download it from mysql site.
a. Driver name = mysql-connector-java-x.x.xx
b. Copy it into someplace related to your sqldeveloper directory.
c. Set it up in menu sqldev Tools/Preferences/Database/Third Party JDBC Driver (add entry)
• In Linux/mysql server change file /etc/mysql/mysql.conf.d/mysqld.cnf
look for
bind-address = 127.0.0.1 (this linux localhost)
and change to
bind-address = xxx.xxx.xxx.xxx (this linux server real IP or machine name if DNS is up)
• Enter to linux mysql and grant needed access for example
# mysql –u root -p
GRANT ALL ON . to root#'yourWindowsClientComputerName' IDENTIFIED BY 'mysqlPasswd';
flush privileges;
restart mysql - sudo /etc/init.d/mysql restart
• Start sqldev and create a new connection
a. user = root
b. pass = (your mysql pass)
c. Choose MySql tab
i. Hostname = the linux IP hostname
ii. Port = 3306 (default for mysql)
iii. Choose Database = (from pull down the mysql database you want to use)
iv. save and connect
That is all I had to do in my case.
Thank you,
Ale
Although #BrianHart 's answer is correct, if you are connecting from a remote host, you'll also need to allow remote hosts to connect to the MySQL/MariaDB database.
My article describes the full instructions to connect to a MySQL/MariaDB database in Oracle SQL Developer:
https://alvinbunk.wordpress.com/2017/06/29/using-oracle-sql-developer-to-connect-to-mysqlmariadb-databases/
You may find the following relevant as well:
Oracle SQL Developer connection to Microsoft SQL Server
In my case I had to place the ntlmauth.dll in the sql-developer application directory itself (i.e. sql-developer\jdk\jre\bin). Why this location over the system jre/bin I have no idea. But it worked.