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';
username: Username.host: Specify which hosts the user can connect from. For example,localhostonly local connections are allowed,%connections from any host are allowed.password: The user's 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';
privileges: The required permission, such asALL PRIVILEGES、SELECT、INSERT、UPDATE、DELETE, etc.database_name.*: Indicates granting permissions on a database or table.database_name.*indicates granting permissions on all tables of an entire database,database_name.table_nameindicates granting permissions on a specified table.TO 'username'@'host': Specify the user and host to grant permissions to.
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
basedir: The basic installation directory of the MySQL server.datadir: The location where MySQL data files are stored.socket: The Unix socket file path of the MySQL server.pid-file: The file path that stores the process ID of the currently running MySQL server.port: The port number the MySQL server listens on; the default is 3306.
2. Server Options
bind-address: Specify the IP address the MySQL server listens on; it can be an IP address or hostname.server-id: In replication configuration, set a unique identifier for each MySQL server.default-storage-engine: The default storage engine, such as InnoDB or MyISAM.max_connections: The maximum number of connections the server can maintain simultaneously.thread_cache_size: The size of the thread cache, used to improve the startup speed of new connections.query_cache_size: The size of the query cache, used to improve the efficiency of identical queries.default-character-set: The default character set.collation-server: The server's default collation.
3. Performance Tuning
innodb_buffer_pool_size: The buffer pool size of the InnoDB storage engine; this is one of the most important parameters in InnoDB performance tuning.key_buffer_size: The key buffer size of the MyISAM storage engine.table_open_cache: The number of tables that can be open simultaneously in the cache.thread_concurrency: The number of threads allowed to run simultaneously.
4. Security Settings
skip-networking: Prevent the MySQL server from listening on network connections, allowing only local connections.skip-grant-tables: Start the MySQL server without a password; usually used to recover a forgotten root password, but this is a security risk.auth_native_password=1: Enable native password authentication for MySQL 5.7 and above.
5. Log Settings
log_error: The path of the error log file.general_log: A log that records all client connections and queries.slow_query_log: Record slow queries whose execution time exceeds a specific threshold.log_queries_not_using_indexes: Record queries that do not use indexes.
6. Replication Settings
master_hostandmaster_user: The address of the master server and the replication user.master_password: The password of the replication user.master_log_fileandmaster_log_pos: The log file and position used for replication.
Commands for Managing MySQL
The following lists common commands used when working with the MySQL database:
-
USE Database name :
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:
