0x00 教程内容
- Mysql的安装与启动
- Mysql的使用
0x01 Mysql的安装与启动
1. 安装Mysql
a. 切换到root用户,用yum源安装
yum install mysql-server
但可能报下面的错:
b. 所以我们要先下载包
点击链接会下载rpm文件,然后再上传到服务器上:
http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
如果你的服务器安装了wget,也可以直接在服务器下载下来:
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
然后安装rpm文件(需要root用户):
rpm -ivh mysql-community-release-el7-5.noarch.rpm
c. 重新用yum源安装
yum install -y mysql-server
2. 启动Mysql
a. 启动Mysql服务
service mysqld start
b. 查看Mysql的状态
service mysqld status
c. 设置Mysql的root用户密码为root:
mysqladmin -u root password 'root'
d. 登录(执行后手动输入密码:root)
mysql -uroot -p
e. 设置使得其他节点可以通过root账号连上Mysql服务:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
刷新权限:
flush privileges;
0x02 Mysql的使用
1. 数据库操作
a. 查看数据库列表
show databases;
b. 使用某数据库(mysql)
use mysql;
c. 创建数据库(sny)
create database sny;
2. 表操作
a. 查看所有表
show tables;
b. 查看某张表内容(user表)
select * from user;
PS:美观地查看:
select * from user\G;
3. 执行sql文件
a. 在~/scripts/sql新建文件(init_movie.sql):
CREATE DATABASE IF NOT EXISTS movie; USE movie; DROP TABLE IF EXISTS movie; CREATE TABLE movie ( id INT, title VARCHAR(128), release_date DATE, video_release_date DATE, imdb_url VARCHAR(1024) );
b. 在~/scripts/sql
新建文件(movie.sql):
USE movie; insert into movie values(1, "Toy Story (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Toy%20Story%20(1995)"); insert into movie values(2, "GoldenEye (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?GoldenEye%20(1995)"); insert into movie values(3, "Four Rooms (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Four%20Rooms%20(1995)"); insert into movie values(4, "Get Shorty (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Get%20Shorty%20(1995)"); insert into movie values(5, "Copycat (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Copycat%20(1995)"); insert into movie values(6, "Shanghai Triad (Yao a yao yao dao waipo qiao) (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/Title?Yao+a+yao+yao+dao+waipo+qiao+(1995)"); insert into movie values(7, "Twelve Monkeys (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Twelve%20Monkeys%20(1995)"); insert into movie values(8, "Babe (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Babe%20(1995)"); insert into movie values(9, "Dead Man Walking (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Dead%20Man%20Walking%20(1995)"); insert into movie values(10, "Richard III (1995)", STR_TO_DATE("22-Jan-1996", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Richard%20III%20(1995)"); insert into movie values(11, "Seven (Se7en) (1995)", STR_TO_DATE("01-Jan-1995", "%d-%M-%Y"), null, "http://us.imdb.com/M/title-exact?Se7en%20(1995)");
c. 执行:
source ~/init_movie.sql
source ~/movie.sql
d. 结果就是新建了movie数据库,并且有一张movie表,里面也有相应的数据。
0xFF 总结
- 本操作,要求服务器能连上外网,不然下载不了依赖。
- 注意:如果服务器关机的话,Mysql服务是没有自启动的,请自行查资料,设置Mysql成自启动。