The pain of programming

This blog documents a bunch of things I learn. Keeping them here helps me dig out things I might forget, but I hope it could also help others.

Thursday 31 December 2009

Turning a text file into a MySQL DB

Here is something I made from a text file that had formatting like this...
line text1 :: line text1 \n
line text2 :: line text2 \n

In 3 steps, I had it as a MySQL DB;

Step 1)
CREATE DATABASE myDatabaseName;

Step 2)
CREATE TABLE myTableName
(
fieldOne VARCHAR(350),
fieldTwo VARCHAR(350)
);

Step 3)
LOAD DATA LOCAL INFILE 'text-file.txt'
INTO TABLE myTableName
FIELDS TERMINATED BY '::'
LINES TERMINATED BY '\n'
(fieldOne, fieldTwo);

Friday 17 October 2008

Saturday 11 October 2008

MySQL setup on Linux / Setup MySQL for Skeletonz CMS

I've used MySQL with hosting companies. Usually its all setup for me, so I found myself once again a newbie when it came to doing it myself.
I made these notes after reading around some websites. I've documented them here in case I forget any of it.

I installed MySQL a while back and played with some commands in the MySQL client. I realise now that I never set it up properly. I just typed sudo MySQL and then had root access. This was fine for a while, but when it came to installing a CMS locally (Skeletonz) I realised I needed to change the access and login approach.

I might not be 100% right with any of this, but here goes nothing.

With a new install of MySQL you are not configured. You first need to set access rights.
Lets start with root. This root isn't the Linux root, its the MySQL root which is different. To set it up type...

mysqladmin -u root password MyRo0tPassw0rd

...where...
mysqladmin : is the administration setting command,
-u : is the user argument,
root : tells MySQL you want to do something with root access,
password : is the argument to set the password for root,
...and finally the password you want to use.

If all goes well, you can test it like this at the shell prompt...

mysql -u root -p

...where...
mysql : is the command to start mysql,
-u : is the user argument,
root : is the login,
-p : is the password argument.
You should then be prompted for your MySQL root password (not your Linux root password).

So this means you are now logged in as root. However you do not want this when running a CMS for security reasons (sorry I can't give one yet, but I guess its the same reason we don't login to linux as root (normally)). So you need to set up a normal user account. To do this you need to be logged in as MySQL root and maybe create a database to link the user too.

At the mysql> prompt. Type this...

CREATE DATABASE skeletonz;

Now you have created a database you can assign a user to it...
GRANT ALL
ON skeletonz.*
TO username@localhost
IDENTIFIED BY "UserPassword";

ALL : gives the user all access to database commands. It can be replaced with individual commands you want to give your user.
* : grants access to all tables created on the database skeletonz
username : is the username the user will use to login into the database.
localhost : means the user will be accessing the database locally. You could give this user rights to access the DB remotely with this line, TO username@"%" or even an ipaddress.
The last line is the user's password and the ; ends the MySQL statement. Note: this could have been written all on one line.

To test your MySQL user account, quit MySQL and go back to your Linux shell prompt and type...

mysql -u username -p

...where...
username : is the your username.
After entering your user password (not your root password) you will get the mysql> prompt and be logged in as the user.

Thats it!

For users of Skeletonz:
If you are using skeletonz CMS you can edit the config file with the username and password of the created database. Have MySQL running with the skeletonz user logged in and run the skeletonz launcher. Open a browser and type localhost:14002 (unless you have changed the port number in the config file) and you should see your skeletonz homepage.