【数据库MySQL】练习---数据库的源码安装和简单表的操作

1、使用源码安装MySQL 5.7

1安装开发工具和开发包

[root@bogon ~]#yum install make gcc gcc-c++ cmake bison-devel ncurses-devel -y
[root@bogon ~]#yum install libaio libaio-devel openssl-devel -y
[root@bogon ~]#yum install perl-Data-Dumper -y
[root@bogon ~]#yum install net-tools -y 

(2)安装MySQL

(3)解压

[root@bogon~mysql-5.7.14]# tar xf mysql-boost-5.7.14.tar.gz -C /usr/local/src/

(4)检查

[root@bogon ~ mysql-5.7.14]# cd /usr/local/src/mysql-5.7.14/
[root@bogon ~ mysql-5.7.14]# cmake . -DWITH_BOOST=/usr/local/src/mysql-5.7.14/boost/boost_1_59_0/

(5)编译

[root@bogon ~ mysql-5.7.14]# make

(6)安装

[root@bogon ~ mysql-5.7.14]# make install

(7)创建用户和组

[root@bogon ~]# useradd -r -u 333 -g 36 -c 'MySQL Server' mysql
[root@bogon ~]# groupadd -r -g 333 mysql

(8)初始化

[root@bogon local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/date

(9)提供配置文件和脚本

#提供配置文件
[root@bogon ~]# \cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
修改:datadir
[root@bogon ~]# vim /etc/my.cnf
datadir = /usr/local/mysql/data
#提供服务脚本
[root@bogon ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

(10)启动服务

#添加到系统服务
[root@bogon ~]# chkconfig --add mysqld
#启动服务
[root@bogon ~]# systemctl start mysqld
#开机自启动
[root@bogon ~]# chkconfig mysqld on

(11)添加环境变量

#添加环境变量
[root@bogon ~]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@bogon ~]# 加载到当前
source /etc/profile.d/mysql.sh

(12)登录数据库

[root@bogon ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.14

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

2、创建数据库school,字符集为utf8

mysql> create database if not exists school character set utf8;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

3、在school数据库中创建Student和Score表 

     

(1)应用school数据库

mysql> use school;                       
Database changed
mysql> select database();
+------------+
| database() |
+------------+
| school     |
+------------+
1 row in set (0.00 sec)

(2)创建student表

mysql> create table student(
    -> ID int(10) primary key unique auto_increment comment '学号',
    -> Name varchar(20) not null comment '姓名',
    -> Sex enum('M','F') comment '性别',
    -> Birth year comment '出生年份',
    -> Department varchar(20) not null comment '院系',
    -> Address varchar(50)  comment '家庭住址'
    -> )comment = '学生信息表';
Query OK, 0 rows affected (0.01 sec)

(3)创建score表

mysql> create table score(
    -> ID int(10) primary key unique auto_increment comment '编号',
    -> Stu_id int(10) not null comment '学号',
    -> C_name varchar(10) comment '课程名',
    -> Grade int(10) comment '分数'
    -> )comment = '成绩信息表';
Query OK, 0 rows affected (0.01 sec)

(4)查看school中创建的表

(5)查看表的结构

    

4、授权用户tom,密码mysql,能够从任何地方登录并管理数据库school

(1)关闭mysql服务

[root@bogon ~]# systemctl stop mysqld

(2)修改配置文件

vim /etc/my.cnf
添加: validate_password=off

(3)重新启动mysqld服务

[root@bogon ~]# systemctl restart mysqld

(4)登录mysql

[root@bogon ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> grant select on *.* to 'tom'@'%' identified by 'mysql';
Query OK, 0 rows affected, 1 warning (0.00 sec)

 (5)客户端用tom身份登录数据库

[root@bogon ~]# mysql -utom -p'mysql' -h192.168.74.135
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql>


5、使用mysql客户端登录服务器,重置root密码

(1)关闭服务

[root@bogon ~]# systemctl stop mysqld

(2)修改配置文件

[root@bogon ~]# vim /etc/my.cnf

(3)重启服务

[root@bogon ~]# systemctl start mysqld

(4)空密码登录数据库并修改密码

[root@bogon ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> update mysql.user 
    -> set authentication_string=password('abcABC123!')
    -> where user='root' and host='localhost';
Query OK, 1 row affected, 1 warning (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> \q
Bye

(5)重新登录

[root@bogon ~]# mysql -uroot -p'123abcABC!'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

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

mysql> 

猜你喜欢

转载自blog.csdn.net/trichloromethane/article/details/112583669