Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » scout » Scout, MySQL and Docker
Scout, MySQL and Docker [message #1749501] Wed, 07 December 2016 12:32
Matthias Zimmermann is currently offline Matthias ZimmermannFriend
Messages: 185
Registered: June 2015
Senior Member
Goal: Get started with MySQL and Docker in 30'
Prerequisite: Running Scout "Hello World" + running Docker installation, some shell (eg. bash as used in the text below)

Steps to reproduce:

  1. Pull MySQL Docker image
  2. Create Database
  3. Add user to access Database
  4. Add MySql Service to Scout "Hello World"
  5. Use the MySql service


1. Pull MySQL Docker Image
Command line sequence to pull MySQL image and start MySQL container
docker pull mysql/mysql-server
docker run --name mysql -e MYSQL_ROOT_PASSWORD=password -d -p 3306:3306 mysql/mysql-server:latest


After the above commands you have an instance of a MySQL server running in a Docker container.

2. Create Database
Use the running container to add a minimal MySQL datbase named 'my_first_db'
docker exec -it mysql mysql -uroot -ppassword


Now continue on the MySQL command line interface (CLI)
create database my_first_db;
use my_first_db;
CREATE TABLE example (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, value VARCHAR(100), time_created TIMESTAMP(6));
INSERT INTO example (value) VALUES ('hello world!');


3. Add User to access Database
This step might not be necessary on a linux or a mac box as the default root user should do(did not try this).

However, on Windows the Docker host is running with a different IP address than the host OS. Therefore, the user that wants to access the MySQL server running in the container needs some additional permissions. See here for more information.

Using the following commands on the MySQL CLI you can add a user with the necessary permissions.
CREATE USER 'scout'@'localhost' IDENTIFIED BY 's3cret!';
GRANT ALL PRIVILEGES ON *.* TO 'scout'@'localhost' WITH GRANT OPTION;
CREATE USER 'scout'@'%' IDENTIFIED BY 's3cret!';
GRANT ALL PRIVILEGES ON *.* TO 'scout'@'%'WITH GRANT OPTION;
FLUSH PRIVILEGES;
exit;


You now have a user scout with password s3cret! to access the sample database from the host OS.

4. Add MySql Service to Scout "Hello World"
There are two points for this step. First, add the necessary Maven dependencies to the pom.xml of the Scout "Hello World" server module.
               <!-- add the two dependencies below -->
		<dependency>
			<groupId>org.eclipse.scout.rt</groupId>
			<artifactId>org.eclipse.scout.rt.server.jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.17</version>
		</dependency>
	</dependencies>
</project>


Most, likely you have now to update the helloworld.server, the helloworld.server.app.dev and the helloworld.server.app.war projekcts (use Alt+F5 or the corresponding context menu in the Package Explorer)

Then, add the MysqlSqlService to the hello world server according to the code shown below:
public class MysqlSqlService extends AbstractMySqlSqlService {
	
	@Override
	protected String getConfiguredJdbcMappingName() {
		String host = "<Your Docker IP here>"; // Docker IP as obtained from 'docker-machine ip default'
		return "jdbc:mysql://" + host + "/my_first_db";;
	}
	
	@Override
	protected String getConfiguredUsername() {
		return "scout";
	}
	
	@Override
	protected String getConfiguredPassword() {
		return "s3cret!";
	}
}


5. Use the MySql Service
This is the simplest step: Just use the new Service in the existing HelloWorldService and restart the hello world server and client app in the Eclipse IDE.

For example:
public class HelloWorldService implements IHelloWorldService {

	@Override
	public HelloWorldFormData load(HelloWorldFormData input) {
		StringHolder text = new StringHolder();
		SQL.selectInto("SELECT value FROM example WHERE id = 1 INTO :text", new NVPair("text", text));

		StringBuilder msg = new StringBuilder();
		msg.append(text.getValue());
		input.getMessage().setValue(msg.toString());
		return input;
	}
}


When the hello world form is opened in the message "hello world!" from the MySQL database is shown in the message field.

[Updated on: Wed, 07 December 2016 13:57]

Report message to a moderator

Previous Topic:Fire property chane events when key is pressed?
Next Topic:Scout, PostgreSQL and Docker
Goto Forum:
  


Current Time: Wed May 24 00:35:15 GMT 2017

Powered by FUDForum. Page generated in 0.02180 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software