Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MariaDB establishes a connection


May 16, 2021 MariaDB


Table of contents


One way to establish a connection to MariaDB is to use mysql binary at the command prompt.

MySQL script

Check out the example given below.

[root@host]# mysql -u root -p

Enter password:******

The code given above connects to MariaDB and provides a command prompt to execute sql commands. A fter entering the code, a welcome message is displayed indicating that the connection was successful and the version number is displayed.

Welcome to the MariaDB monitor. Commands end with ; or g. 
Your MariaDB connection id is 122323232 
Server version: 5.5.40-MariaDB-log
  
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.  
mysql> 

The example uses root access, but any user with permissions can of course access MariaDB prompts and take action.

Disconnect from MariaDB with the exit command as follows -

mysql> exit

PHP connection script

Another way to connect to and disconnect from MariaDB is to use PHP scripts. P HP provides the first () function to mysql_connect database connection. I t uses five optional parameters and returns the MariaDB link identifier after a successful connection, or the false on a failed connection. I t also provides a function of mysql_close to close database connections, which use a single argument.

Grammar

Check out the following PHP connection script syntax -

connection mysql_connect(server,user,passwd,new_link,client_flag);

The parameters are described below -

S.No Parameters and descriptions
1

server

This optional parameter specifies the host name of the server running the database. I ts default value is "localhost:.3036".

2

user

This optional parameter specifies the user name to access the database. I ts default value is the owner of the server.

3

passwd

This optional parameter specifies the user's password. I ts default value is empty.

4

new_link

This optional parameter specifies that the identifier of the current connection is returned on the second call to mysql_connect(), using the same parameter, instead of the new connection.

5

client flags - the client's logo

This optional parameter uses a combination of the following constant values -

  • MYSQL_CLIENT_SSL - it uses ssl encryption.

  • MYSQL_CLIENT_COMPRESS - It uses a compression protocol.

  • MYSQL_CLIENT_IGNORE_SPACE - It allows spaces after the function name.

  • MYSQL_CLIENT_INTERACTIVE - It allows the number of interactive timeouts to be in activity before the connection is closed.

Check out the PHP break script syntax given below -

bool mysql_close ( resource $link_identifier );

If a resource is omitted, the most recently opened resource is closed. I t returns true, or false, on successful shutdown.

Try the example code below to connect to the MariaDB server -

<html>
   <head>
      <title>Connect to MariaDB Server</title>
   </head>

   <body>
      <?php
         $dbhost = 'localhost:3036';
         $dbuser = 'guest1';
         $dbpass = 'guest1a';
         $conn = mysql_connect($dbhost, $dbuser, $dbpass);
      
         if(! $conn ) {
            die('Could not connect: ' . mysql_error());
         }
         
         echo 'Connected successfully';
         mysql_close($conn);
      ?>
   </body>
</html>

After a successful connection, you will see the output below -

mysql> Connected successfully