Diary

August 21, 2009

Creating My SQL database and user

Filed under: my-sql — Himanshu @ 7:40 am

I sent email from my hotmail a/c  to my user at server where I had hosted postfix, let’s see if it goes through. See my prior post for the context.

Anyway, this post is for Installed My SQL and PHP. Its pretty easy to install mysql and php in ubuntu. Use apt-get install and you are done. I haven rarely used MySQL, and never used it from command line. After installing mysql also tried creating database as: mysqladmin create <databasename>. Command complainted about password for user. I used switch to supply password and command prompt was on next line. I assumed the command must be successful.

Now, I wanted to setup wordpress. And want to use different database, user then I had created and used in prior command. I wanted to have wp user to do anything on its database, but not more then that.

Googled a bit and found that user can be created in mysql with host specification, found it interesting. (see this). So concept is user can be created with host specification or with wild card (which is % not *). and user will have access to the database accordingly. So same user accessing database from localhost will have more permissions then if s/he access database from outside server.

So, I used following commands to create database and user. This commands needs to be run from mysql command prompt which can be started using mysql –user=root –password=<root-user-password>. I had used root, so you are seeing root user in the command, but any other user who has permission of creating database, creating user and allocating permissions.

   1: CREATE USER '<user-name>'@'localhost' IDENTIFIED BY '<password-for-the-user>';
   2: CREATE USER '<user-name>'@'%' IDENTIFIED BY 'password-for-the-user';
   3: GRANT ALL PRIVILEGES ON <database-name>.* TO '<user-name>'@'localhost';
   4: GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP ON <database-name>.* TO '<user-name>'@'localhost';

Opps, forgot to mention. Please make sure you first create database and then run line 3 and line 4.

Powered by WordPress