linux学习之路--MySQL
第一步:下载mysql
1、对于无网络条件来说,需要安装文件交互软件,xftp 6 或者 MobaXterm,把优盘的里安装包copy进去就行,链接在下方
我是链接:https://pan.baidu.com/s/12ugq0TQSTcjfZey2sWCOkA 我是提取码:ppto
1 |
[[email protected]]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz
|
2、若报-bash: wget: command not found,则表明没有安装wget,需要安装,安装命令如下:
1 |
yum -y install wget |
安装完成即可以使用。
3、检查是否本地已经安装了mysql(|前后带不带空格都行),没有反应就是没有安装
1 |
rpm -qa | grep mysql
|
4、卸载以前的mysql
1 |
rpm -e 已经存在的MySQL全名
|
第二步:解压文件
1 |
[[email protected]]# tar -zxvf mysql-5.7.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/
|
文件名修改为mysql:
1 |
切到解压目录 [[email protected]]#cd /usr/local 查看目录下文件 [[email protected]]#ll [[email protected] local]# mv mysql-5.7.17-linux-glibc2.5-x86_64/ mysql [[email protected] local]#ll
|
第三步:配置启动文件
1、2步对于5.7以后的版本可能会不存在my-default.cnf文件,就不需要进行这两步操作,官网上给出解释是,不需要配置
1.然后去到mysql的support-files目录下,复制my.cnf到 /etc/my.cnf(mysqld启动时自动读取)
1 2 3 |
[[email protected] local]# cd mysql/support-files/ [[email protected] support-files]# cp my-default.cnf /etc/my.cnf cp: overwrite ‘/etc/my.cnf’? yes
|
注意:如果你在安装时Linux虚拟机时同时安装了默认的mysql,此时操作以上步骤,终端将会提示你文件已存在是否覆盖,输入yes覆盖即可。
2.配置数据库编码
1 |
[[email protected] support-files]# vim /etc/my.cnf 按键盘上的【Insert】键编写 |
添加以下内容:
1 2 3 4 5 6 |
[mysql] default-character-set=utf8
[mysqld] default-storage-engine=INNODB character_set_server=utf8
写入后按【Esc】退出编辑模式,然后直接输入【:wq】保存退出 |
3.复制mysql.server到/etc/init.d/目录下(目的想实现开机自动执行效果)
1 |
[[email protected] support-files]# cp mysql.server /etc/init.d/mysql |
4.修改/etc/init.d/mysql参数
1 |
[[email protected] support-files]# vim /etc/init.d/mysql |
修改以下内容:
1 2 |
basedir=/usr/local/mysql datadir=/usr/local/mysql/data
|
5.出于安全便利,创建一个操作数据库的专门用户
建立一个mysql的组:
1 |
[[email protected] support-files]# groupadd mysql |
建立mysql用户,并且把用户放到mysql组:
1 |
[[email protected] support-files]# useradd -r -g mysql mysql |
给mysql用户设置一个密码:
1 |
[[email protected] support-files]# passwd mysql |
给目录/usr/local/mysql 更改拥有者:
1 |
[[email protected] support-files]# chown -R mysql:mysql /usr/local/mysql/ |
第四步:初始化 mysql 的数据库
1.初始化
1 2 |
[[email protected] support-files]# cd /usr/local/mysql/bin/ [[email protected] bin]# ./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data |
初始化后会生成一个临时密码 [email protected]::*(最好先记录这个临时密码)
2.给数据库加密
1 |
[[email protected] bin]# ./mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data |
3.启动mysql
1 |
[[email protected] bin]# ./mysqld_safe --user=mysql & |
PS:如果启动失败则将/etc/my.cnf更改为其它名称即可。
启动:根目录service mysql start
4.检查mysql是否启动
1 |
[[email protected] bin]# ps -ef|grep mysql |
发现有进程便代表启动成功,退出mysql。(exit不好使的话,用 \q )
1 |
mysql>exit |
第五步:进入客户端
1.登录
1 |
[[email protected] bin]# ./mysql -uroot -p |
1 |
Enter password:这里输入之前的临时密码 |
2.修改密码
1 |
mysql> set password=password('新密码'); |
第六步:设置远程访问
1.打开mysql的默认端口3306
1 |
[[email protected] bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent |
提示FirewallD is not running,如下图所示。(ps:图片是在网上找的)
(1)通过systemctl status firewalld查看firewalld状态,发现当前是dead状态,即防火墙未开启。
(2)通过systemctl start firewalld开启防火墙,没有任何提示即开启成功。
(3)再次通过systemctl status firewalld查看firewalld状态,显示running即已开启了。
如果要关闭防火墙设置,可能通过systemctl stop firewalld这条指令来关闭该功能。
打开3306端口正确为:
1 2 3 4 |
[[email protected] bin]# firewall-cmd --zone=public --add-port=3306/tcp --permanent success [[email protected] bin]# firewall-cmd --reload success |
2.设置mysql的远程访问
设置远程访问账号:grant all privileges on . to 远程访问用户名@’%’ identified by ‘用户密码’;
1 |
mysql> grant all privileges on *.* to [email protected]'%' identified by 'root'; |
刷新:
1 |
mysql> flush privileges; |
第七步:设置开机自启动
1.添加服务mysql
1 |
[[email protected] bin]# chkconfig --add mysql |
2.设置mysql服务为自启动
1 |
[[email protected] bin]# chkconfig mysql on |
第八步:配置环境变量
1.打开配置文件
1 |
[[email protected]]# vim /etc/profile |
2.最后一行添加:
1 |
export PATH=$JAVA_HOME/bin:/usr/local/mysql/bin:$PATH |
3.使修改生效:
1 |
[[email protected]]# source /etc/profile |
至此,mysql5.7的安装就完成了!!!
第九步:测试安装的数据库
1.切到MySQL安装的bin文件下
1 |
[[email protected]]# cd usr/local/mysql/bin/ |
2.登录MySQL
1 |
[[email protected] bin]# ./mysql –uroot –p 输入设置好的密码,进去到MySQL里 |
3.查询当前数据库,其中语句的执行结尾要带有分号;
1 |
mysql> show databases; |
4.打开需要的数据库
1 |
mysql> use 数据库名; |
5.查看库表
1 |
mysql> show tables; |
6.创建数据库;
1 |
mysql> create database 数据库名; |
7.导出数据库和导出数据库里其中一张表
1 |
mysql> mysqldump -u root -p dz > /home/数据库名.sql; mysql> mysqldump -u root -p dz 库表名 > /home/数据库表名.sql; |
8.导入数据库
1 |
mysql> source /home/数据库文件.sql |