Saturday, January 29, 2011

How do I create a data base in MySQL and add a user?

To start working with MySQL from PHP I need to connect to the MySQL server using "mysql_connect". Doing so, I need to specify user-name and password. But for that I need first to create a user with a password. How can I do it?

After I connect to the MySQL server I need to select a data base. But for that the DB should exist. How do I create a DB? Can I do it from PHP?

  • UPDATE: Since you don't have MySQL installed, here are instructions for installing PHP, Apache and MySQL. howtoforge.com


    You'll need a user setup before you create the DB. This will probably have to be done through mysql itself or through your ISPs control panel

    The MySQL command syntax for adding a user is as follows:

    CREATE USER user [IDENTIFIED BY [PASSWORD] 'password']
        [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
    

    You can then use the GRANT command to allow particular access. Here's the syntax:

    GRANT
        priv_type [(column_list)]
          [, priv_type [(column_list)]] ...
        ON [object_type] priv_level
        TO user [IDENTIFIED BY [PASSWORD] 'password']
            [, user [IDENTIFIED BY [PASSWORD] 'password']] ...
        [REQUIRE {NONE | ssl_option [[AND] ssl_option] ...}]
        [WITH with_option ...]
    
    object_type:
        TABLE
      | FUNCTION
      | PROCEDURE
    
    priv_level:
        *
      | *.*
      | db_name.*
      | db_name.tbl_name
      | tbl_name
      | db_name.routine_name
    
    ssl_option:
        SSL
      | X509
      | CIPHER 'cipher'
      | ISSUER 'issuer'
      | SUBJECT 'subject'
    
    with_option:
        GRANT OPTION
      | MAX_QUERIES_PER_HOUR count
      | MAX_UPDATES_PER_HOUR count
      | MAX_CONNECTIONS_PER_HOUR count
      | MAX_USER_CONNECTIONS count
    

    Here's the method for using mysql_connect to connect to a database: php.net
    Here's the method for creating a db with php: php.net

    Brendan Long : The question is how to create a user, not how to connect to the server.
    Roman : I did it before through control panel (when I used commercial web server). But now I am working on my computer and I have no control panel. So, I think I need to do it via command line in MySQL. And the question is how to do it.
    From Griffo
  • http://dev.mysql.com/doc/refman/5.1/en/create-user.html

    There's a command to set the root user's password, but I don't remember what it is.

  • Have you installed the MySQL server yet? If not try going here:

    http://dev.mysql.com/doc/refman/5.1/en/installing.html

    If you have then you can skip to this section:

    http://dev.mysql.com/doc/refman/5.1/en/unix-post-installation.html

    Roman : When I type mysql in the command line (in Ubuntu) I get: The program 'mysql' is currently not installed. It is also written: You can install it by typing: sudo apt-get install mysql-client-5.0. I think it will be the easiest way if I just type "sudo apt-get install ...". Will it work this way?
    From malonso
  • you can do the following with the mysql command line client

    MYSQL CREATE DB COMMAND - CREATE DATABASE mydbname

    MYSQL CREATE USER COMMAND - CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';

    MYSQL ASSIGN USER PRIVILEGES COMMAND - GRANT ALL PRIVILEGES ON mydbname TO 'myuser'@'localhost';

    your php connect script would be something like this

    $dbuser = "myuser";
    $dbpass = "mypassword";
    $dbhost = 'localhost';
    $dbname = "mydbname";
    
    $db = mysql_connect ($dbhost,$dbuser,$dbpass);
    mysql_select_db ($dbname,$db);
    
  • There are lots of good tool providing you with the details about how to do it from the command line. Another method you might want to consider is setting up phpMyAdmin or the Mysql GUI Tools. These interfaces will give you an easier starting point.

    From Zoredache

0 comments:

Post a Comment