Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Feb 26 '10 at 20:39

This question came from our site for professional and enthusiast programmers.

add comment

5 Answers

up vote 1 down vote accepted

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);
share|improve this answer
add comment

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

share|improve this answer
    
The question is how to create a user, not how to connect to the server. –  Brendan Long Feb 26 '10 at 19:16
    
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. –  Roman Feb 26 '10 at 19:21
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
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? –  Roman Feb 26 '10 at 19:19
1  
That will install the client you can use to access mysql - which you will need - but you will also need to install the actual server itself. Your best bet is to follow the (HowToForge) tutorial in Griffo's post (howtoforge.com/ubuntu_lamp_for_newbies) to make sure that all the working parts are there. –  malonso Feb 26 '10 at 19:46
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.