3.3.2 mysql安装
mysql的安装方法很多,最简单的而且最适用就是二进制方法安装。接下来我们使用mysql的二进制包来进行安装部署mysql
3.3.2.1 解压软件
解压软件
tar xf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz -C /data/server/
cd /data/server/
ln -s mysql-5.6.35-linux-glibc2.5-x86_64 mysql
3.3.2.2 基本用户
创建专用用户
useradd -s /sbin/nologin -M mysql
3.3.2.3 初始化
初始化mysql数据库
/data/server/mysql/scripts/mysql_install_db --basedir=/data/server/mysql --datadir=/data/server/mysql/data/ --user=mysql
3.3.2.4 相关配置文件
数据库配置文件管理
mv /etc/my.cnf /etc/my.cnf-bak
cp /data/server/mysql/support-files/my-default.cnf /etc/my.cnf
数据库启动命令配置
cp /data/server/mysql/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
启动文件修改
sed -i ‘s#/usr/local/mysql#/data/server/mysql#g’ /data/server/mysql/bin/mysqld_safe /etc/init.d/mysqld
数据库文件权限管理
chown -R mysql.mysql /data/server/mysql/
3.3.2.5 启动数据库
启动数据库前检查
netstat -tnulp | grep mysql
启动数据库
/etc/init.d/mysqld start
检查数据库启动状态
netstat -tnulp|grep mysqld
配置环境变量
vim /etc/profile
末尾添加这条配置
PATH=/data/server/mysql/bin:$PATH
让配置文件生效
source /etc/profile
测试进入数据库
[root@localhost server]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.35 MySQL Community Server (GPL)
Copyright © 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>
设置开机自启动服务控制脚本
-
将mysqld服务加入到系统服务并且开机自启动
[root@localhost mysql]# chkconfig --add mysqld -
启动mysqld服务
[root@localhost mysql]# systemctl start mysqld -
查看mysqld服务状态
[root@localhost mysql]# systemctl status mysqld
将mysql服务设置为开机自启动服务
chkconfig --add mysqld
chkconfig mysqld on
- 把MySQL服务加入到防火墙例外规则
[root@localhost mysql]# firewall-cmd --add-service=mysql --zone=public --permanent
[root@localhost mysql]# firewall-cmd --reload
3.3.3 mysql简单操作
3.3.3.1 登录及基本信息查询
设置mysql数据库登录用户名与密码
前文有提到过登录数据库需要输入用户名和密码,此时我们就设置一个密码
[root@localhost server]# mysqladmin -u root password ‘123456’
Warning: Using a password on the command line interface can be insecure.
测试一下,密码是否设置成功
[root@localhost server]# mysqladmin -u root password ‘123456’
Warning: Using a password on the command line interface can be insecure.
[root@localhost server]# mysql -u root -p123456
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.6.35 MySQL Community Server (GPL)
Copyright © 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 ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;
说明:
出现这个页面,表示用户和密码设置成功,可以正常登录
查询默认的数据库信息
show databases; <— 查询默认的数据库信息
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
4 rows in set (0.00 sec)
选择使用一个数据库
use mysql;
<— 表示选择使用一个数据库,相当于cd进入一个数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
查看当前所在数据库
select database();
<— 表示查看当前所在数据库,类似于pwd命令的功能
mysql> select database();
±-----------+
| database() |
±-----------+
| mysql |
±-----------+
1 row in set (0.00 sec)
查看当前登录数据库的用户
select user();
<— 查看当前登录数据库的用户,类似于whoami命令并且mysql还可以限制指定用户可以从哪里进行连接登录数据库
mysql> select user();
±---------------+
| user() |
±---------------+
| root@localhost |
±---------------+
1 row in set (0.00 sec)
查看可以登录mysql数据库的目录
select user,host from mysql.user;
—查看可以登录mysql数据库的目录,以及都可以从哪里进行管理mysql数据库
mysql> select user,host from mysql.user;
±-----±----------------------+
| user | host |
±-----±----------------------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
±-----±----------------------+
6 rows in set (0.00 sec)
3.3.3.2 创建及删除数据库
创建一个数据库
create database 数据库名称; ===>可以创建一个数据库,通过show databases;查看
mysql> create database iwebshop;
Query OK, 1 row affected (0.05 sec)
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| iwebshop |
±-------------------+
5 rows in set (0.00 sec)
对指定数据库创建用户
创建一个数据库的用户:
GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;
flush privileges;
mysql> grant all on iwebshop.* to ‘iwebshop’@‘localhost’ identified by ‘123456’;
Query OK, 0 rows affected (0.18 sec)
#创建一个用户wordpress,权限为全部,并且设置允许登录的网段,和最后设置密码
创建用户后,更新一下数据库的信息flush privileges;
mysql> flush privileges;
Query OK, 0 rows affected (0.32 sec)
#flush privileges 创建完用户后,更新一下数据库的信息
查看数据库的用户信息select user,host from mysql.user;
mysql> select user,host from mysql.user;
±---------±----------------------+
| user | host |
±---------±----------------------+
| root | 127.0.0.1 |
| root | ::1 |
| | localhost |
| iwebshop | localhost |
| root | localhost |
| | localhost.localdomain |
| root | localhost.localdomain |
±---------±----------------------+
7 rows in set (0.01 sec)
#查看数据库内的用户信息,和对应可以登录的主机
删除一个数据库
drop database 数据库; ===>可以删除一个数据库,通过show databases;查看
mysql> drop database iwebshop;
Query OK, 0 rows affected (0.02 sec)
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
±-------------------+
4 rows in set (0.00 sec)