標籤:centos mariadb
centos7.3通過yum方式安裝mariadb
通過安裝包組的方式安裝
yum groupinfo mariadb mariadb-client
yum groupinstall mariadb mariadb-client
啟動服務,第一次啟動時間有點長
systemctl start mariadb
systemctl status mariadb
systemctl enable mariadb
安全性原則設定,設定密碼等
mysql_secure_installation
查看tcp 3306連接埠是否開啟
ss -tunlp
mariadb設定檔
more /etc/my.cnf
資料庫存放位置
ls /var/lib/mysql
登入資料庫
mysql -uroot -p
協助及查詢命令
help
\s
select version();
select user();
show databases;
show mysql;
show tables;
system ls /root
system hostname
quit
centos6.8通過二進位包安裝mariadb
二進位包:
https://downloads.mariadb.org/mariadb/5.5.57/
建立使用者mysql
useradd -r -u 306 -d /app/data/ -s /sbin/nologin mysql
id mysql
getent passwd mysql
建立家目錄即資料庫存放目錄,並更改其屬主為mysql
mkdir /app/data
chown mysql. /app/data/
ll /app/data/ -d
解壓mariadb安裝包,注意必須指定目錄/usr/local/
tar -xf mariadb-5.5.57-linux-x86_64.tar.gz -C /usr/local/
cd /usr/local/
建立一個軟連結,並遞迴變更檔許可權
ln -s mariadb-5.5.57-linux-x86_64/ mysql
chown -R mysql. mysql
ll mysql/
進入安裝目錄mysql,安裝mariadb
cd /usr/local/mysql
產生資料庫檔案,指定資料庫存放目錄和使用者
scripts/mysql_install_db --datadir=/app/data/ --user=mysql
複製設定檔
mkdir /etc/mysql
cp support-files/my-large.cnf /etc/mysql/my.cnf
編輯複製的設定檔,在[mysqld]服務配置中增加如下3條內容
vi /etc/mysql/my.cnf
[mysqld]
datadir = /app/data/
skip_name_resolve = on
innodb_file_per_table = on
添加mysqld服務啟動項,並嘗試啟動服務
chkconfig --add mysqld
chkconfig --list | grep mysqld
service mysqld start
報錯提示如下
Can‘t create/write to file ‘/var/log/mysqld.log‘ (Errcode: 13)
建立記錄檔,嘗試重新啟動服務
touch mysqld.log
chown mysql. mysqld.log
service mysqld start
ss -tnlp
添加PATH路徑,方便執行mysql命令
cd /etc/profile.d/
vi mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
. mysql.sh
登入資料庫,首次登入無密碼
mysql
exit
修改安全性原則yynny
mysql_secure_installation
通過密碼登入
mysql -uroot -p
SQL語句
查看資料庫,SQL語句不區分大小寫show databases;SHOW DATABASES;切換資料庫use mysql;查詢庫中的表show tables;查詢表結構desc user;查詢表中的所有資訊select * from user;查詢表中的指定列資訊select host,user,password from user;如果未指定use哪個資料庫,需指定資料庫名.表名select host,user,password from mysql.user;查詢目前使用者select user();建立資料庫create database mydb;SELECT也可以直接顯示內容,類似於echo,as表示定義欄位別名,支援直接算數運算select "usera" as name,1+2 as result;刪除資料庫drop database mydb;查看所有支援的字元集show character set;查看所有支援的定序show collation;擷取命令協助help showhelp create database查看系統內建的引擎show engines;建立表help create table;use mydbcreate table student (id int unsigned primary key,name varchar(20) not null,age tinyint unsigned);show tables;desc student;查看錶索引show indexes from mydb.student;添加資料,字串必須加‘‘insert into student values(1,‘zhang‘,20);添加個別欄位資料insert into student(id,name) values(2,‘wang‘);查詢資料select * from student;更改記錄update student set name=‘li‘ where id=2;刪除記錄delete from student where id=2;清空表truncate table mydb.student;select * from mydb.student;排序,預設索引會自動排序insert into student values(1,‘zhang‘,20);insert into student values(2,‘wang‘,21);insert into student values(4,‘li‘,24);insert into student values(10,‘zhao‘,20);insert into student values(5,‘song‘,21);select * from student;查詢按name列排序select * from student order by name;倒序descselect * from student order by name desc;查詢結果以別名顯示,as關鍵字可以省略select id 編號,name 名字 from student;查詢空值nullinsert into student(id,name) values(8,‘yang‘);select * from student where age is null;
通過舊錶來建立新表create table test1 (sid int,name varchar(20),address varchar(100));create table test2 select * from test1;desc test2;同上,匯入舊錶的所有資料到新表create table student2 select * from student;select * from student2;同上,只匯入部分資料create table student3 select id,name from student;select * from student3;大量匯入資料到新表insert into test2 select id,name from student;尋找name欄位包含a的資料條目select * from test2 where name like "%a%";條件過濾select * from student where id in (1,5,10);select * from student where id >=2 and id <=8;select * from student where is between 2 and 8;select * from student where name != ‘zhang‘;
添加遠程登入使用者,使用者的組成有兩部分:‘使用者名稱‘@‘主機地址‘,如下使用者名稱[email protected]用戶端網段,密碼redhat建立遠程登入使用者create user ‘usera‘@‘192.168.%.%‘ identified by ‘redhat‘;create user ‘userb‘@‘192.168.10.%‘ identified by ‘redhat‘;測試遠程登入mysql -uusera -h 192.168.10.10 -p預設為普通使用者權限,不能顯示所有資料庫show databases;刪除使用者,注意使用者名稱必須和之前建立的保持一致。使用者名稱格式為:‘使用者名稱‘@‘主機地址‘drop user ‘userb‘@‘192.168.10.%‘;更改使用者口令set password for ‘usera‘@‘192.168.%.%‘=password(‘centos‘);通過mysql管理命令更改使用者口令/usr/local/mysql/bin/mysqladmin -u usera -p centos password ‘redhat‘授權查詢和刪除,和允許所有操作grant select on mydb.* to ‘usera‘@‘192.168.%.%‘;grant delect on mydb.test to ‘usera‘@‘192.168.%.%‘;grant all on mydb.* to ‘userb‘@‘192.168.%.%‘ identified by userb; #建立帳號並授權其對mydb的所有許可權遠程通過usera使用者登入,再次查看資料庫,即可看到mydb的資料庫show databases;use mydb;delete from test;禁止使用者操作revoke delete on mydb.* from ‘userb‘@‘192.168.%.%‘;flush privileges;需要退出再重新登入,才會生效
本文出自 “rackie” 部落格,請務必保留此出處http://rackie386.blog.51cto.com/11279229/1952051
centos通過yum方式和二進位包安裝mariadb