Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

MySQL Management

MySQL Management

Starting and Stopping the MySQL Server

On Windows systems

Start the MySQL server:

1. **Through the "Services" management tool: ** Open the "Run" dialog (Win + R), enter services.msc, find the "MySQL" service, right-click and select "Start".

**2. Through the Command Prompt: ** Open the Command Prompt (as administrator) and enter the following command:

net start mysql

Stop the MySQL server:

1. **Through the "Services" management tool:** Also open the "Run" dialog, enter services.msc, find the "MySQL" service, right-click and select "Stop".

2、Through the Command Prompt: Open the Command Prompt (as administrator) and enter the following command:

net stop mysql

On Linux systems

1. Start the MySQL service:

Use the systemd command (applicable to most modern Linux distributions such as Ubuntu, CentOS, etc.):

sudo systemctl start mysql

Use the service command (in some older distributions):

sudo service mysql start

2. Stop the MySQL service:

Using systemd:



sudo systemctl stop mysql

Using the service command:



sudo service mysql stop

3. Restart the MySQL service:

Using systemd:



sudo systemctl restart mysql

Using the service command:



sudo service mysql restart

4. Check the MySQL service status:

Use the systemd command:



sudo systemctl status mysql

Using the service command:



sudo service mysql status

On Mac OS systems

Start the MySQL service:

Using the command line:



sudo /usr/local/mysql/support-files/mysql.server start

Stop the MySQL service:

Using the command line:



sudo /usr/local/mysql/support-files/mysql.server stop

Restart the MySQL service:

Using the command line:



sudo /usr/local/mysql/support-files/mysql.server restart

Check the MySQL service status:

Using the command line:



sudo /usr/local/mysql/support-files/mysql.server status

In the above commands, mysql may vary depending on the installation path or version.

In Mac OS, the MySQL installation path is usually /usr/local/mysql/, so starting and stopping the MySQL service requires the mysql.server script under this path.


MySQL User Setup

In MySQL, user setup includes operations such as creating users, setting permissions, and managing users. The following are some common MySQL user setup operations, including creating users, setting permissions, viewing and deleting users, etc.

Create users

To create a new user, you can use the following SQL command:



CREATE USER 'username'@'host' IDENTIFIED BY 'password';

Example

CREATE

USER

'john'

@

'localhost'

IDENTIFIED

BY

'password123'

;

Granting Permissions

After creating users, you need to grant them access permissions; use the GRANT command to grant permissions:



GRANT privileges ON database_name.* TO 'username'@'host';

Example

GRANT

ALL

PRIVILEGES

ON

test_db

.*

TO

'john'

@

'localhost'

;

Flush Privileges

After granting or revoking permissions, you need to flush privileges for the changes to take effect:



FLUSH PRIVILEGES;

View User Permissions

To view a specific user's permissions, you can use the following command:



SHOW GRANTS FOR 'username'@'host';

Example

SHOW

GRANTS

FOR

'john'

@

'localhost'

;

Revoke Permissions

To revoke a user's permissions, use the REVOKE command:



REVOKE privileges ON database_name.* FROM 'username'@'host';

Example

REVOKE

ALL

PRIVILEGES

ON

test_db

.*

FROM

'john'

@

'localhost'

;

Delete user

If you need to delete a user, you can use the following command:



DROP USER 'username'@'host';

Example

DROP

USER

'john'

@

'localhost'

;

Change a user's password

To change a user's password, you can use the ALTER USER command:

ALTER USER 'username'@'host' IDENTIFIED BY 'new_password';

Example

ALTER

USER

'john'

@

'localhost'

IDENTIFIED

BY

'newpassword456'

;

Change User Host

To change a user's host (i.e., which hosts are allowed to connect), you can first delete the user and then create a new one.

Example

-- Delete the old user

DROP

USER

'john'

@

'localhost'

;

-- Recreate the user and specify the new host

CREATE

USER

'john'

@

'%'

IDENTIFIED

BY

'password123'

;

Specify Permissions When Creating a User

When creating a user, you can also grant permissions at the same time (in MySQL 8.0.16 and later):

Example

CREATE

USER

'john'

@

'localhost'

IDENTIFIED

BY

'password123'

WITH

GRANT

OPTION

;

GRANT

ALL

PRIVILEGES

ON

test_db

.*

TO

'john'

@

'localhost'

;


/etc/my.cnf File Configuration

The /etc/my.cnf file is the MySQL configuration file, used to configure various parameters and options of the MySQL server.

In general, you do not need to modify this configuration file; its default configuration is 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 different directories for storing error log files; generally you do not need to change these settings.

The /etc/my.cnf file may differ across systems and MySQL versions, but generally contains the following sections:

1. Basic Settings

2. Server Options

3. Performance Tuning

4. Security Settings

5. Log Settings

6. Replication Settings


Commands for Managing MySQL

The following lists common commands used when working with the MySQL database:


Select the MySQL database to operate on; after using this command, all MySQL commands apply only to that database.

mysql> use RUNOOB;

Database changed

-

SHOW DATABASES:
List the databases in the MySQL database management system.

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 tables in the specified database; before using this command, you need to use the use command to select the database to operate on.

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 Data tables:
Display the attributes of a data table, attribute types, primary key information, whether it is NULL, default values, and other information.

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 Data tables:
Display detailed index information of a data table, including 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 [FROM db_name] [LIKE 'pattern'] \G:
This command outputs the performance and statistical information of the MySQL database management system.

mysql> SHOW TABLE STATUS  FROM RUNOOB;   # 显示数据库 RUNOOB 中所有表的信息

mysql> SHOW TABLE STATUS from RUNOOB LIKE 'runoob%'; # 表名以runoob开头的表的信息 mysql> SHOW TABLE STATUS from RUNOOB LIKE 'runoob%'\G; # 加上 \G,查询结果按列打印

GIF demonstration: