Home > Database > Mysql Tutorial > Some basic usage of mysql under linux

Some basic usage of mysql under linux

黄舟
Release: 2016-12-16 11:23:18
Original
1078 people have browsed it

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]#

via 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 the password 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 have known it for a long time:)


3] How to operate commands in the database, I think this is all 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 a ; sign. Some novice brothers may have forgotten to enter a ; sign, and the result cannot be exited. :):)

1] Check what databases are available in mysql?

Code:

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 show databases; Use the command to view the list of databases and find that there are two databases, mysql and test. These are self-built by the system and are for everyone to 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?

Code:

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)

Yes Hasn't it been deleted? ?

Code:

mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.00 sec)

mysql>


5] There are many questions about how to operate a database. It is recommended to read the mysql manual. There's so much in there. If you operate a database, you must first 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 password of the database 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 It 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

This way we get to The backup file linuxsir031130.sql of the database named linux in mysql can be found in the /home/beinan directory

To sum up, we must learn to be flexible when studying. :):)

5] How to import the backed up database into the database?

First 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 directory /home/beinan 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 the password here

If the machine is good and the database is small, it will only take a few minutes.

6] Some other commonly used mysql commands;

View status
mysql> show status;

View process

Code:

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, Display and use the mysql database. The usage has been briefly mentioned before; such as login, etc.

mysqladmin command, a 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, as mentioned earlier It has been briefly explained;


myisamchk is used to repair the database file with the .myi suffix;

For example, if we want to check whether there is a problem with the .myi database table of the database named linux, we should use the following command;

To use mysqld Server stop
[root@linuxsir01 root]# /opt/mysql/share/mysql.server stop

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

The above is the basic usage of mysql under Linux. For more related articles, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template