1、使用rpm安装mysql
安装环境
OS: centos 7.5
MYSQL: mysql5.7
- 因为系统自带的mariadb的mariadb-libs-1:5.5.56-2.el7.x86_64包和mysql冲突,所以先移除此包
rpm -e mariadb-libs-1:5.5.56-2.el7.x86_64 --nodeps
- 安装依赖的2个包,有先后顺序
yum install mysql-community-common-5.7.26-1.el7.x86_64.rpm
yum install mysql-community-libs-5.7.26-1.el7.x86_64.rpm
- 安装mysql客户端工具
yum install mysql-community-client-5.7.26-1.el7.x86_64.rpm
- 安装mysql server
yum install mysql-community-server-5.7.26-1.el7.x86_64.rpm
- 启动mysql
systemctl start mysqld.service
- 找出mysql的密码
cat /var/log/mysqld.log |grep password
- 使用默认密码进入mysql后,必须修改密码后才能使用
alter user root@'localhost' identified by '123456';
2、二进制安装mariadb
- 下载二进制文件,并解压,建立符号连接
tar xvf mariadb-10.3.15-linux-x86_64.tar.gz -C /usr/local/
ln -sv mariadb-10.3.15-linux-x86_64 mysql
- 建立mysql账号和组
useradd -r -d /data/mysql mysql
- 建立mysql数据库文件存放的目录,并设置好权限
mkdir /data/mysql -p
chown -R mysql.mysql /data/mysql/
- 修改mysql配置文件
[root@localhost my.cnf.d]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
[mysqld_safe]
log-error=/data/mysql/mariadb.log
pid-file=/data/mysql/mariadb.pid
- 创建数据库文件
cd /usr/local/mysql/
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
- 添加启动服务,并启动服务
cp ./support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
- 修改PATH变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/sql.sh
. /etc/profile.d/sql.sh
- 安全初始化
/user/local/mysql/bin/mysql_secure_installation
3、编译安装mariadb
- 建立mysql账号和组
useradd -r -d /data/mysql mysql
- 建立mysql数据库文件存放的目录,并设置好权限
mkdir /data/mysql -p
chown -R mysql.mysql /data/mysql/
- 安装相关依赖包
yum install -y 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
- 解压源码包
tar xvf mariadb-10.2.18.tar.gz
- cmake编译安装
cd mariadb-10.2.18/
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/data/mysql/ \
-DSYSCONFDIR=/etc \
-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 -j 8 && make install
- 修改mysql配置文件
[root@localhost my.cnf.d]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
[mysqld_safe]
log-error=/data/mysql/mariadb.log
pid-file=/data/mysql/mariadb.pid
- 生成数据库文件
cd /usr/local/mysql
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql
- 准备PATH变量
echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
- 添加启动服务,并启动服务
cp ./support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
service mysqld start
- 运行安全脚本
mysql_secure_installation
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 |
+—-+——–+——-+——-+——+——-+
1)在students表中,查询年龄大于25岁,且为男性的同学的名字和年龄
select name,score from students where age > 25 and swx='f';
2)以ClassID为分组依据,显示每组的平均年龄
select class,avg(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 * from students where name like 'l%';