diff --git a/docs/en/docs/Administration/figures/en-us_image_0231563137.png b/docs/en/docs/Administration/figures/en-us_image_0231563137.png new file mode 100644 index 0000000000000000000000000000000000000000..e55ccfd8251c13bbf47f37f94d2111b0c50d26f4 Binary files /dev/null and b/docs/en/docs/Administration/figures/en-us_image_0231563137.png differ diff --git a/docs/en/docs/Administration/figures/en-us_image_0231563138.png b/docs/en/docs/Administration/figures/en-us_image_0231563138.png new file mode 100644 index 0000000000000000000000000000000000000000..ccf7734682d9cde4cf0ea4ef365143109abeadbf Binary files /dev/null and b/docs/en/docs/Administration/figures/en-us_image_0231563138.png differ diff --git a/docs/en/docs/Administration/setting-up-the-database-server.md b/docs/en/docs/Administration/setting-up-the-database-server.md index b84c1dc97a4923bc3d3b86bcb3a512304f5edada..12923097670be3c7e06651a981335aa115a93f61 100644 --- a/docs/en/docs/Administration/setting-up-the-database-server.md +++ b/docs/en/docs/Administration/setting-up-the-database-server.md @@ -19,6 +19,12 @@ - [Installing, Running, and Uninstalling MySQL](#installing-running-and-uninstalling-mysql) - [Managing Database Users](#managing-database-users-1) - [Managing Databases](#managing-databases-2) + - [GreatSQL Server](#greatsql-server) + - [Software Description](#software-description-3) + - [Configuring the Environment](#configuring-the-environment-3) + - [Installing, Running, and Uninstalling MySQL](#installing-running-and-uninstalling-mysql) + - [Managing Database Users](#managing-database-users-2) + - [Managing Databases](#managing-databases-3) @@ -1438,36 +1444,36 @@ The Structured Query Language \(SQL\) used by MySQL is the most common standard ### Configuring the Environment ->![](./public_sys-resources/icon-note.gif) **NOTE:** ->The following environment configuration is for reference only. Configure the environment based on the site requirements. +>! [] (./public_stys-resources/icon node. gif) * * Explanation:** +>The following environment configurations are only reference examples, and specific configurations will be made according to actual needs -#### Disabling the Firewall and Automatic Startup +####Turn off the firewall and cancel the startup auto start ->![](./public_sys-resources/icon-note.gif) **NOTE:** ->It is recommended that firewall be disabled in the test environment to prevent network impact. Configure the firewall based on actual requirements. +>! [] (./public_stys-resources/icon node. gif) * * Explanation:** +>In testing environments, firewalls are usually turned off to avoid the influence of some network factors, and configurations are made according to actual needs. -1. Stop the firewall service as the **root** user. +1. Stop the firewall with root privileges. - ```shell - systemctl stop firewalld - ``` +```shell +systemctl stop firewalld +``` -2. Disable the firewall service as the **root** user. +2. Turn off the firewall with root privileges. - ```shell - systemctl disable firewalld - ``` +```shell +systemctl disable firewalld +``` - >![](./public_sys-resources/icon-note.gif) **NOTE:** - >The automatic startup is automatically disabled as the firewall is disabled. +>! [] (./public_stys-resources/icon node. gif) * * Explanation:** +>At the same time as executing the disable command to turn off the firewall, it also cancels the startup self start. -#### Disabling SELinux +####Change SELINOX to disabled -1. Modify the configuration file as the **root** user. +1. Modify configuration files with root privileges. - ```shell - sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux - ``` +```shell +sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux +``` #### Creating a User Group and a User @@ -2171,3 +2177,695 @@ In the preceding information: ```shell mysql -h 192.168.202.144 -P 3306 -uroot -p123456 -t db1 < db1.sql ``` + +## GreatSQL Server + +## GreatSQL Server + +### Software Introduction + +GreatSQL is a donation project under the OpenAtom Foundation, certified by the China Academy of Information and Communications Technology (CAICT) as both a trusted open-source community and a trusted open-source project. + +GreatSQL is an **open-source and free** database that can meet financial-grade application scenarios on ordinary hardware. It features **high availability**, **high performance**, **high compatibility**, and **high security**, making it an ideal alternative to MySQL or Percona Server for MySQL. + +GreatSQL strives to maintaining the openness of open-source. It adopts the [GPLv2 license](https://gitee.com/GreatSQL/GreatSQL/blob/master/LICENSE). + +GreatSQL is primarily maintained and operated by the [GreatSQL Community](https://greatsql.cn). It has attracted numerous community maintainers, enthusiasts, and users who contribute in various ways, including code submissions, feature testing, bug discovery, and documentation improvements. + +### Configuring the Environment + +>! [] (./public_stys-resources/icon node. gif) * * Explanation:** +>The following environment configurations are only reference examples, and specific configurations will be made according to actual needs + +####Turn off the firewall and cancel the startup auto start + +>! [] (./public_stys-resources/icon node. gif) * * Explanation:** +>In testing environments, firewalls are usually turned off to avoid the influence of some network factors, and configurations are made according to actual needs. + +1. Stop the firewall with root privileges. + +```shell +systemctl stop firewalld +``` + +2. Turn off the firewall with root privileges. + +```shell +systemctl disable firewalld +``` + +>! [] (./public_stys-resources/icon node. gif) * * Explanation:** +>At the same time as executing the disable command to turn off the firewall, it also cancels the startup self start. + +####Change SELINOX to disabled + +1. Modify configuration files with root privileges. + +```shell +sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux +``` + +#### Creating a User Group and a User + +>![](./public_sys-resources/icon-note.gif) **NOTE:** +>In the server environment, independent users are assigned to each process to implement permission isolation for security purposes. The user group and user are created for the OS, not for the database. + +Create a MySQL user or user group as the **root** user. + + ```shell + groupadd mysql + ``` + + ```shell + useradd -r -g mysql -s /sbin/nologin mysql + ``` + +#### Creating Data Drives + +>![](./public_sys-resources/icon-note.gif) **NOTE:** +> +>- When testing the ultimate performance, you are advised to attach NVMe SSDs with better I/O performance to create GreatSQL test instances to avoid the impact of disk I/O on the performance test result. This section uses NVMe SSDs as an example. For details, see Step 1 to Step 4. +>- In a non-performance test, run the following command as the **root** user to create a data directory. Then skip this section. +> `mkdir /data` + +1. Create a file system \(xfs is used as an example as the **root** user. Create the file system based on the site requirements.\). If a file system has been created for a disk, an error will be reported when you run this command. You can use the **-f** parameter to forcibly create a file system. + + ```shell + mkfs.xfs /dev/nvme0n1 + ``` + +2. Create a data directory. + + ```shell + mkdir /data + ``` + +3. Mount disks. + + ```shell + mount -o noatime,nobarrier /dev/nvme0n1 /data + ``` + +#### Creating GreatSQL Data Directory + +1. In the created data directory **/data**, create directories for processes and grant permissions to the MySQL group or user created as the **root** user. + + ```shell + mkdir -p /data/GreatSQL + cd /data/GreatSQL + chown -R mysql:mysql /data/GreatSQL + ``` + +### Installing, Running, and Uninstalling GreatSQL + +#### Installing GreatSQL + +>! [] (./public_Sys-resources/icon usage. gif) * * Note:** +>Before installing GreatSQL, please check if MySQL/Percona Server For MySQL/MariaDB and other MySQL homomorphic databases have been installed first. If they have already been installed, they need to be uninstalled first, otherwise GreatSQL cannot be installed again, which may cause conflicts. + +1. Configure the local yum source. For details, see [Configuring the Repo Server](./configuring-the-repo-server.md). + +2. Clear the cache. + + ```shell + dnf clean all + ``` + +3. Create a cache. + + ```shell + dnf makecache + ``` + +4. Install the MySQL server as the **root** user. + + ```shell + dnf install greatsql-server + ``` + +If there is a conflict error during installation, you need to uninstall MySQL/Percona Server For MySQL/MariaDB and other MySQL homomorphic databases first. + +5. Check the installed RPM package. + + ```shell + rpm -qa | grep greatsql-server + ``` + +#### Running MySQL + + +1. Modify configuration file `/etc/my.cnf` + 1. Modify the/etc/my.cnf file with **root** user, including the software installation path basedir, datadir, listening port number, innobd_fuffer_pool_2, etc., according to the actual situation. + + ```shell + vi /etc/my.cnf + ``` + + Edit `/etc/my.cnf` as follows (the following is a example): + + ```text + [client] + datadir = /data/GreatSQL/mysql.sock + + [mysqld] + !includedir /etc/my.cnf.d + user = mysql + datadir = /data/GreatSQL + socket = /data/GreatSQL/mysql.sock + log-error = /data/GreatSQL/mysqld.log + pid-file = /data/GreatSQL/mysqld.pid + slow_query_log = ON + long_query_time = 0.01 + log_slow_verbosity = FULL + log_error_verbosity = 3 + innodb_buffer_pool_size = 1G + innodb_redo_log_capacity = 256M + innodb_io_capacity = 10000 + innodb_io_capacity_max = 20000 + innodb_flush_sync = OFF + ``` + +If you want GreatSQL to achieve better running status and performance, you can refer to the [my.cnf configuration file reference template](https://gitee.com/GreatSQL/GreatSQL-Doc/blob/master/docs/my.cnf-example) provided by the GreatSQL community. + + 2. Ensure that the my.cnf configuration file is modified correctly. + + ```shell + cat /etc/my.cnf + ``` + + ![](./figures/zh-cn_image_0231563137.png) + + 3. Start GreatSQL database with root user + + ```shell + systemctl start mysqld + ``` + + The initial startup may be slower and may take several minutes due to the task of initializing the GreatSQL database. + + After initialization is complete, check if there are already data files in the directory corresponding to the 'datadir' configuration parameter, as well as the corresponding log files `/data/GreatSQL/mysqld.log`. + + ```shell + ls -la /data/GreatSQL + ``` + + 查看`/data/GreatSQL/mysqld.log`日志文件,若其中包括“Bootstrapping complete”表示初始化数据库完成,且包含“A temporary password is generated for root@localhost: =dz;kqt8svrF”这样的信息,则其中的“=dz;kqt8svrF”为GreatSQL数据库root账户的初始密码,第一次登入后要立即修改才能继续使用GreatSQL。 + + Check the `/data/GreatSQL/mysqld.log` log file. If it includes the message "Bootstrapping complete," it indicates that the database initialization is finished. Additionally, if it contains a line such as "A temporary password is generated for root@localhost: =dz;kqt8svrF," the string "=dz;kqt8svrF" is the initial temporary password for the `root@localhost` account in the GreatSQL database. After logging in for the first time, you must change this password immediately to continue using GreatSQL. + + + 4. Login to GreatSQL。 + + >![](./public_sys-resources/icon-note.gif) **说明:** + >- When prompted to enter a password, please enter the temporary password generated in [step 4](#li15634560588). + + ```shell + mysql -uroot -p + ``` + + ![](./figures/zh-cn_image_0231563138.png) + + 5. Change the temporary password + + As shown in the screenshot above, after logging into the GreatSQL database for the first time, you must modify the temporary password before proceeding with further operations. + + ```shell + greatsql [(none)]> \s + ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. + greatsql [(none)]> ALTER USER USER() IDENTIFIED BY 'GreatSQL@202X'; + Query OK, 0 rows affected (0.00 sec) + ``` + + 6. Create a new database account named `GreatSQL` that allows access from any server, and grant this account full permissions to perform all operations on all data objects within the database named `test`. + + ```shell + -- Specify the password for the new account GreatSQL as "GreatSQL-202X". It is recommended to modify it to a password of your own + greatsql [(none)]> CREATE USER GreatSQL@'%' IDENTIFIED BY 'GreatSQL-202X'; + Query OK, 0 rows affected (0.00 sec) + + greatsql [(none)]> GRANT ALL ON test.* TO GreatSQL@'%'; + Query OK, 0 rows affected (0.00 sec) + ``` + + 7. Creating a new database named `test` + + ```shell + greatsql [(none)]> CREATE DATABASE test; + Query OK, 1 row affected (0.00 sec) + ``` + + 8. exit GreatSQL server + + Run SQL command `\q` or `exit` to exit GreatSQL database Server. + + ```shell + greatsql> exit + ``` + + 9. Switch to the newly created `GreatSQL` account to log in and operate the database. + + ```shell + -- Use the password "GreatSQL-202X" specified when creating the GreatSQL account, or use the password you have set yourself. + -- The test specified afterward means that the test database will be used immediately after logging in. Ensure to include a space; otherwise, it will be interpreted as part of the password. + mysql -uGreatSQL -p test + Enter password: + ... + + -- Creating new table + greatsql [test]> CREATE TABLE t1(id INT PRIMARY KEY); + Query OK, 0 rows affected (1.48 sec) + + -- show tables + greatsql [test]> SHOW TABLES; + +----------------+ + | Tables_in_test | + +----------------+ + | t1 | + +----------------+ + 1 row in set (0.00 sec) + + -- inserting some rows + greatsql [test]> INSERT INTO t1 SELECT RAND()*1024; + Query OK, 1 row affected (0.34 sec) + Records: 1 Duplicates: 0 Warnings: 0 + + -- reading rows from table + greatsql [test]> SELECT * FROM t1; + +-----+ + | id | + +-----+ + | 565 | + +-----+ + 1 row in set (0.00 sec) + ``` + + This completes the basic operations for the GreatSQL database. + + For more related SQL commands/syntax, please refer to the manual: [SQL Statements](https://dev.mysql.com/doc/refman/8.0/en/sql-statements.html). + + For more content on application development based on GreatSQL, please refer to: [Application Development](https://greatsql.cn/docs/12-dev-guide/12-dev-guide.html). + +#### Uninstalling GreatSQL + +1. Stop the database process as the **root** user. + + ```shell + systemctl stop mysqld + ``` + +2. Execute the command **dnf remove -y greatsql-server** with root privileges to uninstall GreatSQL. + + ```shell + dnf remove -y greasql-server + ``` + +### Managing Database Users + +#### Creating Users + +Run the **CREATE USER** statement to create one or more users and set corresponding passwords. + +```sql +CREATE USER 'username'@'hostname' IDENTIFIED BY 'password'; +``` + +In the preceding information: + +- _username_: name of a user. +- _host_: hostname, that is, the name of the host where the user connects to the database. As a local user, you can set the parameter to **localhost**. If the host name is not specified during user creation, the host name is **%** by default, indicating a group of hosts. +- _password_: password for logging in to the server. The password can be null. If the password is null, the user can log in to the server without entering the password. This method, however, is not recommended because it provides low security. + +To use the **CREATE USER** statement, you must have the **INSERT** permission on the database or the global **CREATE USER** permission. + +After a user account is created using the **CREATE USER** statement, a record is added to the user table in the database. If the account to be created exists, an error will occur during statement execution. + +A new user has few permissions and can perform only operations that do not require permissions. For example, a user can run the **SHOW** statement to query the list of all storage engines and character sets. + +##### Example + + Create a local user whose password is "GreatSQL@202X-local" and username is "GreatSQL-local". + +```sql +CREATE USER GreatSQL_local@localhost IDENTIFIED BY 'GreatSQL@202X'; +``` + + Create a user whose password is "GreatSQL@202X-remote", username is **GreatSQL_remote**, and hostname is **192.168.1.100**. + +```sql +CREATE USER 'GreatSQL_remote'@'192.168.1.100' IDENTIFIED BY 'GreatSQL@202X-remote'; +``` + +#### Viewing Users + +Run the **SHOW GRANTS** or **SELECT** statement to view one or more users. + +View a specific user: + +```sql +SHOW GRANTS [FOR 'username'@'hostname']; +``` + +```sql +SELECT USER,HOST,PASSWORD FROM mysql.user WHERE USER='username'; +``` + +View all users: + +```sql +SELECT USER,HOST FROM mysql.user; +``` + +In the preceding information: + +- _username_: name of a user. +- _hostname_: host name. + +##### Example + + View the user **userexample1**. + +```sql +SHOW GRANTS FOR GreatSQL; ++----------------------------------------------------+ +| Grants for GreatSQL@% | ++----------------------------------------------------+ +| GRANT USAGE ON *.* TO `GreatSQL`@`%` | +| GRANT ALL PRIVILEGES ON `test`.* TO `GreatSQL`@`%` | ++----------------------------------------------------+ +2 rows in set (0.00 sec) +``` + + View all users in the MySQL database. + +```sql +SELECT USER,HOST FROM mysql.user; +``` + +#### Modifying Users + +##### Modifying a Username + +Run the **RENAME USER** statement to change one or more existing usernames. + +```sql +RENAME USER 'oldusername'@'hostname' TO 'newusername'@'hostname'; +``` + +In the preceding information: + +- _oldusername_: original username. +- _newusername_: new username. +- _hostname_: host name. + +The **RENAME USER** statement is used to rename an existing account. If the original account does not exist in the system or the new account exists, an error will occur when the statement is executed. + +To use the **RENAME USER** statement, you must have the **UPDATE** permission on the database or the global **CREATE USER** permission. + +##### Example of Modifying a User + + Change the username **userexample1** to **userexample2** and change the hostname to **localhost**. + +```sql +RENAME USER 'userexample1'@'localhost' TO 'userexample2'@'localhost'; +``` + +##### Modifying a User Password + +Use the **SET PASSWORD** statement to modify the login password of a user. + +```sql +SET PASSWORD FOR 'username'@'hostname' = 'newpassword'; +``` + +In the preceding information: + +- **FOR'**_username_**'@'**_hostname_**'**: specifies the username and hostname whose password is to be changed. This parameter is optional. +- _newpassword_: new password. + +If the **FOR** clause is not added to the **SET PASSWORD** statement, the password of the current user is changed. + +The **FOR** clause must be given in the format of **'**_username_**'@'**_hostname_**'**, where _username_ indicates the username of the account and _hostname_ indicates the hostname of the account. + +The account whose password is to be changed must exist in the system. Otherwise, an error occurs when the statement is executed. + +##### Example of Changing a User Password + + Change the password of user **GreatSQL_remote** whose hostname is **localhost** to "GreatSQL@@202X-remote". + +```sql +SET PASSWORD FOR 'GreatSQL_remote'@'192.168.1.100' = 'GreatSQL@@202X-remote'; +``` + +#### Deleting Users + +Use the **DROP USER** statement to delete one or more user accounts and related permissions. + +```sql +DROP USER 'username1'@'hostname1' [,'username2'@'hostname2']...; +``` + +>![](./public_sys-resources/icon-caution.gif) **CAUTION:** +>The deletion of users does not affect the tables, indexes, or other database objects that they have created, because the database does not record the accounts that have created these objects. + +The **DROP USER** statement can be used to delete one or more database accounts and their original permissions. + +To use the **DROP USER** statement, you must have the **DELETE** permission on the database or the global **CREATE USER** permission. + +In the **DROP USER** statement, if the hostname of an account is not specified, the hostname is **%** by default. + +##### Example + + Delete the local user **userexample**. + +```sql +DROP USER 'userexample'@'localhost'; +``` + +#### Granting Permissions to a User + +Run the **GRANT** statement to grant permissions to a new user. + +```sql +GRANT privileges ON databasename.tablename TO 'username'@'hostname'; +``` + +In the preceding information: + +- **ON** clause: specifies the object and level on which the permission is granted. +- _privileges_: indicates the operation permissions of a user, such as **SELECT**, INSERT, and **UPDATE**. To grant all permissions to a user, use **ALL**. +- _databasename_: database name. +- _tablename_: table name. +- **TO** clause: sets the user password and specifies the user to whom the permission is granted. +- _username_: name of a user. +- _hostname_: host name. + +To grant the user the permission to operate all databases and tables, use asterisks \(\*\), for example, **\*.\***. + +If you specify a password for an existing user in the **TO** clause, the new password will overwrite the original password. + +If the permission is granted to a non-existent user, a **CREATE USER** statement is automatically executed to create the user, but the password must be specified for the user. + +##### Example + + Grant the **SELECT** and **INSERT** permissions to local user **userexample**. + +```sql +GRANT SELECT,INSERT ON *.* TO 'userexample'@'localhost'; +``` + +#### Deleting User Permissions + +Run the **REVOKE** statement to delete the permissions of a user, but the user will not be deleted. + +```sql +REVOKE privilege ON databasename.tablename FROM 'username'@'hostname'; +``` + +The parameters in the **REVOKE** statement are the same as those in the **GRANT** statement. + +To use the **REVOKE** statement, you must have the global **CREATE USER** or **UPDATE** permission for the database. + +##### Example + + Delete the **INSERT** permission of local user **userexample**. + +```sql +REVOKE INSERT ON *.* FROM 'userexample'@'localhost'; +``` + +### Managing Databases + +#### Creating a Database + +Run the **CREATE DATABASE** statement to create a database. + +```sql +CREATE DATABASE databasename; +``` + +In the preceding command, _databasename_ can be replaced with the database name, which is case insensitive. + +##### Example + + Create a database named **databaseexample**. + +```sql +CREATE DATABASE databaseexample; +``` + +#### Viewing a Database + +Run the **SHOW DATABASES** statement to view a database. + +```sql +SHOW DATABASES; +``` + +##### Example + + View all databases. + +```sql +SHOW DATABASES; +``` + +#### Selecting a Database + +Generally, you need to select a target database before creating or querying a table. Use the **USE** statement to select a database. + +```sql +USE databasename; +``` + +In the preceding command, _databasename_ indicates the database name. + +##### Example + + Select the **databaseexample** database. + +```sql +USE databaseexample; +``` + +#### Deleting a Database + +Run the **DROP DATABASE** statement to delete a database. + +>![](./public_sys-resources/icon-caution.gif) **CAUTION:** +>Exercise caution when deleting a database. Once a database is deleted, all tables and data in the database will be deleted. + +```sql +DROP DATABASE databasename; +``` + +In the preceding command, _databasename_ indicates the database name. + +The **DROP DATABASE** command is used to delete an existing database. After this command is executed, all tables in the database are deleted, but the user permissions of the database are not automatically deleted. + +To use **DROP DATABASE**, you need the **DROP** permission on the database. + +**DROP SCHEMA** is a synonym of **DROP DATABASE**. + +##### Example + + Delete the **databaseexample** database. + +```sql +DROP DATABASE databaseexample; +``` + +#### Backing Up a Database + +Run the **mysqldump** command as the **root** user to back up the database. + +Back up one or more tables: + +```shell +mysqldump [options] databasename [tablename ...] > outfile +``` + +Back up one or more databases: + +```shell +mysqldump [options] -databases databasename ... > outfile +``` + +Back up all databases: + +```shell +mysqldump [options] -all-databases > outputfile +``` + +In the preceding information: + +- _databasename_: database name. +- _tablename_: name of a data table. +- _outfile_: database backup file. +- _options_: parameter option of the **mysqldump** command. Multiple parameters can be separated by spaces. The common parameters of the **mysqldump** command are as follows: + - **-u, \-\-user**= _username_: specifies the username. + - **-p, \-\-password**\[= _password_\]: specifies the password. In the following examples, the password is specified as "123456". You need to adjust it according to your actual situation. + - **-P, \-\-port**= _portnumber_: specifies the port number. + - **-h, \-\-host**= _hostname_: specifies the hostname. + - **-r, \-\-result-file**= _filename_: saves the export result to a specified file, which is equivalent to **\>**. + - **-t**: backs up data only. + - **-d**: backs up the table structure only. + +##### Example + + Back up all the databases of user **root** on port **3306** of the host whose IP address is **192.168.202.144** to the **alldb.sql** file. + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 --all-databases > alldb.sql +``` + + Back up the db1 database of user **root** on port **3306** of the host whose IP address is **192.168.202.144** to the **db1.sql** file. + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 --databases db1 > db1.sql +``` + + Back up the tb1 table of the db1 database of user **root** on port **3306** of the host whose IP address is **192.168.202.144** to the **db1tb1.sql** file. + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 db1 tb1 > db1tb1.sql +``` + + Back up only the table structure of the db1 database of user **root** on port **3306** of the host whose IP address is **192.168.202.144** to the **db1.sql** file. + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 -d db1 > db1.sql +``` + + Back up only the table structure of the db1 database of user **root** on port **3306** of the host whose IP address is **192.168.202.144** to the **db1.sql** file. + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 -t db1 > db1.sql +``` + +#### Restoring a Database + +Run the **mysql** command as the **root** user to restore the database. + +Restore one or more tables: + +```shell +mysql -h hostname -P portnumber -u username -ppassword databasename < infile +``` + +In the preceding information: + +- _hostname_: host name. +- _portnumber_: port number. +- _username_: name of a user. +- _password_: password. +- _databasename_: database name. +- _infile_: **outfile** parameter in the **mysqldump** command. + +##### Example + + Restore a database. + +```shell +mysql -h 192.168.202.144 -P 3306 -uroot -p123456 -t db1 < db1.sql +``` + diff --git a/docs/zh/docs/Administration/figures/zh-cn_image_0231563137.png b/docs/zh/docs/Administration/figures/zh-cn_image_0231563137.png new file mode 100644 index 0000000000000000000000000000000000000000..e55ccfd8251c13bbf47f37f94d2111b0c50d26f4 Binary files /dev/null and b/docs/zh/docs/Administration/figures/zh-cn_image_0231563137.png differ diff --git a/docs/zh/docs/Administration/figures/zh-cn_image_0231563138.png b/docs/zh/docs/Administration/figures/zh-cn_image_0231563138.png new file mode 100644 index 0000000000000000000000000000000000000000..ccf7734682d9cde4cf0ea4ef365143109abeadbf Binary files /dev/null and b/docs/zh/docs/Administration/figures/zh-cn_image_0231563138.png differ diff --git "a/docs/zh/docs/Administration/\346\220\255\345\273\272\346\225\260\346\215\256\345\272\223\346\234\215\345\212\241\345\231\250.md" "b/docs/zh/docs/Administration/\346\220\255\345\273\272\346\225\260\346\215\256\345\272\223\346\234\215\345\212\241\345\231\250.md" index 541c6bc31dfbdad0f50410d89a6d7dbe935523a0..efd4f8e5a395283037f4636e2299e7543f6a6eac 100644 --- "a/docs/zh/docs/Administration/\346\220\255\345\273\272\346\225\260\346\215\256\345\272\223\346\234\215\345\212\241\345\231\250.md" +++ "b/docs/zh/docs/Administration/\346\220\255\345\273\272\346\225\260\346\215\256\345\272\223\346\234\215\345\212\241\345\231\250.md" @@ -2,13 +2,13 @@ - [搭建数据库服务器](#搭建数据库服务器) - - [PostgreSql服务器](#postgresql服务器) + - [PostgreSQL服务器](#postgresql服务器) - [软件介绍](#软件介绍) - [配置环境](#配置环境) - [安装、运行和卸载](#安装运行和卸载) - [管理数据库角色](#管理数据库角色) - [管理数据库](#管理数据库) - - [Mariadb服务器](#mariadb服务器) + - [MariaDB服务器](#mariadb服务器) - [软件介绍](#软件介绍-1) - [配置环境](#配置环境-1) - [安装、运行和卸载](#安装运行和卸载-1) @@ -20,19 +20,25 @@ - [安装、运行和卸载](#安装运行和卸载-2) - [管理数据库用户](#管理数据库用户-1) - [管理数据库](#管理数据库-2) + - [GreatSQL服务器](#greatsql服务器) + - [软件介绍](#软件介绍-3) + - [配置环境](#配置环境-3) + - [安装、运行和卸载](#安装运行和卸载-3) + - [管理数据库用户](#管理数据库用户-2) + - [管理数据库](#管理数据库-3) -## PostgreSql服务器 +## PostgreSQL服务器 ### 软件介绍 PostgreSQL的架构如[图1](#fig26022387391)所示,主要进程说明如[表1](#table62020913417)所示。 -**图 1** PostgreSql架构 +**图 1** PostgreSQL架构 ![](./figures/PostgreSql_architecture.png) -**表 1** PostgreSql中的主要进程说明 +**表 1** PostgreSQL中的主要进程说明 -

进程类别

@@ -54,7 +60,7 @@ PostgreSQL的架构如[图1](#fig26022387391)所示,主要进程说明如[表1

Postgres(常驻进程)

管理后端的常驻进程,也称为“postmaster”。其默认侦听UNIXDomain Socket和TCP/IP(Windows等,一部分的平台只侦听TCP/IP)的5432端口,等待来自前端的连接处理。侦听的端口号可以在PostgreSql的设置文件postgresql.conf中修改。

+

管理后端的常驻进程,也称为“postmaster”。其默认侦听UNIXDomain Socket和TCP/IP(Windows等,一部分的平台只侦听TCP/IP)的5432端口,等待来自前端的连接处理。侦听的端口号可以在PostgreSQL的设置文件postgresql.conf中修改。

子进程

@@ -777,7 +783,7 @@ psql命令不会自动创建databasename数据库,所以在执行psql恢复数 [postgres@localhost ~]# psql -h 192.168.202.144 -p 3306 -U postgres -W -d newdb < db1.sql ``` -## Mariadb服务器 +## MariaDB服务器 ### 软件介绍 @@ -788,9 +794,9 @@ MariaDB的架构如[图2](#fig13492418164520)所示。 **图 2** MariaDB逻辑架构 ![](./figures/logical_architectureofMariaDB.png) -当Mariadb接受到Sql语句时,其详细的执行过程如下: +当MariaDB接受到SQL语句时,其详细的执行过程如下: -1. 当客户端连接到mariadb的时候,会认证客户端的主机名、用户、密码,认证功能可以做成插件。 +1. 当客户端连接到MariaDB的时候,会认证客户端的主机名、用户、密码,认证功能可以做成插件。 2. 如果登录成功,客户端发送sql命令到服务端。由解析器解析sql语句。 3. 服务端检查客户端是否有权限去获取它想要的资源。 4. 如果查询已经存储在query cache当中,那么结果立即返回。 @@ -977,7 +983,7 @@ MariaDB的架构如[图2](#fig13492418164520)所示。 dnf makecache ``` -4. 在root权限下安装mariadb服务器。 +4. 在root权限下安装MariaDB服务器。 ```shell dnf install mariadb-server @@ -991,7 +997,7 @@ MariaDB的架构如[图2](#fig13492418164520)所示。 #### 运行 -1. 在root权限下开启mariadb服务器。 +1. 在root权限下开启MariaDB服务器。 ```shell systemctl start mariadb @@ -1025,7 +1031,7 @@ MariaDB的架构如[图2](#fig13492418164520)所示。 kill -9 进程ID ``` -2. 在root权限下执行**dnf remove mariadb-server**命令卸载mariadb。 +2. 在root权限下执行**dnf remove mariadb-server**命令卸载MariaDB。 ```shell dnf remove mariadb-server @@ -2157,3 +2163,685 @@ mysql -h hostname -P portnumber -u username -ppassword databasename < infile ```shell # mysql -h 192.168.202.144 -P 3306 -uroot -p123456 -t db1 < db1.sql ``` + + +## GreatSQL服务器 + +### 软件介绍 + +GreatSQL 数据库是开放原子开源基金会旗下捐赠项目,拥有中国信通院可信开源社区+可信开源项目双认证。 + +GreatSQL 数据库是一款 **开源免费** 数据库,可在普通硬件上满足金融级应用场景,具有 **高可用**、**高性能**、**高兼容**、**高安全** 等特性,可作为 MySQL 或 Percona Server for MySQL 的理想可选替换。 + +GreatSQL 致力于保持开源的开放性。GreatSQL 采用 [GPLv2 协议](https://gitee.com/GreatSQL/GreatSQL/blob/master/LICENSE)。 + +GreatSQL 数据库由 [GreatSQL 社区](https://greatsql.cn) 主导负责运营维护,已有[众多社区维护者、爱好者及用户](https://greatsql.cn/docs/community/6-thanks.html)参与,包括提交代码、功能测试验证、发现 Bug 以及文档改进等多方面贡献。 + +### 配置环境 + +>![](./public_sys-resources/icon-note.gif) **说明:** +>以下环境配置仅为参考示例,具体配置视实际需求做配置 + +#### 关闭防火墙并取消开机自启动 + +>![](./public_sys-resources/icon-note.gif) **说明:** +>测试环境下通常会关闭防火墙以避免部分网络因素影响,视实际需求做配置。 + +1. 在root权限下停止防火墙。 + + ```shell + systemctl stop firewalld + ``` + +2. 在root权限下关闭防火墙。 + + ```shell + systemctl disable firewalld + ``` + + >![](./public_sys-resources/icon-note.gif) **说明:** + >执行disable命令关闭防火墙的同时,也取消了开机自启动。 + +#### 修改SELINUX为disabled + +1. 在root权限下修改配置文件。 + + ```shell + sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/sysconfig/selinux + ``` + +#### 创建组和用户 + +>![](./public_sys-resources/icon-note.gif) **说明:** +>服务器环境下,为了系统安全,通常会为进程分配单独的用户,以实现权限隔离。本章节创建的组和用户都是操作系统层面的,不是数据库层面的。 + +在root权限下创建MySQL用户(组)。 + + ```shell + groupadd mysql + ``` + + ```shell + useradd -r -g mysql -s /sbin/nologin mysql + ``` + +#### 搭建数据盘 + +>![](./public_sys-resources/icon-note.gif) **说明:** +>- 测试极限性能时,建议单独挂载IO性能更优的NVME SSD存储介质创建GreatSQL测试实例,避免磁盘IO对性能测试结果的影响,本文以单独挂载NVME SSD为例,参考步骤1\~步骤4。 +>- 非性能测试时,在root权限下执行以下命令,创建数据目录即可。然后跳过本小节: +> mkdir /data + +1. 在root权限下创建文件系统(以xfs为例,根据实际需求创建文件系统),若磁盘之前已做过文件系统,执行此命令会出现报错,可使用-f参数强制创建文件系统。 + + ```shell + mkfs.xfs /dev/nvme0n1 + ``` + +2. 在root权限下创建数据目录。 + + ```shell + mkdir /data + ``` + +3. 在root权限下挂载磁盘。 + + ```shell + mount -o noatime,nobarrier /dev/nvme0n1 /data + ``` + +#### 创建数据库目录 + +1. 在已创建的数据目录 **/data** 基础上,使用root权限继续创建进程所需的相关目录并授权MySQL用户(组)。 + + ```shell + mkdir -p /data/GreatSQL + cd /data/GreatSQL + chown -R mysql:mysql /data/GreatSQL + ``` + +### 安装、运行和卸载 + +#### 安装 + +>![](./public_sys-resources/icon-caution.gif) **注意:** +> 安装GreatSQL前请先检查是否已先安装了MySQL/Percona Server For MySQL/MariaDB等这些MySQL同态数据库,如果已经安装需要先卸载,否则无法再安装GreatSQL,会产生冲突。 + +1. 配置本地yum源,详细信息请参考[搭建repo服务器](./搭建repo服务器.html)章节。 + +2. 清除缓存。 + + ```shell + dnf clean all + ``` + +3. 创建缓存。 + + ```shell + dnf makecache + ``` + +4. 在root权限下安装MySQL服务器。 + + ```shell + dnf install greatsql-server + ``` +如果安装时遇到冲突报错,需要先卸载MySQL/Percona Server For MySQL/MariaDB等MySQL同态数据库。 + +5. 查看安装后的rpm包。 + + ```shell + rpm -qa | grep greatsql-server + ``` + +#### 运行 + +1. 修改配置文件。 + 1. 在root权限下修改/etc/my.cnf文件,包括软件安装路径basedir、数据路径datadir、监听端口号port、内存缓冲区innodb_buffer_pool_size等根据实际情况修改。 + + ```shell + vi /etc/my.cnf + ``` + + 编辑/etc/my.cnf内容如下(下面是一个最基本参考): + + ```text + [client] + datadir = /data/GreatSQL/mysql.sock + + [mysqld] + !includedir /etc/my.cnf.d + user = mysql + datadir = /data/GreatSQL + socket = /data/GreatSQL/mysql.sock + log-error = /data/GreatSQL/mysqld.log + pid-file = /data/GreatSQL/mysqld.pid + slow_query_log = ON + long_query_time = 0.01 + log_slow_verbosity = FULL + log_error_verbosity = 3 + innodb_buffer_pool_size = 1G + innodb_redo_log_capacity = 256M + innodb_io_capacity = 10000 + innodb_io_capacity_max = 20000 + innodb_flush_sync = OFF + ``` + +如果想要让GreatSQL获得更好的运行状态和性能,可以参考GreatSQL社区提供的[my.cnf配置文件参考模板](https://gitee.com/GreatSQL/GreatSQL-Doc/blob/master/docs/my.cnf-example)。 + + 2. 确保my.cnf配置文件修改正确。 + + ```shell + cat /etc/my.cnf + ``` + + ![](./figures/zh-cn_image_0231563137.png) + + 3. 在root权限下启动GreatSQL数据库。 + + ```shell + systemctl start mysqld + ``` + + 首次启动会比较慢,可能需要数分钟,因为包含初始化GreatSQL数据库的工作。 + + 待初始化完成后,查看`datadir`配置参数对应的目录下是否已有数据文件,以及相应的日志文件`/data/GreatSQL/mysqld.log`。 + + ```shell + ls -la /data/GreatSQL + ``` + + 查看`/data/GreatSQL/mysqld.log`日志文件,若其中包括“Bootstrapping complete”表示初始化数据库完成,且包含“A temporary password is generated for root@localhost: =dz;kqt8svrF”这样的信息,则其中的“=dz;kqt8svrF”为GreatSQL数据库root账户的初始密码,第一次登入后要立即修改才能继续使用GreatSQL。 + + + 4. 登录数据库。 + + >![](./public_sys-resources/icon-note.gif) **说明:** + >- 提示输入密码时,请输入[步骤4](#li15634560588)产生的初始密码。 + + ```shell + mysql -uroot -p + ``` + + ![](./figures/zh-cn_image_0231563138.png) + + 5. 修改初始化密码 + + 如上面截图所示,首次登录GreatSQL数据库后,需要先修改临时初始密码才能继续后面的操作。 + + + ```shell + greatsql [(none)]> \s + ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement. + greatsql [(none)]> ALTER USER USER() IDENTIFIED BY 'GreatSQL@202X'; + Query OK, 0 rows affected (0.00 sec) + ``` + + 6. 创建一个允许从任何服务器访问的数据库账户GreatSQL,并授权允许该账户对名为test的数据库下所有数据对象的所有操作权限 + + ```shell + -- 在这里指定新账户GreatSQL的密码为"GreatSQL-202X",建议修改成您自己设定成其他密码 + greatsql [(none)]> CREATE USER GreatSQL@'%' IDENTIFIED BY 'GreatSQL-202X'; + Query OK, 0 rows affected (0.00 sec) + + greatsql [(none)]> GRANT ALL ON test.* TO GreatSQL@'%'; + Query OK, 0 rows affected (0.00 sec) + ``` + + 7. 创建名为test的新数据库 + + ```shell + greatsql [(none)]> CREATE DATABASE test; + Query OK, 1 row affected (0.00 sec) + ``` + + 8. 退出数据库。 + + 执行 `\q` 或者 `exit` 退出数据库。 + + ```shell + greatsql> exit + ``` + + 9. 切换新创建的GreatSQL账户登录,并操作数据库 + + ```shell + -- 这里使用上面创建GreatSQL新账户时指定的密码 "GreatSQL-202X",或者修改成您自己设定的密码 + -- 后面指定test是指登录后立即使用test数据库,要带空格,否则会被认为是密码 + mysql -uGreatSQL -p test + Enter password: + ... + + -- 创建新表 + greatsql [test]> CREATE TABLE t1(id INT PRIMARY KEY); + Query OK, 0 rows affected (1.48 sec) + + -- 查看都有哪些数据表 + greatsql [test]> SHOW TABLES; + +----------------+ + | Tables_in_test | + +----------------+ + | t1 | + +----------------+ + 1 row in set (0.00 sec) + + -- 写入测试数据 + greatsql [test]> INSERT INTO t1 SELECT RAND()*1024; + Query OK, 1 row affected (0.34 sec) + Records: 1 Duplicates: 0 Warnings: 0 + + -- 查询数据 + greatsql [test]> SELECT * FROM t1; + +-----+ + | id | + +-----+ + | 565 | + +-----+ + 1 row in set (0.00 sec) + ``` + + 这就完成了对GreatSQL数据库的基本操作。 + + 更多相关SQL命令/语法详见手册:[SQL Statements](https://dev.mysql.com/doc/refman/8.0/en/sql-statements.html)。 + + 更多基于 GreatSQL 的应用开发内容请参考:[应用开发](https://greatsql.cn/docs/12-dev-guide/12-dev-guide.html)。 + +#### 卸载GreatSQL + +1. 在root权限下关闭数据库进程。 + + ```shell + systemctl stop mysqld + ``` + +2. 在root权限下执行**dnf remove -y greatsql-server**命令卸载GreatSQL。 + + ```shell + dnf remove -y greasql-server + ``` + +### 管理数据库用户 + +#### 创建用户 + +可以使用CREATE USER语句来创建一个或多个用户,并设置相应的口令。 + +```shell +CREATE USER 'username'@'hostname' IDENTIFIED BY 'password'; +``` + +其中: + +- username:用户名。 +- host:主机名,即用户连接数据库时所在的主机的名字。若是本地用户可用localhost,若在创建的过程中,未指定主机名,则主机名默认为“%”,表示一组主机。 +- password:用户的登录密码,密码可以为空,如果为空则该用户可以不需要密码登录服务器,但从安全的角度而言,不推荐这种做法。 + +使用CREATE USER语句必须拥有数据库的INSERT权限或全局CREATE USER权限。 + +使用CREATE USER语句创建一个用户帐号后,会在系统自身的数据库的user表中添加一条新记录。若创建的帐户已经存在,则语句执行时会出现错误。 + +新创建的用户拥有的权限很少,只允许进行不需要权限的操作,如使用SHOW语句查询所有存储引擎和字符集的列表等。 + +##### 示例 + +创建密码为"GreatSQL@202X-local",用户名为**GreatSQL_local**的本地用户。 + +```shell +CREATE USER GreatSQL_local@localhost IDENTIFIED BY 'GreatSQL@202X'; +``` + +创建密码为"GreatSQL@202X-remote",用户名为**GreatSQL_remote**,主机名为**192.168.1.100**的用户。 + +```shell +CREATE USER 'GreatSQL_remote'@'192.168.1.100' IDENTIFIED BY 'GreatSQL@202X-remote'; +``` + +#### 查看用户 + +可以使用SHOW GRANTS语句或SELECT语句查看一个或多个用户。 + +查看特定用户: + +```shell +SHOW GRANTS [FOR 'username'@'hostname']; +``` + +```shell +SELECT USER,HOST,PASSWORD FROM mysql.user WHERE USER='username'; +``` + +查看所有用户: + +```shell +SELECT USER,HOST FROM mysql.user; +``` + +其中: + +- username:用户名。 +- hostname:主机名。 + +##### 示例 + +查看userexample1用户。 + +```sql +SHOW GRANTS FOR GreatSQL; ++----------------------------------------------------+ +| Grants for GreatSQL@% | ++----------------------------------------------------+ +| GRANT USAGE ON *.* TO `GreatSQL`@`%` | +| GRANT ALL PRIVILEGES ON `test`.* TO `GreatSQL`@`%` | ++----------------------------------------------------+ +2 rows in set (0.00 sec) +``` + +#### 修改用户 + +##### 修改用户名 + +可以使用RENAME USER语句修改一个或多个已经存在的用户名。 + +```shell +RENAME USER 'oldusername'@'hostname' TO 'newusername'@'hostname'; +``` + +其中: + +- oldusername:旧的用户名。 +- newusername:新的用户名。 +- hostname:主机名。 + +RENAME USER语句用于对原有的帐号进行重命名。若系统中旧帐号不存在或者新帐号已存在,则该语句执行时会出现错误。 + +使用RENAME USER语句,必须拥有数据库的UPDATE权限或全局CREATE USER权限。 + +##### 修改用户示例 + +将用户名userexample1修改为userexapme2,主机名为locahost。 + +```shell +> RENAME USER 'userexample1'@'localhost' TO 'userexample2'@'localhost'; +``` + +##### 修改用户密码 + +可以使用SET PASSWORD语句修改一个用户的登录密码。 + +```shell +SET PASSWORD FOR 'username'@'hostname' = 'newpassword'; +``` + +其中: + +- FOR 'username'@'hostname':FOR字句,可选项,指定欲修改密码的用户名及主机名。 +- 'newpassword':新密码。 + +在SET PASSWORD语句中,若不加上FOR子句,表示修改当前用户的密码。 + +FOR字句中必须以'username'@'hostname'的格式给定,username为帐户的用户名,hostname为帐户的主机名。 + +欲修改密码的帐号必须在系统中存在,否则语句执行时会出现错误。 + +**建议**:更推荐使用`ALTER USER`命令来修改数据库中的账号密码。 + +##### 修改用户密码示例 + +将用户名为GreatSQL_remote的密码修改为"GreatSQL@@202X-remote"。 + +```shell +SET PASSWORD FOR 'GreatSQL_remote'@'192.168.1.100' = 'GreatSQL@@202X-remote'; +``` + +#### 删除用户 + +可以使用DROP USER语句来删除一个或多个用户帐号以及相关的权限。 + +```shell +DROP USER 'username1'@'hostname1' [,'username2'@'hostname2']…; +``` + +>![](./public_sys-resources/icon-caution.gif) **注意:** +>用户的删除不会影响他们之前所创建的表、索引或其他数据库对象,因为数据库并不会记录创建了这些对象的帐号。 + +DROP USER语句可用于删除一个或多个数据库帐号,并删除其原有权限。 + +使用DROP USER语句必须拥有数据库的DELETE权限或全局CREATE USER权限。 + +在DROP USER语句的使用中,若没有明确地给出帐号的主机名,则该主机名默认为“%”。 + +##### 示例 + +删除用户名为userexample的本地用户。 + +```shell +DROP USER 'userexample'@'localhost'; +``` + +#### 用户授权 + +可以使用GRANT语句来对新建用户的授权。 + +```shell +GRANT privileges ON databasename.tablename TO 'username'@'hostname'; +``` + +其中: + +- ON字句:用于指定权限授予的对象和级别。 +- privileges:用户的操作权限,如SELECT,INSERT,UPDATE等,如果要授予所有的权限则使用ALL。 +- databasename:数据库名。 +- tablename:表名。 +- TO字句:用来设定用户密码,以及指定被赋予权限的用户。 +- username:用户名。 +- hostname:主机名。 + +如果要授予该用户对所有数据库和表的相应操作权限则可用\*表示,如\*.\*。 + +如果在TO子句中给系统中存在的用户指定密码,则新密码会将原密码覆盖。 + +如果权限被授予给一个不存在的用户,则会自动执行一条CREATE USER语句来创建这个用户,但同时必须为该用户指定密码。 + +##### 示例 + +对本地用户userexample授予SELECT和INSERT权限。 + +```shell +GRANT SELECT,INSERT ON *.* TO 'userexample'@'localhost'; +``` + +#### 回收用户权限 + +可以使用REVOKE语句来回收一个用户的权限,但此用户不会被删除。 + +```shell +REVOKE privilege ON databasename.tablename FROM 'username'@'hostname'; +``` + +其中REVOKE语句的参数与GRANT语句的参数含义相同。 + +要使用 REVOKE 语句,必须拥有数据库的全局CREATE USER权限或UPDATE权限。 + +##### 示例 + +回收本地用户userexample的INSERT权限。 + +```shell +REVOKE INSERT ON *.* FROM 'userexample'@'localhost'; +``` + +### 管理数据库 + +#### 创建数据库 + +可以使用CREATE DATABASE语句来创建数据库。 + +```shell +CREATE DATABASE databasename; +``` + +其中:databasename为数据库名称,且数据库名称不区分大小写。 + +##### 示例 + +创建数据库名为databaseexample的数据库。 + +```shell +CREATE DATABASE databaseexample; +``` + +#### 查看数据库 + +可以使用SHOW DATABASES语句来查看数据库。 + +```shell +SHOW DATABASES; +``` + +##### 示例 + +查看所有数据库。 + +```shell +SHOW DATABASES; +``` + +#### 选择数据库 + +一般创建表,查询表等操作首先需要选择一个目标数据库。可以使用USE语句来选择数据库。 + +```shell +USE databasename; +``` + +其中:databasename为数据库名称。 + +##### 示例 + +选择databaseexample数据库。 + +```shell +USE databaseexample; +``` + +#### 删除数据库 + +可以使用DROP DATABASE语句来删除数据库。 + +>![](./public_sys-resources/icon-caution.gif) **注意:** +>删除数据库要谨慎操作,一旦删除,数据库中的所有表和数据都会删除。 + +```shell +DROP DATABASE databasename; +``` + +其中:databasename为数据库名称。 + +DROP DATABASE命令用于删除创建过\(已存在\)的数据库,且会删除数据库中的所有表,但数据库的用户权限不会自动删除。 + +要使用DROP DATABASE,您需要数据库的DROP权限。 + +DROP SCHEMA是DROP DATABASE的同义词。 + +##### 示例 + +删除databaseexample数据库。 + +```shell +DROP DATABASE databaseexample; +``` + +#### 备份数据库 + +可以在root权限下使用mysqldump命令备份数据库。 + +备份一个或多个表: + +```shell +mysqldump [options] databasename [tablename ...] > outfile +``` + +备份一个或多个库: + +```shell +mysqldump [options] -databases databasename ... > outfile +``` + +备份所有库: + +```shell +mysqldump [options] -all-databases > outputfile +``` + +其中: + +- databasename:数据库名称。 +- tablename:数据表名称。 +- outfile:数据库备份的文件。 +- options:mysqldump命令参数选项,多个参数之间可以使用空格分隔。常用的mysqldump命令参数选项如下: + - -u, \-\-user= _username_ :指定用户名。 + - -p, \-\-password\[= _password_\]:指定密码,在下面的例子中密码都指定为123456,您需要根据实际情况调整。 + - -P, \-\-port= _portnumber_ :指定端口。 + - -h, \-\-host= _hostname_ :指定主机名。 + - -r, \-\-result-file= _filename_ :将导出结果保存到指定的文件中,等同于“\>”。 + - -t:只备份数据。 + - -d:只备份表结构。 + +##### 示例 + +备份主机为192.168.202.144,端口为3306,root用户下的所有有权限读取的数据库到alldb.sql中。 + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 --all-databases > alldb.sql +``` + +备份主机为192.168.202.144,端口为3306,root用户下的db1数据库到db1.sql中。 + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 --databases db1 > db1.sql +``` + +备份主机为192.168.202.144,端口为3306,root用户下的db1数据库的tb1表到db1tb1.sql中。 + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 db1 tb1 > db1tb1.sql +``` + +只备份主机为192.168.202.144,端口为3306,root用户下的db1数据库的表结构到db1.sql中。 + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 -d db1 > db1.sql +``` + +只备份主机为192.168.202.144,端口为3306,root用户下的db1数据库的数据到db1.sql中。 + +```shell +mysqldump -h 192.168.202.144 -P 3306 -uroot -p123456 -t db1 > db1.sql +``` + +#### 恢复数据库 + +可以在root权限下使用mysql命令恢复数据库。 + +恢复一个或多个表: + +```shell +mysql -h hostname -P portnumber -u username -ppassword databasename < infile +``` + +其中: + +- hostname:主机名。 +- portnumber:端口号。 +- username:用户名。 +- password:密码。 +- databasename:数据库名。 +- infile:mysqldump命令中的outfile参数。 + +##### 示例 + +恢复数据库。 + +```shell +# mysql -h 192.168.202.144 -P 3306 -uroot -p123456 -t db1 < db1.sql +```