1] How to create an administrative user for the mysqld database?
After the database is installed, we should create an administrative account for the mysql database. To set the root user as administrator, we should run the following command;
[root@linuxsir01 root]# /opt/mysql/bin/mysqladmin -u root password 123456
[root@linuxsir01 root]#
From the above command, we can know that the administrator of the mysql database is root and the password is 123456.
2] How to enter the mysql database? Take the mysql database administrator root with a password of 123456 as an example;
[root@linuxsir01 root]#/opt/mysql/bin/mysql -uroot -p123456
After outputting the above command, the following prompt appears;
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6 to server version: 3.23.58
Type 'help;' or 'h' for help. Type 'c' to clear the buffer.
mysql>
Note: When operating these commands, the mysqld server should be opened. These novice brothers already know this:)
3] How to operate commands in the database, I think this is covered in the mysql manual. I will mainly talk about a few things to pay attention to. In fact, I can't understand a few commands. If you want to learn it yourself, it is not difficult; if you have operated mysql in windows, it is actually the same here. Mysql is a cross-platform database and its usage is the same.
In the mysql database, every command ends with ";". Some novice brothers may have forgotten to enter the ";" ending in the command, and the result cannot be exited. :):)
1] Check what databases are in mysql?
mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)
mysql>
After mysql is installed and the administrator is set up, when entering the system for the first time, we use the show databases; command to view the list of databases and find that there are two databases, mysql and test, which are self-built by the system and are used by everyone. For practice.
4] How to create and delete a database?
For example, if I want to create a database named linux, I should run the following command
mysql> create database [database name];
So we should run the following command to create a database named linux
mysql> create database linux;
Query OK, 1 row affected (0.00 sec)
Has it been built? ? It must have been built, because everything is OK :)
Check if there is a Linux database?
mysql> show databases;
+----------+
| Database |
+----------+
| linux |
| mysql |
| test |
+----------+
3 rows in set (0.00 sec)
mysql>
So how do we delete a database? ?
mysql> drop database [database name];
For example, if we want to delete the Linux database we just created, we should use the following command;
mysql> drop database linux;
Query OK, 0 rows affected (0.00 sec)
Has it been deleted? ?
mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)
mysql>
5] How to operate a database is a common question. It is recommended to read the mysql manual. There is so much in it. If you operate a database, you first need to specify a database as the current database. You should use the use command.
mysql>use [database];
For example, if I want to specify the Linux database as the current database, it should be
mysql> use linux;
Database changed
mysql>
6] How to back up the database? ?
For example, if we want to back up a database named linux that already exists in mysql, we need to use the command mysqldump.
The command format is as follows:
[root@linuxsir01 root]# /opt/mysql/bin/mysqldump -uroot -p linux > /root/linux.sql
Enter password: Enter the database password here
Through the above command, we need to understand two things. First, the database must be backed up as a database administrator; secondly: the backup destination is /root, and the backup file name is linux.sql. In fact, the location and file name of the backup are determined according to your own situation. You can choose the file name yourself and arrange the path yourself;
For example, I want to back up the Linux database to /home/beinan. The file name of the database is linuxsir031130.sql, so I should enter the following command.
[root@linuxsir01 root]#/opt/mysql/bin/mysqldump -uroot -p linux > /home/beinan/linuxsir031130.sql
Enter password: Enter the database password of the database administrator root here
In this way, we can find the backup file linuxsir031130.sql of the database named linux in mysql in the /home/beinan directory.
To sum up, we must learn to be flexible when we study. :):)
5] How to import the backed up database into the database?
First of all, we still need to operate the above processes, such as adding a database administrator (if you have not added a mysql database administrator), creating a database, etc.
For example, if we want to import the backup of linuxsir031130.sql in the /home/beinan directory into a database named linux, we should do the following;
[root@linuxsir01 root]# /opt/mysql/bin/mysql -uroot -p linux < /home/beinan/linuxsir031130.sql
Enter password: Enter password here
If the machine is good and the database is relatively small, it will only take a few minutes.
6] Some other commonly used mysql instructions;
View status
mysql> show status;
View progress
mysql> show processlist;
+----+------+-----------+------+---------+------+- ------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+- ------+------------------+
| 16 | root | localhost | NULL | Query | 0 | NULL | show processlist |
+----+------+-----------+------+---------+------+- ------+------------------+
1 row in set (0.00 sec)
mysql>
To view the table, you should first specify a database as the current database; for example, a database named linux;
mysql>use linux;
mysql> show tables;
Empty set (0.00 sec)
mysql>
7] A little supplement to the common commands of mysql database;
Several commonly used mysql-related management commands
mysql command: Basic text-based display and use of the mysql database. The usage has been briefly mentioned before; such as login, etc.
mysqladmin command, the command used to create and maintain mysql database, has been briefly mentioned before;
isamchk is used to repair, check and optimize database files with .ism suffix;
mysqldump is used to back up the database, which has been briefly explained before;
myisamchk is used to repair database files with .myi suffix;
For example, if we want to check whether there is a problem with the database table named linux.myi, we should use the following command;
To stop the mysqld server
[root@linuxsir01 root]# /opt/mysql/share/mysql.server stop
and then execute
[root@linuxsir01 root]# /opt/mysql/bin/myisamchk /opt/mysql/var/linux/*.MYI
The above command means to check all .myi files. The database directory is in the /opt/mysql/var/linux/ directory.
If there is a problem, you should use the -r parameter to fix it
[root@linuxsir01 root]# /opt/mysql/bin/myisamchk -r /opt/mysql/var/linux/*.MYI
6] mysqlshow command: display the database and table selected by the user
[root@linuxsir01 root]# /opt/mysql/bin/mysqlshow -uroot -p [database name]
For example, if I want to view the database named linux; it should be:
[root@linuxsir01 root]# /opt/mysql/bin/mysqlshow -uroot -p linux