Tag: database

  • Create A New MYSQL Database and User

    Here is a quick guide to creating a new MYSQL database, adding a user, and then finally a simple table. I will be using debian/ubuntu as the host system.

    First create the database.

    sudo mysqladmin create testdatabase

    Login as the root user.

    sudo mysql -u root

    Create the database user.

    CREATE USER `test_user`@`localhost` IDENTIFIED BY 'password';

    Now grant the new user permissions on the new database.

    GRANT ALL ON testdatabase.* to `test_user`@`localhost`;

    Don’t forget to flush privileges!

    FLUSH PRIVILEGES;

    Now login to your new user/database.

    mysql -u test_user -p testdatabase

    Now we can create a test table.

    CREATE TABLE `members` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` tinytext COLLATE utf8mb4_unicode_ci NOT NULL,
      `description` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `address` text COLLATE utf8mb4_unicode_ci NOT NULL,
      `url` text COLLATE utf8mb4_unicode_ci NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

    Lets get the description of the table to make sure it worked.

    mysql> desc members;
    +-------------+----------+------+-----+---------+----------------+
    | Field       | Type     | Null | Key | Default | Extra          |
    +-------------+----------+------+-----+---------+----------------+
    | id          | int      | NO   | PRI | NULL    | auto_increment |
    | name        | tinytext | NO   |     | NULL    |                |
    | description | text     | NO   |     | NULL    |                |
    | address     | text     | NO   |     | NULL    |                |
    | url         | text     | NO   |     | NULL    |                |
    +-------------+----------+------+-----+---------+----------------+
    5 rows in set (0.00 sec)
    
    mysql> 
    

    Finally, lets add a quick row.

    mysql> INSERT INTO members(name, description, address, url) VALUES('Matt', 'Hackerman', '1234 Main St.','https://pghcpc.com');
    Query OK, 1 row affected (0.01 sec)

    Run a select query now to show your new row.

    mysql> select * from members;
    +----+------+-------------+---------------+--------------------+
    | id | name | description | address       | url                |
    +----+------+-------------+---------------+--------------------+
    |  1 | Matt | Hackerman   | 1234 Main St. | https://pghcpc.com |
    +----+------+-------------+---------------+--------------------+
    1 row in set (0.00 sec)
    

    That’s it! Have fun with your new database!