In this short article I will demonstrate how you can set-up a user for remote access to your MySQL server. This will allow you to use the array of awesome GUI tools offered by MySQL AB to administer your remote MySQL databases.

In this article I will be using phpMyAdmin to apply the rights therefore; in order to follow along with this article you will need the following software, all of which are available as free open source downloads. UPDATE: If you have access to SSH or some form of command line utility and are comfortable using this you can also grant the needed privileges running the following command as root: GRANT ALL PRIVILEGES ON *.* to 'root'@'%' IDENTIFIED BY 'password'

On your server you should have installed and running versions of Apache Web Server and MySQL server. If you need help in setting up the software applications, either ask your system administrator or read my article on getting a WAMP environment set-up.

So, once you have everything installed and running the next step is to login to your phpMyAdmin application. Go to the URL where this is installed and log in using your root username and password. Once logged in you will be presented by the phpMyAdmin home interface. On this page you will find a link called ‘Privileges’. Click on this link which will take you to the following page where you can change the privileges to allow for remote administration.

On the right hand side of the list of users is a link to edit the user, click on the edit link for the user you want to give remote access. In my case I have chosen to give my root user this privilege as I want to be able to manage all my databases with one login. You can of course provide limited access by giving a user remote access that has only been given access right to a predefined database or databases.

On the next screen look for a ‘fieldset’ with the title ‘Change Login Information / Copy User’. This is where you are going to change the access rights. On the Host drop down form field change the value from ‘local’ to ‘Any Host’ and hit the ‘Go’ button. Your query will execute and you should receive a message that states ‘Your SQL query has been executed successfully’.

Right, now we have a user that has remote access right, the only steps left over is to add these credentials to the MySQL GUI tools. So without further a due, launch your MySQL Administrator. BTW, you can safely logout of phpMyAdmin at this stage. When the administrator has launched you will be presented with the following screen.

screenshot of the mysql administration login screen

Now, go ahead and click on the button top right labeled ‘…’. This will launch the Options dialog where we will be creating our connection.

screenshot of the mysql administration options dialog

First stop is to create a new connection, so go ahead and click on the button labeled ‘New Connection’. First thing is to give your connection a name. Next, type in the username and password for the user we previously granted remote access rights too. In the following field add your host name. Leave the port number as is unless you know that your MySQL server is running on a different port. If you want to limit this connection to only a specific database you can enter the name of this database in the ‘Schema’ field. After having entered all of the above you should click on ‘Apply’ and then ‘Close’. You now have a new connection that you can select from the ‘Stored Connection’ drop down. For security reasons the password field will be blank, so go ahead and re-enter your password and hit Ok.

If you have followed along and entered everything correctly you will be presented with the MySQL Administrator window. From here you can administer your remote databases in a myriad of ways from doing back-ups and restores to setting up a replication schedule. Your first step though would more then likely be the ‘Catalogs’ view, which will list all of the databases hosted by this MySQL server or just the schema you entered earlier during the set-up process.

That is all there is to it. I hope you find this tutorial useful and that it will go some way in making your MySQL database administration easier.

Until next time,
Schalk Neethling

Merging PDF’s with PDFBox

15 Jan 2008 In: Articles, How-To, Java

Merging Portable Document Format documents using PDFBox couldn’t be simpler. The developer(s) of PDFBox has taken care of all of the hard work and encapsulated it in one class of their Application Programming Interface. All you need to do is use it.

The class I am referring to is the PDFMergerUtility class. This class provides everything you need to take multiple single or multi page PDF documents and merge them into one PDF document. Below I will go over the simple steps of using this class to merge all PDF’s located in a directory without having to pass each file as an argument.

The first step is to initialize the class as follows:

PDFMergerUtility mergePdf = new PDFMergerUtility();

With the class initialized we can start to use it to merge our PDF’s. The next step in our process is to read and store the two arguments that gets passed into our application for later use. When invoking our utility from the command line we expect two arguments to be passed in, the first, the folder that contains the documents and the second, the file name of the final merged PDF. We store these arguments as two String variables:

String folder = args[0];
String destinationFileName = args[1];

The next step is to get hold of all of the files in the directory that was passed to our utility and store them as a String variable called folder. For this I wrote a small method that uses the java.io.File class.

private static String[] getFiles(String folder) throws IOException
{
	File _folder = new File(folder);
	String[] filesInFolder;   

	if(_folder.isDirectory())
	{
		filesInFolder = _folder.list();
		return filesInFolder;
	}
	else
	{
		throw new IOException("Path is not a directory");
	}
}

The first thing we check is that the directory passed to us is in fact a directory. If not, we throw an IOException with the message Path is not a directory. After we verified that this is a directory we use the list() function from the java.io.File class to get the files from the directory. The list() method returns an array of all of the files in the directory. We store this in a String array and return this array to the caller.

String[] filesInFolder;   

if(_folder.isDirectory())
{
	filesInFolder = _folder.list();
	return filesInFolder;
}

Because the final steps of our utility can possibly cause one of two exception two be thrown, we will enclose it within a try/catch block. The first thing we do inside our try block is to store the size of the array as an int variable called numberOfFiles, we will be using this inside our for loop a little later. Next we store our files in a String[] called, you guessed it, files. Armed with this information we can go ahead and loop through our array of files. The reason why we need to loop through our files is because we need to add them to the source of the PDFMergeUtility using it’s addSource function.

The for loop is then also where we will be making use of the first of our two variables, numberOfFiles.

for(int i = 0; i < numberOfFiles; i++)

Inside the loop we add each file to the PDFMergeUtility’s source using the following line of code:

mergePdf.addSource(folder + File.separator + files[i]);

The only steps left for us is to set the file name and location of the merged document and then call the PDFMergeUtility’s mergeDocuments() method.

mergePdf.setDestinationFileName(folder + File.separator + destinationFileName);
mergePdf.mergeDocuments();

To close of our try block we catch the two possible exception that could be thrown by the methods used inside the try block. These are the COSVisitorException and an IOException. With this done our utility is complete! I hope you enjoyed this tutorial and find the utility useful. You can download the complete source here and use it as you see fit. Please feel free to post your comments as to how this utility can be improved and expanded upon.

Until next time,
Schalk Neethling
Volume4

Add to Technorati Favorites

Resources

I am currently using a freely distributed theme for Wordpress. I am aware that it is not as standards compliant and acessible as it can be. To this end I am going to devote some time to ensuring that this theme does conform to the W3C standards and accessibility guidelines. Please post your comments with regards to isssues you encounter while using this site.

Untill next time,
Schalk Neethling

About the Author

Schalk Neethling is a highly experienced and enthusiastic Java standards based web developer currently located in Pretoria, South-Africa. He has been working on the web for the past 6 years developing and designing web sites/applications for clients around the world.

He has extensive knowledge of XHTML, CSS, JavaScript, Ajax, JEE, JSP, PHP and more. He is actively involved in the open source, standards and accessibility community.


Sponsors