MySQL 第二次练习(源码安装、数据库和表的建立)

1、使用源码安装MySQL 5.7。 

安装依赖包和开发包:

[root@localhost ~]# yum install make gcc gcc-c++ cmake bison-devel ncurses-devel -y
[root@localhost mysql-5.7.36]# yum install  openssl-devel -y

 上传从官网下载的源码到虚拟机:

[root@localhost ~]# ls
anaconda-ks.cfg  mysql-boost-5.7.36.tar.gz

创建组和用户:

[root@localhost ~]# groupadd -r mysql
[root@localhost ~]# useradd -r -g mysql -s /bin/false mysql

 解压:

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

 编译安装:

[root@localhost ~]# cd /usr/local/src/mysql-5.7.36/
[root@localhost mysql-5.7.36]# mkdir -p /data/mysql
[root@localhost mysql-5.7.36]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DWITH_BOOST=/usr/local/src/mysql-5.7.36/boost
[root@localhost mysql-5.7.36]# make
[root@localhost mysql-5.7.36]# make install

 为MySQL提供一个服务脚本

[root@localhost mysql-5.7.36]# cp /usr/local/mysql/support-files/mysql.server  /etc/init.d/
[root@localhost mysql-5.7.36]# chkconfig --add mysql.server  //添加为系统服务
[root@localhost mysql-5.7.36]# chkconfig  mysql.server on //设置开机自启动

 设置环境变量:

[root@localhost mysql-5.7.36]# vi /etc/profile.d/mysql.sh
添加如下:
export PATH=$PATH:/usr/local/mysql/bin

[root@localhost mysql-5.7.36]# source /etc/profile.d/mysql.sh

初始化:

[root@localhost mysql-5.7.36]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql

修改:

[root@localhost mysql-5.7.36]# vi /etc/my.cnf
datadir=/data/mysql 
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/data/mysql/mysqld.pid

启动服务:

[root@localhost mysql-5.7.36]# systemctl start mysql.server

 临时登录:

[root@localhost mysql-5.7.36]# mysql -uroot -p'f0&%#l_6fUn!'
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.36

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

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 school;
Query OK, 1 row affected (0.00 sec)

mysql> show create database school;
+----------+-------------------------------------------------------------------+
| Database | Create Database                                                   |
+----------+-------------------------------------------------------------------+
| school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+-------------------------------------------------------------------+
1 row in set (0.00 sec)


mysql> \q
Bye

 修改字符集为UTF8


mysql> alter database school character set utf8;
Query OK, 1 row affected (0.00 sec)

mysql> show create database school;
+----------+-----------------------------------------------------------------+
| Database | Create Database                                                 |
+----------+-----------------------------------------------------------------+
| school   | CREATE DATABASE `school` /*!40100 DEFAULT CHARACTER SET utf8 */ |
+----------+-----------------------------------------------------------------+
1 row in set (0.00 sec)

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

Student表定义.jpg

mysql> use school
Database changed
mysql> create table Student
    -> (Id int primary key auto_increment
    -> comment '学号',
    -> Name varchar(20) not null
    -> comment '姓名',
    -> Sex varchar(4) 
    -> comment '性别',
    -> Birth year 
    -> comment '出生年份',
    -> Department varchar(20) not null
    -> comment '院系',
    -> Address varchar(50)
    -> comment '家庭住址');
mysql> desc Student;
+------------+-------------+------+-----+---------+----------------+
| Field      | Type        | Null | Key | Default | Extra          |
+------------+-------------+------+-----+---------+----------------+
| Id         | int(11)     | NO   | PRI | NULL    | auto_increment |
| Name       | varchar(20) | YES  |     | NULL    |                |
| Sex        | varchar(4)  | YES  |     | NULL    |                |
| Birth      | year(4)     | YES  |     | NULL    |                |
| Department | varchar(20) | NO   |     | NULL    |                |
| Address    | varchar(50) | YES  |     | NULL    |                |
+------------+-------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

Score表定义.jpg

mysql> create table Score
    -> (Id int primary key auto_increment comment '编号',
    -> Stu_id int not null comment '学号',
    -> C_name varchar(20) comment '课程名',
    -> Grade int comment '分数');
Query OK, 0 rows affected (0.00 sec)

mysql> desc Score;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| Id     | int(11)     | NO   | PRI | NULL    | auto_increment |
| Stu_id | int(11)     | NO   |     | NULL    |                |
| C_name | varchar(20) | YES  |     | NULL    |                |
| Grade  | int(11)     | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
4 rows in set (0.01 sec)

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

mysql> create user 'tom'@'%' identified by 'MySQL@123';
Query OK, 0 rows affected (0.20 sec)

mysql> grant all on school to tom@'%';
Query OK, 0 rows affected (0.55 sec)

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


[root@localhost mysql]# mysql -uroot -p'f0&%#l_6fUn!'
mysql> alter user root@localhost identified by 'MySQL@123';
Query OK, 0 rows affected (0.00 sec)
mysql> \q
Bye
 
 
[root@localhost ~]# mysql -uroot -p'MySQL@123'
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 21
Server version: 5.7.36 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
 
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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个F啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值