Access mysql database

1. Log in to the mysql database

[root@localhost /]# mysql -u root   //-u指定用户登陆 
[root@localhost /]# mysql -u root -p   //-p需要密码验证 
[root@localhost /]# mysql -h 8.8.8.8 -u root  //-h指定服务器地址

By default, mysql does not allow remote login, you can add authorized to allow remote login

Log in mysql execute the following command:

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123123' WITH GRANT OPTION;     //允许root用户使用123123密码远程登录
mysql> FLUSH PRIVILEGES;   //刷新权限
update mysql.user set password=('putianhui') where user='root'

2. Exit mysql database

In the implementation of exit or quit from the command mysql tools mysql> Environment


Password-related operations

A way to change your password:
mysql> update mysql.user set password=password('123123') where user='root';        //将root用户的密码更改为123123
mysql> flush privileges;   //刷新用户权限信息
Change Password way:
[root@localhost ~]# mysqladmin -u root -p'旧密码' password '新密码'
mysqladmin -u root password 123123        //更改root用户的密码
Password Forgot your password recovery:
[root@localhost /]# vim /etc/my.cnf   //修改mysql配置文件
---------------------------------------添加下行内容----------------------------------
skip-grant-tables     //跳过密码验证
---------------------------------------------------------------------------------------
[root@localhost /]# systemctl restart mysqld  //重启mysql服务
[root@localhost /]# mysql –uroot   //重新使用root登陆

Note: If you change the password still can not log in, put the user in the user table fields and password fields delete empty rows, then refresh permission information and try again


Mysql common operations

1.  View database

mysql> show databases;   //查看数据库列表


2.  View the table

mysql> use ceshi;            //切换到ceshi数据库
mysql> show tables;            //查看ceshi数据库都有哪些表


3.  View the table structure (information for each field), specify the library name. Table name as a parameter, if only the name of the table first with the use handover to the target database

方法一:
mysql> describe mysql.user;               //查看mysql数据库user表的结构
方法二:
mysql> use mysql                        //切换到mysql数据库
mysql> describe user;                    //查看user表的结构


4.  Create a database

mysql> create database ceshi;       //创建一个数据库,名为ceshi


5.  Create a table

语法:create table 表名(字段1名称 类型,字段2名称 类型,primary key(主键名));
mysql> create table biao1 (name char(16) not null,passwd char(16) not null,primary key(name));          //创建表名称为biao1,有name列、passwd列,将name列设置为主键


6.  To delete a table, to specify the library name. Table name as a parameter, if only the name of the table first with the use handover to the target database

方法一:
mysql> drop table ceshi.biao1;       //删除ceshi库中的biao1
方法二:
mysql> use ceshi;                    //切换到ceshi数据库
mysql> drop table biao1;           //删除biao1


7.  Delete database

mysql> drop database ceshi;    //删除ceshi数据库


8.  Insert Data

语法: insert into 表名(字段1,字段2,) values(字段1的值,字段2的值)
mysql> insert biao1 (name,passwd) values('lisi','123');                //向biao1中插入一条记录name为lisi,passwd为123


9.  query data

语法:select 字段名1,字段名2 from 表名 where 条件表达式
mysql> select name,passwd from biao1 where name='lisi';                //查询biao1中name是lisi的name和passwd信息


10. The  modified data

语法: update 表名 set 字段1名=字段1更改后的值 where 条件表达式
mysql> update ceshi.biao1 set passwd='456' where name='lisi';        //将biao1中name是lisi的passwd更改为456

11. Delete data records, without the conditional expression is to delete all records

语法:delete from 表名where 条件表达式
mysql> delete from biao1 where name='lisi';          //删除biao1中name是lisi的记录

12. Database to grant permissions

GRANT Note:

Ø  permissions list: to list a variety of database operations authorized to use, separated by commas, using all the permissions on behalf of all

Ø  library name table name: used cars designated libraries, and authorized to operate the table, you can use wildcard "*" means that all libraries and tables, ". Ceshi *" means all tables ceshi library

Ø  username @ Source Address: Specifies the user name used to access and allow client addresses, who is the link, from where connections can use wildcard characters "%" means any network, such as "% .aptech.com" "192.168. 1% ", etc.

Ø  the IDENTIFIED BY: for setting a user password to connect to the database, the new user omits this field indicates that the user is a blank password

语法:GRANT 权限列表 ON 库名.表名 TO 用户名@来源地址 [ identified by ‘密码’ ]
mysql> grant all on *.* to 'zhangsan'@'%' identified by 'putianhui';            //新建zhangsan用户授予所有库所有表有所有的权限

13. queries the user's authorization information

语法:show grants for '用户名@'来源地址';
mysql> show grants for 'zhangsan'@'%';  //查询zhangsan用户授权信息

14. revoke user authorization information, but also revoked after landing database, can not operate

语法:revoke 权限信息 on 库名.表名. from '用户名'@'来源地址';
mysql> revoke all on *.* from 'zhangsan'@'%';         //撤销zhangsan用户的授权信息

15 . Modify the properties listed in the table

语法:alter table 表名 modify column 列名 新的列类型
Alter table user modify column 姓名 char(200)         //将user表中姓名列类型改为char(200)