MySQL under linux boot and access

Start and stop   

1 Start  

MySQL file mysql start after the installation is complete, run the following when you need to start in the /etc/init.d directory command.  

[root@test1 init.d]# /etc/init.d/mysql start 

Or: service mysql start

Or: systemctl start mysqld (centos7)

 

2, stop 

service mysql stop 

Or: systemctl stop mysqld  

 

3, automatic start  

1) look at whether mysql automatically start list  

[root@test1 local]# /sbin/chkconfig –list  

2) to add to your MySQL system inside to start the service group  

[root@test1 local]# /sbin/chkconfig – add mysql  

3) to start the service MySQL delete from the group inside.  

[root@test1 local]# /sbin/chkconfig – del mysql

 

Seven, to change MySQL directory  

MySQL default data file storage directory / var / lib / mysql.

If the directory should move to the next / home / data required the following steps:  

1, the establishment of data directory cd / home mkdir data home directory  

2, the process stopped MySQL service: service mysql stop   

3, the / var / lib / mysql entire directory to / home / data  

mv / var / lib / mysql / home / data / This put the MySQL data file to the next / home / data / mysql  

4, if the configuration file to find my.cnf / etc no my.cnf configuration file / directory, please find the / usr / share / mysql / * .cnf file, a copy of which the / etc / and renamed my.cnf )in. Command is as follows:  

[root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf  

5, MySQL configuration file editing /etc/my.cnf  

To ensure that MySQL to work properly, you need to specify the location mysql.sock generated files.

Modify the right socket = / var / lib / mysql / mysql.sock middle row number is: /home/mysql/mysql.sock.

As follows: vi my.cnf (my.cnf file with the vi editor tool, find the following data modified)  

# The MySQL server   

[mysqld]   

port   = 3306   

#socket = /var/lib/mysql/mysql.sock (original content, in order to be more secure with the "#" comment this line)   

socket = /home/data/mysql/mysql.sock (add this line)  

6, modify the MySQL startup script /etc/rc.d/init.d/mysql  

Finally, the need to modify the MySQL startup script /etc/rc.d/init.d/mysql, to which datadir = / var / lib / mysql row, right-hand side path into the actual storage path you are now in: home / data / mysql.  

[Root @ test1 etc] # we /etc/rc.d/init.d/mysql  

# Datadir = / var / lib / mysql (comment this line)  

datadir = / home / data / mysql (add this line)  

7, restart the MySQL service  

/etc/rc.d/init.d/mysql start or restart a Linux reboot command  

If the move is successful working properly, or in front of the 7-step control check again.  

Eight, MySQL common operation  

Note: MySQL should be a semicolon after each command; the end.  

1, display database  

mysql> show databases;  

+----------+  | Database |  +----------+ 

| mysql  |  | test   |  +----------+  

2 rows in set (0.04 sec)  

Mysql installed just finished two databases: mysql and test.

mysql database is very important, it contains MySQL system information, we change the password and add users actually use this library to operate in the related table.  

 

2, the display table in the database  

mysql> use mysql; (open database, each library will be operated to open the library, similar FoxPro) 

Database changed  

mysql> show tables;  

+-----------------+  | Tables_in_mysql |  

+-----------------+  | columns_priv  |  

| db       |  | func      |  

| host      |  | tables_priv   |  

| user      |  +-----------------+  

6 rows in set (0.01 sec)  

 

3 showing the structure of the data table:  

describe 表名;  

4, a record in the table:  

select * from 表名;  

For example: mysql library user records displayed in the table. All users of MySQL users are operating in this table.  

Select * from user;  

5, building a database:  

create database 库名;  

For example: Create a name aaa-bit library  

mysql> create databases aaa;  

6, built table:  

use the library name;  

create table table name (field set list);  

For example: the establishment of the library in aaa just created table name, the table has id (serial number, automatic growth), xm (name), xb (sex), csny (birth date) four fields  

use aaa;  

mysql> create table name (id int(3) auto_increment not null primary key, xm char(8),xb char(2),csny date); 

You can describe command to see the table structure just set up.  

mysql> describe name;  

+-------+---------+------+-----+---------+----------------+  

| Field | Type  | Null | Key | Default | Extra     |  

+-------+---------+------+-----+---------+----------------+   

| id  | int(3) |   | PRI | NULL  | auto_increment |  

| xm  | char(8) | YES |   | NULL  |        |  

| xb  | char(2) | YES |   | NULL  |        |  

| csny | date  | YES |   | NULL  |        |  

+-------+---------+------+-----+---------+------------ ----+  

7, for example, recorded an increase: increase several related records.  

mysql> insert into name values ​​( '', 'John Doe', 'M', '1971-10-01');  

mysql> insert into name values ​​( '', 'clouds', 'F', '1972-05-20');  

Select command can be used to verify the results. mysql> select * from name;  

+----+------+------+------------+  

| id | xm  | xb  | csny   

|  +----+------+------+------------+  

| 1 | Joe Smith | M | 1971-10-01 |  

| 2 | clouds | F | 1972-05-20 |  

+----+------+------+------------+  

 

8, for example, modify the record: Bob's date of birth will be changed 1971-01-10  

mysql> update name set csny='1971-01-10' where xm='张三';  

9, delete the record  

For example: Joe Smith delete records. 

mysql> delete from name where xm='张三';  

10, delete the database and delete tables  

drop database library name;  

drop table 表名;  

 

Nine, increasing the MySQL user  

Format: grant select on database * to username @ log on the host identified by "password."

Example 1, add a password for the user user_1 123, so that he can log on any host, and all databases have query, insert, modify, delete permissions.

First to root user connected to the MySQL, then type the following command:  

mysql> grant select,insert,update,delete on *.* to user_1@"%" Identified by "123";

Increased user Example 1 is very dangerous, if you know user_1 the password, he can log in to your MySQL database on any computer online and your data do whatever they want, and solutions See Example 2.  

Example 2, add a user user_2 password for the 123, so that the user can only log on localhost, and can be made to the database aaa query, insert, modify, delete operations (localhost means the local host, that is, the MySQL database where that host ), so that users know that the use of user_2 password, he can not access the database directly from the internet, the library can only be operated by aaa MYSQL host.  

mysql>grant select,insert,update,delete on aaa.* to user_2@localhost identified by "123";  

If you can not use the new user log in MySQL, use the following command at login:  

mysql -u user_1 -p -h 192.168.113.50 (-h heel to login ip address of the host)  

Ten, backup and recovery  

1, backup  

For example: aaa library on the cases to create a backup file in back_aaa  

[Root @ test1 root] # cd / home / data / mysql (directory into the library, the library according to the present embodiment has been val / lib / mysql to / home / data / mysql, see Part VII above content)  

[root@test1 mysql]# mysqldump -u root -p --opt aaa > back_aaa  

2, recovery  

[root@test mysql]# mysql -u root -p ccc < back_aaa

 

Reprinted from: https: //www.cnblogs.com/hunter007/articles/2251795.html and made some changes in its content

Guess you like

Origin www.cnblogs.com/indifferent/p/10978588.html