到mysql官網上下載rpm包
https://dev.mysql.com/downloads/mysql/5.7.html
https://dev.mysql.com/downloads/file/?id=487595
四個rmp包以下
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-server-5.7.26-1.el7.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-client-5.7.26-1.el7.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-common-5.7.26-1.el7.x86_64.rpm
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-community-libs-5.7.26-1.el7.x86_64.rpm
wget下載相應的包,rpm安裝就能夠了,若是出現警告,加--force --nodeps參數
安裝順序 common-->libs-->client-->server
二、二進制安裝mariadbnode
二進制格式安裝過程
(1) 準備用戶
groupadd -r -g 306 mysql
useradd -r -g 306 -u 306 -d /data/mysql mysql -s /sbin/nologin
(2) 準備數據目錄,建議使用邏輯卷
mkdir /data/mysql
chown mysql:mysql /data/mysql
或者用install命令
install -d /data/mysql -o mysql -g mysql
(3) 準備二進制程序
下載地址:https://downloads.mariadb.org/
tar xf mariadb-VERSION-linux-x86_64.tar.gz -C /usr/local
cd /usr/local
ln -sv mariadb-VERSION mysql
chown -R root:mysql /usr/local/mysql/
4) 準備配置文件
mkdir /etc/mysql/
cp support-files/my-large.cnf /etc/mysql/my.cnf
[mysqld]中添加三個選項:
datadir = /data/mysql
innodb_file_per_table = on
skip_name_resolve = on 禁止主機名解析,建議使用
(5)建立數據庫文件
cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
(6)準備服務腳本,並啓動服務
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
service mysqld start
(7)PATH路徑
echo ‘PATH=/user/local/mysql/bin:$PATH’ > /etc/profile.d/mysql
(8)安全初始化
/user/local/mysql/bin/mysql_secure_installation
三、編譯安裝mariadbmysql
安裝包
yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel
作準備用戶和數據目錄
useradd –r –s /sbin/nologin –d /data/mysql/ mysql
mkdir /data/mysql
chown mysql.mysql /data/mysql
tar xvf mariadb-10.2.18.tar.gz
cmake 編譯安裝
cmake的重要特性之一是其獨立於源碼(out-of-source)的編譯功能,即編譯工做能夠在
另外一個指定的目錄中而非源碼目錄中進行,這能夠保證源碼目錄不受任何一次編譯的影
響,所以在同一個源碼樹上能夠進行屢次不一樣的編譯,如針對於不一樣平臺編譯
編譯選項:https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
cd mariadb-10.2.18/
cmake . \
-DCMAKE_INSTALL_PREFIX=/app/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc/mysql \
-DMYSQL_USER=mysql \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
-DWITH_DEBUG=0 \
-DWITH_READLINE=1 \
-DWITH_SSL=system \
-DWITH_ZLIB=system \
-DWITH_LIBWRAP=0 \
-DENABLED_LOCAL_INFILE=1 \
-DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
make && make install
提示:若是出錯,執行rm -f CMakeCache.txt
準備環境變量
echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
生成數據庫文件
cd /app/mysql/
scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql
準備配置文件
cp /app/mysql/support-files/my-huge.cnf /etc/my.cnf
準備啓動腳本
cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
啓動服務
chkconfig --add mysqld ;service mysqld start
四、students表的數據,實現下面的需求,寫出sql:react
MariaDB [db1]> select * from students;linux
+—-+——–+——-+——-+——+——-+c++
| id | name | phone | score | swx | class |sql
+—-+——–+——-+——-+——+——-+數據庫
| 1 | adc | 110 | 86 | m | 1 |安全
| 2 | zhou | NULL | 80 | f | 1 |app
| 3 | fei | NULL | 100 | f | 2 |
| 4 | lin | NULL | 88 | m | 2 |
| 5 | wang | 1000 | 99 | m | 2 |
| 6 | 飛哥 | 10000 | 99 | m | 2 |
1)在students表中,查詢年齡大於25歲,且爲男性的同窗的名字和年齡
select name,age from students where age>25 and swx='m';
2)以ClassID爲分組依據,顯示每組的平均年齡
select class,ave(age) from students group by class;
3)顯示第2題中平均年齡大於30的分組及平均年齡
select class,avg(age) from students group by class having avg(age)>30;
4)顯示以L開頭的名字的同窗的信息
select name from student where name like 'L%';
5)顯示TeacherID非空的同窗的相關信息
select * from students where teacherid is not null;