码迷,mamicode.com
首页 > 数据库 > 详细

Linux中mariadb的通用二进制安装及mysql的基本命令

时间:2016-04-17 18:00:10      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:二进制   linux   通用   mysql   

一、mariadb的二进制安装:   

[root@www ~]# tar -xf mariadb-5.5.46-linux-x86_64.tar.gz -C /usr/local/
[root@www local]# ln -s mariadb-5.5.46-linux-x86_64/ mysql
[root@www local]# ls
 mariadb-5.5.46-linux-x86_64  mysql    //最好软链接成mysql

   1、先创建mysql系统用户,mysql组,让mysql系统用户去运行mysql,保证安全性: 

[root@www /]# groupadd mysql
[root@www /]# useradd -r mysql

  2、把mysql目录下的文件和目录都设置为属主root,属组mysql:

[root@www mysql]# chown -R root:mysql ./*

  3、创建mysql存储数据的目录,并把属主和属组:

[root@www mysql]# mkdir -p /data/mydata
[root@www mysql]# chown -R mysql:mysql /data/mydata/
[root@www mysql]# ll -d /data/mydata/
drwxr-xr-x. 2 mysql mysql 4096 Apr 17 08:24 /data/mydata/

 4、根据自己服务器的配置选择配置文件:

         my-huge.cnf : 用于高端产品服务器,包括1到2GB RAM,主要运行mysql  
         my-innodb-heavy-4G.ini : 用于只有innodb的安装,最多有4GB RAM,支持大的查询和低流量
         my-large.cnf : 用于中等规模的产品服务器,包括大约512M RAM
         my-medium.cnf : 用于低端产品服务器,包括很少内存(少于128M)
         my-small.cnf : 用于最低设备的服务器,只有一点内存(少于512M)

[root@www support-files]# cp my-large.cnf /etc/my.cnf
[root@www support-files]# vim /etc/my.cnf
[mysqld]                              //在mysqld这里添加三句话
datadir = /data/mydata                //存储数据目录
skip_name_resolve = ON                //禁止反解主机名
innodb_file_per_table = ON            //修改InnoDB为独立表空间模式

  5、复制mysql.server到/etc/init.d/目录下:

[root@www support-files]# cp mysql.server /etc/init.d/mysqld
[root@www support-files]# chkconfig --add mysqld

  6、对数据库进行初始化安装:

[root@www scripts]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --datadir=/data/mydata --basedir=/usr/local/mysql
[root@www scripts]# service mysqld start
Starting MySQL.. SUCCESS!

  7、对数据库进行安全加固,数据库默认密码都是为空,删除test的数据库:  

[root@www scripts]# /usr/local/mysql/bin/mysql_secure_installation
  Enter current password for root (enter for none):     //直接回车,因为密码为空
  Set root password? [Y/n] y     //设置新密码
  New password: 
  Remove anonymous users? [Y/n] y  //删除其他没用的用户
  ... Success!
  Disallow root login remotely? [Y/n] n   //禁止远程登录mysql,建议禁止
   ... skipping
  Remove test database and access to it? [Y/n] y  //是否删除测试数据库
  - Dropping test database...
  Reload privilege tables now? [Y/n] y     //是否重新加载权限列表
  ... Success!

  到此Mariadb二进制安装完成,可以使用mysql数据库了:  

[root@www bin]# mysql -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 11
Server version: 5.5.46-MariaDB-log MariaDB Server

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

Type ‘help;‘ or ‘\h‘ for help. Type ‘\c‘ to clear the current input statement.

MariaDB [(none)]>

 二、mysql的基本命令:  

    1、用户的创建及授权:        

      用户账号:‘username‘@‘host‘

                 host:此用户访问当前mysql服务器时,允许其通过哪些主机远程创建连接

                      表示方式:IP,网络地址、主机名、通配符(%和_);

                            172.18.1.1.  172.18.0.0/24  www.magedu.com 172.18.%.%           

     创建用户账号:                            //只有查看的权限

                  CREATE USER ‘username‘@‘host‘ IDENTIFIED BY ‘passwd’ 

MariaDB [(none)]> create user ‘testusr‘@‘localhost‘;
Query OK, 0 rows affected (0.00 sec)

       删除用户账号:

                 DROP USER ‘user‘@‘host‘[,user@host]....

MariaDB [(none)]> drop user ‘testusr‘@‘localhost‘;
Query OK, 0 rows affected (0.00 sec)

     给用户授权:

                 权限级别:管理权限、数据库、表、字段、存储历程;

         GRANT priv_tyep,...ON [object_type] db_name.tbl_name TO ‘user‘@‘host‘ [IDENTIFIED BY ‘passwd‘] ;

             priv_tyep : select、insert、delete、update、atler、drop、create或者all

                db_name.tbl_name:     //可以对表限制

                  *.*:所有库的所有表

                  db_name.*:指定库的所有表

                  db_name.tbl_name:指定库的特定表

                  db_name.routine_name:指定库上的存储过程或存储函数

MariaDB [mytest]> grant select on mytest.students to ‘testusr‘@‘localhost‘;
Query OK, 0 rows affected (0.14 sec)      //如果前面创建了用户,那这里不同给密码

    取消用户授权:

             REVOKE priv_type,..ON db_name FROM ‘username‘@‘host‘;   

MariaDB [mytest]> revoke select on mytest.students from ‘testusr‘@‘localhost‘;
Query OK, 0 rows affected (0.00 sec)

       查看用户所获得的权限:

           show grants for ‘user‘@‘host‘;

           show grants for  current_user;   //查看当前用户的权限   

MariaDB [mytest]> show grants for ‘testusr‘@‘localhost‘;
+---------------------------------------------+
| Grants for testusr@localhost                |
+---------------------------------------------+
| GRANT USAGE ON *.* TO ‘testusr‘@‘localhost‘ |
+---------------------------------------------+
1 row in set (0.00 sec)

MariaDB [mytest]> show grants for current_user;
+----------------------------------------------------------------------------------------------------------------------------------------+
| Grants for root@localhost                                                                                                              |
+----------------------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO ‘root‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ WITH GRANT OPTION |
| GRANT PROXY ON ‘‘@‘‘ TO ‘root‘@‘localhost‘ WITH GRANT OPTION                                                                           |
+----------------------------------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

注意:MariaDB服务进程启动时,会读取mysql库的所有授权表至内存中;

              1、GRANT或REVOKE命令等执行的权限操作会保存于表中,MariaDB此时一般会自动重读授权表,权限修改会立即生效  

              2、其它方式实现的权限修改,要想生效,必须手动运行FLUSH PRIVILEGES命令方可

 

mysql的命令分为两种,客户端命令和服务端命令:  

    客户端命令:

                  \use           \u  db_name:设定哪个库为默认数据库

                         \exit           \q:退出

                         \delimiter   \d CHAR:设定新的语句结束符

                         \go            \g:语句结束标记

                         \ego          \G: 语句结束标记,结果竖排方式显示;

                         \status       \s: 显示客户端信息

                         \clear         \c:取消语句执行,相当于CRTRL +C

     服务端命令:

                                DDL:  数据定义语言,主要用于管理数据组件,例如表、索引、视图、用户、存储过程

                                         create、drop、alter

                                DDM: 数据操纵语言,主要用管理表中的数据,实现数据的增、删、改、查

                                         insert、delete、select、update

                                DCL:  用户权限 

                                         grant、reovke

     注意:每个语句结束符都必须要有语句结束符,默认为(;)。

数据库数据都是有类型的:

         字符型:
                 定长字符型: 最大不超过255字符

                       char(#) :    不区分字符大小写                  以下 # 都代表数字

                       binary(#) :  区分字符大小写

                 变长字符型:   最大好像是6W个字符

                        varchar(#) 和 varbinary(#):

         对象存储:

                    text和 blob(布尔型)

          内置类型:

                    set 和 enum(枚举) :限定选择哪些类型

        数值型:

                  精确数值型:int 、bigint、smallint、tinyint

                  近似数值型:float和dobule      

     数据类型有修饰符:

                  UNSIGNED:无符号      要紧跟在数值型后面,不然会报错

                  NOT NULL:非空

                  DEFAULT value:默认值


1、数据库的管理:创建、删除、和修改

             创建:CREATE { DATABASE | SCHEMA } db_name ;                 

MariaDB [(none)]> create database mytest;
Query OK, 1 row affected (0.01 sec)

        也可以在后面跟上语句设置字符集合排序规则,不写表示默认:

             [DEFAULT] CHARACTER SET [=] charset_name //设定字符集

                        [DEFAULT] COLLATE [=] colation_name      //设定排序规则            

                    查看支持的所有字符集:SHOW CHARACTER SET    

                    查看支持的所有排序的字符集: SHOW COLLATION

MariaDB [mytest]> status;    //查看创建数据库的信息
--------------
mysql  Ver 15.1 Distrib 5.5.46-MariaDB, for Linux (x86_64) using readline 5.1

Connection id:		11
Current database:	mytest
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		‘‘
Using delimiter:	;
Server:			MariaDB
Server version:		5.5.46-MariaDB-log MariaDB Server
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	latin1
Db     characterset:	latin1
Client characterset:	utf8
Conn.  characterset:	utf8
UNIX socket:		/tmp/mysql.sock
Uptime:			1 hour 21 min 59 sec

Threads: 1  Questions: 70  Slow queries: 0  Opens: 6  Flush tables: 2  Open tables: 32  Queries per second avg: 0.014
--------------

    修改:ALTER {DATABASE | SCHEMA } [db_name]        

                            [DEFAULT] CHARACTER SET [=] charset_name

                            [DEFAULT] COLLATE [=] colation_name

MariaDB [mytest]> alter database mytest character set = cp932;
Query OK, 1 row affected (0.00 sec)

     删除:DROP {DATABASE | SCHEMA } db_name     

MariaDB [mytest]> drop database mytest;
Query OK, 1 row affected (0.37 sec)

2、表的管理:创建、修改、删除

        创建: CREATE TABLE [IF NOT EXISTS] tbl_name (create_definition) [table_options]

                   (create_definition:)

                           字段:col_name data_type

                           键:

                              PRIMARY KEY(col1,col2,..) 主键

                              UNIQUE KEY(col1,col2...)  唯一键

                              FOREIGN KEY(column)       外键                   

                   (table_options:)

                             ENGINE[=]engine_name :指明存储引擎

                                    查看数据库支持的所有存储引擎类型:

                                          mysql>SHOW ENGINES;

                                    查看某表的存储引擎类型:

                                          mysql>SHOW TABLES STATUS [LIKE ‘tbl_name‘]                      

示例:创建一张表,包含ID号,姓名,年龄,性别,身份证号,把ID和名称设为主键,身份证号设置为唯一键    

MariaDB [mytest]>create table students(id int unsigned not null,name char(30) not null,age tinyint unsigned,userID bigint,gender enum("w","m"),primary key(id,name),unique key(userID));
Query OK, 0 rows affected (7.48 sec)

MariaDB [mytest]> desc students;
+--------+---------------------+------+-----+---------+-------+
| Field  | Type                | Null | Key | Default | Extra |
+--------+---------------------+------+-----+---------+-------+
| id     | int(10) unsigned    | NO   | PRI | NULL    |       |
| name   | char(30)            | NO   | PRI | NULL    |       |
| age    | tinyint(3) unsigned | YES  |     | NULL    |       |
| userID | bigint(20)          | YES  | UNI | NULL    |       |
| gender | enum(‘w‘,‘m‘)       | YES  |     | NULL    |       |
+--------+---------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

   修改: ALTER [ONLINE | OFFLINE] [IGNORE] TABLE tbl_name  [alter_specification]                    

           alter_specification:

             字段:

               添加:ADD [COLUMN] col_name data_type [FIRST |AFTER col_name]

                              删除:DROP [COLUMN] col_name

                              修改:

                                      改名字:CHANGE [COLUMN] old_col_name new_col_name column_definition [FIRST|AFTER col_name]

                                      改属性:MODIFY [COLUMN] col_name column_definition [FIRST | AFTER col_name] 

    示例:在name后面添加一个班级,修改属性,最后删除这个字段;

MariaDB [mytest]> alter table students add class varchar(100) not null after name;
Query OK, 0 rows affected (0.27 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mytest]> alter table students modify class varchar(50) after name;
Query OK, 0 rows affected (0.16 sec)               
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mytest]> alter table students drop class;
Query OK, 0 rows affected (0.12 sec)               
Records: 0  Duplicates: 0  Warnings: 0

           键:

             添加:ADD {PRIMARY |UNIQUE|FOREIGN} KEY (col1,col2,...) 

                         删除:DROP PRIMARY KEY 删除主键

                                  DROP FOREIHN KEY  fk_symbol

MariaDB [mytest]> alter table students add unique key(age);
Query OK, 0 rows affected (0.63 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mytest]> alter table students drop primary key;
Query OK, 0 rows affected (0.22 sec)               
Records: 0  Duplicates: 0  Warnings: 0

                     索引:

                          添加: ADD {INDEX|KEY} [index_name](col1,col2..)

                          删除: DROP {index|key} index_name

MariaDB [mytest]> alter table students add index ssalc(class);
Query OK, 0 rows affected (0.30 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mytest]> show index from students;
|  Table   | Non_unique | Key_name 
| students |          1 | ssalc    

MariaDB [mytest]> alter table students drop index ssalc;
Query OK, 0 rows affected (0.08 sec)
Records: 0  Duplicates: 0  Warnings: 0

 删除表:      DROP TABLE [IF EXISTS] tbl_name;                   

                       

3、索引的管理:创建和删除

     创建: CREATE [UNIQUE|FULLTEXT|SPATAL] INDEX index_name [BTREE|HASH] ON tbl_name (Col1,col2...) 

     删除:  DROP INDEX index_name ON tbl_name   

MariaDB [mytest]> create index class_and_name on students(class,name);
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [mytest]> create index class_and_name on students(class,name);
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

4、对表的数据进行管理:insert、delete、update、select                                          

INSERY INTO:  添加语句

              INSERT [INTO] tbl_name [(col1,..)] {VALUES|VALUE}(val1,...),(...)

MariaDB [mytest]> insert into students(id,name,age) values (1,‘xiao ming‘,24);
Query OK, 1 row affected, 1 warning (0.09 sec)

      注意:

                   字符型:引号 ‘ ‘        ‘xiao ming‘

                   数值型:不能用引号 ‘ ‘     74

SELECT: 查询语句              

                  1、SELECT * FROM tbl_name;                      查看表中的所有字段

                  2、SELECT col1,col2,......FROM tbl_name;      挑选表中的字段查看

                3、SELECT col1,.....FROM tbl_name WHERE clause;  根据where clause条件查看字段

MariaDB [mytest]> select * from students;
+----+-----------+------+--------+--------+-------+
| id | name      | age  | userID | gender | class |
+----+-----------+------+--------+--------+-------+
|  1 | xiao ming |   24 |   NULL | NULL   |       |
+----+-----------+------+--------+--------+-------+
1 row in set (0.04 sec)

MariaDB [mytest]> select id,name  from students;
+----+-----------+
| id | name      |
+----+-----------+
|  1 | xiao ming |
+----+-----------+
2 rows in set (0.00 sec)

              WHERE clause:用于指明挑选条件

                                   col_name 操作符 value ;

                                      比如:age > 30 ;

          操作符有:         

             比较条件:>,<,>=,<=,== 

             组合条件:and  or not

                     大于等于和小于等于: BETWEEN....AND.....                                     

                     通配符:

                            LIKE  ‘PAT‘  

                                   %:任意长度的任意字符

                                   _: 任意单个字符

                    正则表达式:  RLIKE ‘PAT‘

                    数据为空: is null                //age is null

                    数据不为空:is not null        //age is not null      

MariaDB [mytest]> select * from students where age > 22;
+----+-----------+------+--------+--------+------------+
| id | name      | age  | userID | gender | class      |
+----+-----------+------+--------+--------+------------+
|  2 | xiao ming |   23 |   NULL | NULL   | yi nian ji |
|  3 | xiao hong |   24 |   NULL | NULL   | yi nian ji |
|  4 | xiao tian |   25 |   NULL | NULL   | yi nian ji |
+----+-----------+------+--------+--------+------------+
3 rows in set (0.09 sec)

MariaDB [mytest]> select * from students where gender is not null;
+----+---------+------+--------+--------+------------+
| id | name    | age  | userID | gender | class      |
+----+---------+------+--------+--------+------------+
|  5 | xiao li |   26 |   NULL | w      | er nian ji |
+----+---------+------+--------+--------+------------+
1 row in set (0.00 sec)

MariaDB [mytest]> select * from students where name rlike ‘ng$‘;
+----+-----------+------+--------+--------+------------+
| id | name      | age  | userID | gender | class      |
+----+-----------+------+--------+--------+------------+
|  1 | xiao wang |   22 |   NULL | NULL   | yi nian ji |
|  2 | xiao ming |   23 |   NULL | NULL   | yi nian ji |
|  3 | xiao hong |   24 |   NULL | NULL   | yi nian ji |
+----+-----------+------+--------+--------+------------+
3 rows in set (0.06 sec)

             4、对数据进行排序:

                         SELECT col1,...FROM tbl_name [WHERE clause] ORDER BY col_name,col_name2 [ASC|DESC]                        

                                ASC:升序                                        

                                DESC:降序  

MariaDB [mytest]> select * from students order by id desc;
+----+-----------+------+--------+--------+------------+
| id | name      | age  | userID | gender | class      |
+----+-----------+------+--------+--------+------------+
|  5 | xiao li   |   26 |   NULL | w      | er nian ji |
|  4 | xiao tian |   25 |   NULL | NULL   | yi nian ji |
|  3 | xiao hong |   24 |   NULL | NULL   | yi nian ji |
|  2 | xiao ming |   23 |   NULL | NULL   | yi nian ji |
|  1 | xiao wang |   22 |   NULL | NULL   | yi nian ji |
+----+-----------+------+--------+--------+------------+
5 rows in set (0.04 sec)

delete:按行进行删除

              1、DELETE FROM tbl_name [WHERE where_conditin] 

             2、DELETE FROM tbl_name [ORDER BY ..] [LIMIT row_count]

MariaDB [mytest]> delete from students where gender is not null;
Query OK, 1 row affected (0.13 sec)

MariaDB [mytest]> delete from students  order by id desc limit 2;
Query OK, 2 rows affected (0.04 sec)    //把最大两个ID删除

update:对数据进行修改

UPDATE table_reference SET col_name1=value1 [, col_name2=value2] ...[WHERE where_condition] [ORDER BY ...] [LIMIT row_count] 

MariaDB [mytest]> update students set age=age-10 where id >1;
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2  Changed: 2  Warnings: 0
MariaDB [mytest]> update students set age=age+10  order by  age desc limit 2;
Query OK, 2 rows affected (0.05 sec)
Rows matched: 2  Changed: 2  Warnings: 0


Linux中mariadb的通用二进制安装及mysql的基本命令

标签:二进制   linux   通用   mysql   

原文地址:http://lanxianting.blog.51cto.com/7394580/1764757

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!