1、使用rpm安装mysql
yum install mysql-server安装服务器包,实际上这条命令会自动安装客户端包。
查看包配置文件

service mysqld start 启动 数据库

启动成功,会出现提示,实际上是跑了一个初始化的脚本在/var/lib/mysql/中生成了套接字文件及其他数据库文件
mysql默认没有启动密码。可以执行以下命令设置mysql中root口令。
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h centos6 password 'new-password'


u指定用户 -p口令 -S套接字文件。本机连接需要指定套接字,通过套接字文件连接
也可不指定以上选项。

centos7中安装包么有取消了mysql包,用mariadb包取代mysql,我们可以直接安装mariadb-server包,过程和centos6安装启动类似。
yum install mariadb-server
查看配置文件路径

启动数据库服务查看端口

连接数据库

安装完成。
2、二进制安装mariadb
二进制安装是已经编译完成的包,可以直接在mariadb官网中下载
下载路径https://mariadb.org/mariadb-10-0-11-now-available/

因为二进制包已经编译完成。有指定的安装路径。需要将包解压到/usr/local/mysql目录中
tar -xf mariadb-10.2.19-linux-x86_64.tar.gz -C /usr/local 解压到指定目录

解压完成。名称太长,可以指定一个软连接。方便以后修改

此目录存放这mariadb的程序

创建mysql用户,默认二进制安装没有生成mysql用户
useradd -r -s /sbin/nologin -d /data/mysql -c 'maridb user' mysql
mkdir /data/mysql 创建mysql的家目录
查看目录信息。发现所属组不对,需要改成mysql用户和组

创建mysql用户,默认二进制安装没有生成mysql用户

执行scripts目录中的脚本。初始化创建数据库。并指定用户和数据库存放路径
./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

目录中生成初始数据库

创建一个配置文件目录 /etc/mysql 用于存放数据库配置文件。
cp support-files/my-huge.cnf /etc/mysql/my.cnf

修改一下 socket 文件路径指向我们自己创建的数据库路径。
cp support-files/mysql.server /etc/init.d/mysqld 将服务脚本拷贝到init.d目录中
chkconfig -add mysqld 加入系统启动服务

加入path路径。

连接数据库
3、编译安装mariadb
下载源码包https://downloads.mariadb.org/mariadb/10.3.15/
安装编译工具
yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boostdevel
gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssldevel
libevent-devel libaio-devel
创建用户组
useradd -r -s /sbin/nologin -d /data/mysql/ mysql


tar -xvf mariadb-10.2.19.tar.gz 解压文件
进入mariadb-10.2.19目录开始执行编译
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
4、students表的数据,实现下面的需求,写出sql:
MariaDB [db1]> select * from students;
+—-+——–+——-+——-+——+——-+
| id | name | phone | score | swx | class |
+—-+——–+——-+——-+——+——-+
| 1 | adc | 110 | 86 | m | 1 |
| 2 | zhou | NULL | 80 | f | 1 |
| 3 | fei | NULL | 100 | f | 2 |
| 4 | lin | NULL | 88 | m | 2 |
| 5 | wang | 1000 | 99 | m | 2 |
| 6 | 飞哥 | 10000 | 99 | m | 2 |
+—-+——–+——-+——-+——+——-+
create table students(id int unsigned auto_increment primary key ,name varchar(10) not null,phone varchar(11),socre tinyint unsigned,swx enum('f','m') default 'm' ,class tinyint unsigned);
创建表结构
alter table students add age tinyint unsigned after class;
update students set age=25 where id=1;
update students set age=22 where id=2;
update students set age=28 where id=3;
update students set age=35 where id=4;
update students set age=32 where id=5;
update students set age=25 where id=6;
添加年龄字段并更新数据
1)在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name,age from students where age > 25 and swx='m';

2)以ClassID为分组依据,显示每组的平均年龄
select class,avg(age) as 平均年龄 from students group by class;
利用数据库自带函数avg函数统计

3)显示第2题中平均年龄大于30的分组及平均年龄
select class,avg(age) as 平均年龄 from students group by class having avg(age) > 30;

4)显示以L开头的名字的同学的信息
select * from students where name like 'l%';

5)显示TeacherID非空的同学的相关信息
select * from students,teacher where students.class=teacher.id;

select * from students inner join teacher on students.class=teacher.id;

网友评论