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
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;
a. 查看数据库列表
show databases;
b. 使用某数据库(mysql)
use mysql;
c. 创建数据库(sny)
create database sny;
a. 查看所有表
show tables;
b. 查看某张表内容(user表)
select * from user;
PS:美观地查看:
select * from user\G;
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表,里面也有相应的数据。
作者简介:邵奈一
大学大数据讲师、大学市场洞察者、专栏编辑
公众号、微博、CSDN:邵奈一
邵奈一的技术博客导航