CentOS 7 MySQL安装和简单使用

MySQL在CentOS 7不香了

CentOS 7开始,就不再使用MySQL作为默认数据库而是转向MariaDB,MariaDB大部分源自MySQL6.0,据说其性能要优于MySQL。且不管新欢MariaDB香不香,如果你就喜欢旧爱MySQL,要在CentOS 7安装它即可参照下文。
另外,本文针对非root用户,但是需要在sudoer列表中,即拥有sudo权限。

升级系统

  • 查看一下系统版本,至少要确定自己的确实是CentOS 系统
lsb_release -a #查看系统版本

在这里插入图片描述

  • 升级所有包或系统
    升级系统所有包
sudo yum upgrade #只升级所有包,不升级软件和系统内核

在升级包的同时,也可同时升级系统内核,但是绝不建议在生产环境随便这么干,很显然,生产环境对软件版本和系统内核的要求非常精确

sudo yum update #升级系统,升级所有包同时也升级软件和系统内核

这一步视网络情况,若更新比较慢,可使用ctrl + Z暂停任务,jobs查询其任务号,使用bg + 任务号放在后台运行

  • 安装wget
yum install wget

安装MySQL

  • 1.下载安装包
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm #下载mysql 安装包
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm #使用rpm安装
  • 2.安装并启动mysql服务,在安装过程中会询问.rpm 文件的GPG 验证,输入y回车
sudo yum install mysql-server #安装mysql服务
sudo systemctl start mysqld #启动mysql服务
systemctl status mysql #查看myslq状态

在这里插入图片描述
可以看到当前mysql服务为激活状态

增强MySQL

为了安全,可以执行

sudo mysql_secure_installation

相关设置主要包括:

  • 设置MySQL root用户密码,第一次的按回车表示空密码,再设置密码,两次确认
  • 是否移除匿名用户
  • 是否禁止root用户的远程连接
  • 是否移除test数据库
    以上选项建议选择yes,确保数据库使用安全、
    同时注意,若安装的是MySQL 5.7,需要一个已经缓存好的密码,可以在/var/log/mysql.log中找到:
sudo grep 'temporary password' /var/log/mysqld.log

简单使用

mysql-server自带的客户端mysql可用于命令行操作

  • 以root用户登陆mysql,密码为上一步mysql_secure_installation增强设置的密码
mysql -u root -p #-p 表示密码,回车之后输入,不可见

输入成功后进入如下

mysql>          #前面是提示符
  • 查看帮助
mysql>  help; #必须以分号结尾,或\h则不必写分号

所有的命令选项

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
?         (\?) Synonym for `help'.
clear     (\c) Clear command.
connect   (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter. NOTE: Takes the rest of the line as new delimiter.
edit      (\e) Edit command with $EDITOR.
ego       (\G) Send command to mysql server, display result vertically.
exit      (\q) Exit mysql. Same as quit.
go        (\g) Send command to mysql server.
help      (\h) Display this help.
nopager   (\n) Disable pager, print to stdout.
notee     (\t) Don't write into outfile.
pager     (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print     (\p) Print current command.
prompt    (\R) Change your mysql prompt.
quit      (\q) Quit mysql.
rehash    (\#) Rebuild completion hash.
source    (\.) Execute an SQL script file. Takes a file name as an argument.
status    (\s) Get status information from the server.
system    (\!) Execute a system shell command.
tee       (\T) Set outfile [to_outfile]. Append everything into given outfile.
use       (\u) Use another database. Takes database name as argument.
charset   (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings  (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'
  • 创建一个新的MySQL用户

下面的例子中,testdb是新创建的数据库名称,testuser是用户名,字符串password即是登陆密码

create database testdb;
create user 'testuser'@'localhost' identified by 'password';
grant all on testdb.* to 'testuser' identified by 'password';
exit;   #退出客户端
  • 创建一个简单的数据表
    testuser身份登陆
mysql -u testuser -p

下面的例子首先进入testdb,然后在其中创建一个名为customers的表,customer_id设为自增整型的主键,另外两个字段保存姓和名,类型为TEXT

use testdb;
create table customers (customer_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, first_name TEXT, last_name TEXT);

*重设root密码
若忘了root密码,可通过如下方法重设:
1.停止mysql-server, 并以不询问密码的方式重启

sudo systemctl stop mysqld
sudo mysqld_safe --skip-grant-tables &

2.重新以root身份连接MySQL服务

mysql -u root
# 使用如下命令重设密码
use mysql; #使用内置mysql数据库
update user SET PASSWORD=PASSWORD("password") WHERE USER='root'; #括号中为重设的密码
flush privileges; #刷新缓存
exit

3.重启mysql服务

sudo systemctl start mysqld

参考

https://www.linode.com/docs/databases/mysql/how-to-install-mysql-on-centos-7/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值