读书笔记--SQL必知必会--建立练习环境
书目信息
中文名:《SQL必知必会(第4版)》
英文名:《Sams Teach Yourself SQL in 10 Minutes - Fourth Edition》
MySQL
在CentOS7系统中安装MySQL5.7
01-确认系统版本
# uname -a
Linux CentOS-7 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
02-下载MySQL的repo源
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
03-安装yum库
yum localinstall -y mysql57-community-release-el7-7.noarch.rpm
04-安装MySQL
yum install -y mysql-community-server
05-启动MySQL服务
systemctl start mysqld
06-设置MySQL开机启动
systemctl enable mysqld
07-获取初始密码
[root@CentOS-7 ~]# cat /var/log/mysqld.log |grep password
2016-12-15T06:38:55.630758Z 1 [Note] A temporary password is generated for root@localhost: _=pPugkie3*t
08-使用初始密码登录
[root@CentOS-7 ~]# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.7.17
Copyright (c) 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.
09-修改密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Bzbh2017!';
Query OK, 0 rows affected (0.04 sec)
10-设置远程登录
允许root用户使用密码从任何主机连接到mysql服务器
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Bzbh2017!' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> FLUSH PRIVILEGES;
11-查看数据库及表
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.05 sec)
mysql>
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
mysql> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| engine_cost |
| event |
| func |
| general_log |
| gtid_executed |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| server_cost |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
31 rows in set (0.00 sec)
mysql>
12-关闭CentOS7防火墙
CentOS7默认启用firewall作为防火墙,可能会网络不通导致远程登录MySQL失败,这里是简单粗暴地关闭了防火墙。
查看防火墙状态:firewall-cmd --state
启动防火墙:systemctl start firewalld.service
停止防火墙:systemctl stop firewalld.service
禁止防火墙开机启动:systemctl disable firewalld.service
MySQL的主要配置文件
主配置文件: /etc/my.cnf
数据库文件存放位置: /var/lib/mysql/
数据库日志:/var/log/mysqld.log
MySQL数据库默认监听端口:
[root@CentOS-7 ~]# netstat -anp |grep 3306
tcp6 0 0 :::3306 :::* LISTEN 6976/mysqld
在CentOS7系统中安装MariaDB
MariaDB - HomePage
Wiki - MariaDB
MariaDB是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。
开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。
MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。
01-确认系统版本
[root@CentOS-7 ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
02-安装并启动MariaDB
[root@CentOS-7 ~]#yum -y install mariadb-server mariadb-devel
[root@CentOS-7 ~]#systemctl start mariadb.service
[root@CentOS-7 ~]#systemctl enable mariadb.service
03-mysql_secure_installation
[root@CentOS-7 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] n
... skipping.
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
[root@CentOS-7 ~]#
04-开始使用MariaDB
[root@CentOS-7 ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.56-MariaDB MariaDB Server
Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>
建立练习环境
1-创建数据库
mysql> create database sqlbzbh;
Query OK, 1 row affected (0.00 sec)
mysql>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sqlbzbh |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql>
2-选择数据库
mysql> use sqlbzbh;
Database changed
mysql>
mysql> show tables;
Empty set (0.00 sec)
mysql>
3-在选择的数据库中创建表和插入数据
获取脚本并执行:MySQL (and MariaDB) SQL scripts
示例:
mysql> CREATE TABLE Customers
-> (
-> cust_id char(10) NOT NULL ,
-> cust_name char(50) NOT NULL ,
-> cust_address char(50) NULL ,
-> cust_city char(50) NULL ,
-> cust_state char(5) NULL ,
-> cust_zip char(10) NULL ,
-> cust_country char(50) NULL ,
-> cust_contact char(50) NULL ,
-> cust_email char(255) NULL
-> );
Query OK, 0 rows affected (0.08 sec)
mysql>
mysql> INSERT INTO Customers(cust_id, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country, cust_contact, cust_email)
-> VALUES('1000000001', 'Village Toys', '200 Maple Lane', 'Detroit', 'MI', '44444', 'USA', 'John Smith', 'sales@villagetoys.com');
Query OK, 1 row affected (0.00 sec)
mysql>
4-验证表结构和数据
以MySQL为例,命令同样适用于MariaDB。
mysql> use sqlbzbh;
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
mysql>
mysql> show tables;
+-------------------+
| Tables_in_sqlbzbh |
+-------------------+
| Customers |
| OrderItems |
| Orders |
| Products |
| Vendors |
+-------------------+
5 rows in set (0.00 sec)
mysql>
mysql> select * from Vendors;
+---------+-----------------+-----------------+------------+------------+----------+--------------+
| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |
+---------+-----------------+-----------------+------------+------------+----------+--------------+
| BRE02 | Bear Emporium | 500 Park Street | Anytown | OH | 44333 | USA |
| BRS01 | Bears R Us | 123 Main Street | Bear Town | MI | 44444 | USA |
| DLL01 | Doll House Inc. | 555 High Street | Dollsville | CA | 99999 | USA |
| FNG01 | Fun and Games | 42 Galaxy Road | London | NULL | N16 6PS | England |
| FRB01 | Furball Inc. | 1000 5th Avenue | New York | NY | 11111 | USA |
| JTS01 | Jouets et ours | 1 Rue Amusement | Paris | NULL | 45678 | France |
+---------+-----------------+-----------------+------------+------------+----------+--------------+
6 rows in set (0.00 sec)
mysql>
参考信息
CentOS7中的systemctl命令
http://www.cnblogs.com/longrui/p/6071581.html
http://www.cnblogs.com/starof/p/4680083.html
行动是绝望的解药!
欢迎转载和引用,但请在明显处保留原文链接和原作者信息!
本博客内容多为个人工作与学习的记录,少数内容来自于网络并略有修改,已尽力标明原文链接和转载说明。如有冒犯,即刻删除!
以所舍,求所得,有所获,方所成。