MySQL (6) --- Database Management

MySQL Management


Start-up and shut down the MySQL server

Windows system

In Windows, open a command window (cmd), go to the bin directory of your MySQL installation directory.

start up:

cd c:/mysql/bin
mysqld --console

shut down:

cd c:/mysql/bin
mysqladmin -uroot shutdown

Linux system

First, we need to check whether the MySQL server is started by the following command:

ps -ef | grep mysqld

If MySql has been started, the output of the above command mysql process list, if mysql is not started, you can use the following command to start mysql server:

root@host# cd /usr/bin
./mysqld_safe &

If you want to shut down the MySQL server currently running, you can execute the following command:

root@host# cd /usr/bin
./mysqladmin -u root -p shutdown
Enter password: ******

MySQL user settings

If you need to add MySQL user, you only need to add a new user to the user table in the mysql database.

The following examples add a user, the user name guest, password guest123, and authorized users can SELECT, INSERT and UPDATE operating authority:

root@host# mysql -u root -p
Enter password:*******
mysql> use mysql;
Database changed

mysql> INSERT INTO user 
          (host, user, password, 
           select_priv, insert_priv, update_priv) 
           VALUES ('localhost', 'guest', 
           PASSWORD('guest123'), 'Y', 'Y', 'Y');
Query OK, 1 row affected (0.20 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 1 row affected (0.01 sec)

mysql> SELECT host, user, password FROM user WHERE user = 'guest';
+-----------+---------+------------------+
| host      | user    | password         |
+-----------+---------+------------------+
| localhost | guest | 6f8c114b58f2ce9e |
+-----------+---------+------------------+
1 row in set (0.00 sec)

When you add a user, please note that use PASSWORD MySQL provided () function to encrypt the password. You can see in the above example the user password encryption as: 6f8c114b58f2ce9e.

Note: In MySQL5.7 the password in the user table has been replaced authentication_string .

Note: password () function has been removed in the encryption 8.0.11, you can use the MD5 () function instead.

Note: In attention to the need to perform  FLUSH PRIVILEGES  statement. This command will reload the grant tables after execution.

If you do not use this command, you will not be able to use the newly created user to connect to mysql server unless you restart the mysql server.

You can assign permissions to users in the corresponding column permissions, set in the insert statement when creating the user 'Y' can be, user rights are listed below:

  • Select_priv
  • Insert_priv
  • Update_priv
  • Delete_priv
  • Create_priv
  • Drop_priv
  • Reload_priv
  • Shutdown_priv
  • Process_priv
  • File_priv
  • Grant_priv
  • References_priv
  • Index_priv
  • Alter_priv

Another way to add users through the SQL GRANT command, the following command will add a user specified database TUTORIALS zara, password zara123.

root@host# mysql -u root -p
Enter password:*******
mysql> use mysql;
Database changed

mysql> GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP
    -> ON TUTORIALS.*
    -> TO 'zara'@'localhost'
    -> IDENTIFIED BY 'zara123';

The above command will be in the user table in the mysql database to create a record of user information.

Note:  MySQL SQL statements with a semicolon (;) as the end identifier.


/etc/my.cnf file configuration

Under normal circumstances, you do not need to modify the configuration file, the default configuration file as follows:

[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

[mysql.server]
user=mysql
basedir=/var/lib

[safe_mysqld]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

In the configuration file, you can specify a different directory error log file storage, you generally do not need to change these configurations.


MySQL Management Command

The following lists the command uses Mysql database procedure commonly used:

  • USE  database name  :
    Select Mysql database to operate, use this command after all only Mysql command against the database.

    mysql> use RUNOOB;
    Database changed
  • SHOW DATABASES: 
    list database list MySQL database management systems.

    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema |
    | RUNOOB             |
    | cdcol              |
    | mysql              |
    | onethink           |
    | performance_schema |
    | phpmyadmin         |
    | test               |
    | wecenter           |
    | wordpress          |
    +--------------------+
    10 rows in set (0.02 sec)
  • SHOW TABLES:
    Display all the specified database table, need to use the database to select a command to operate before using this command.

    mysql> use RUNOOB;
    Database changed
    mysql> SHOW TABLES;
    +------------------+
    | Tables_in_runoob |
    +------------------+
    | employee_tbl     |
    | runoob_tbl       |
    | tcount_tbl       |
    +------------------+
    3 rows in set (0.00 sec)
  • SHOW COLUMNS FROM  Datasheet :
    displaying the attribute table, the attribute type, primary key information, whether the additional information is NULL, the default values and the like.

    mysql> SHOW COLUMNS FROM runoob_tbl;
    +-----------------+--------------+------+-----+---------+-------+
    | Field           | Type         | Null | Key | Default | Extra |
    +-----------------+--------------+------+-----+---------+-------+
    | runoob_id       | int(11)      | NO   | PRI | NULL    |       |
    | runoob_title    | varchar(255) | YES  |     | NULL    |       |
    | runoob_author   | varchar(255) | YES  |     | NULL    |       |
    | submission_date | date         | YES  |     | NULL    |       |
    +-----------------+--------------+------+-----+---------+-------+
    4 rows in set (0.01 sec)
  • SHOW INDEX FROM  Datasheet :
    displays detailed information about the index data table, comprising PRIMARY KEY (primary key).

    mysql> SHOW INDEX FROM runoob_tbl;
    +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | Table      | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
    +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    | runoob_tbl |          0 | PRIMARY  |            1 | runoob_id   | A         |           2 |     NULL | NULL   |      | BTREE      |         |               |
    +------------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
    1 row in set (0.00 sec)
  • SHOW TABLE STATUS LIKE [FROM db_name] 
    [LIKE 'pattern'] \ G: This command output performance and statistical information Mysql database management systems.

    mysql> SHOW TABLE STATUS FROM RUNOOB; # display database RUNOOB in all tables 
    
    mysql> SHOW TABLE STATUS from RUNOOB LIKE 'runoob%'; # table name starts runoob table information 
    mysql> SHOW TABLE STATUS from RUNOOB LIKE 'runoob % '\ G; # plus \ G, printing results by columns

Gif presentation:

Guess you like

Origin blog.csdn.net/zhangbijun1230/article/details/92376007