博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
读书笔记--SQL必知必会--建立练习环境
阅读量:7062 次
发布时间:2019-06-28

本文共 9308 字,大约阅读时间需要 31 分钟。

书目信息

中文名:

英文名:

MySQL

在CentOS7系统中安装MySQL5.7

01-确认系统版本

# uname -aLinux 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 password2016-12-15T06:38:55.630758Z 1 [Note] A temporary password is generated for root@localhost: _=pPugkie3*t

08-使用初始密码登录

[root@CentOS-7 ~]# mysql -u root -pEnter password: Welcome to the MySQL monitor.  Commands end with ; or \g.Your MySQL connection id is 6Server version: 5.7.17Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> 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 3306tcp6       0      0 :::3306                 :::*                    LISTEN      6976/mysqld

在CentOS7系统中安装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_installationNOTE: 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 currentpassword for the root user.  If you've just installed MariaDB, andyou 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 MariaDBroot 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 anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] Y ... Success!Normally, root should only be allowed to connect from 'localhost'.  Thisensures 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 canaccess.  This is also intended only for testing, and should be removedbefore 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 farwill 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 MariaDBinstallation should now be secure.Thanks for using MariaDB![root@CentOS-7 ~]#

04-开始使用MariaDB

[root@CentOS-7 ~]# mysql -u root -pEnter password: Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 8Server version: 5.5.56-MariaDB MariaDB ServerCopyright (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 changedmysql> mysql> show tables;Empty set (0.00 sec)mysql>

3-在选择的数据库中创建表和插入数据

获取脚本并执行:
示例:

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 namesYou can turn off this feature to get a quicker startup with -ADatabase changedmysql> 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>

参考信息

http://www.cnblogs.com/longrui/p/6071581.html
http://www.cnblogs.com/starof/p/4680083.html

转载于:https://www.cnblogs.com/anliven/p/6185231.html

你可能感兴趣的文章
Oracle10g手工创建数据库
查看>>
JS下载文件
查看>>
Nginx 模块常用命令介绍
查看>>
thinkphp5.0框架swoole的使用
查看>>
继上一篇SQL练习题,给出答案
查看>>
慕课网-Java从零打造企业级电商项目实战_项目初始化_项目结构
查看>>
Esper学习笔记二:进程模型
查看>>
Linux环境PHP7.0安装
查看>>
Reactor 响应式编程
查看>>
Dubbo多注册中心和Zookeeper服务的迁移
查看>>
Linux网络相关、firewalld和netfilter
查看>>
linux基础(day30)
查看>>
四周第五次课(11月10日) 6.5 zip压缩工具 6.6 tar打包 6.7 打包并压缩
查看>>
财务管理后台(前台页面)
查看>>
解决hash冲突的4种方法
查看>>
Kafka简介及安装配置
查看>>
Redis——HyperLogLog
查看>>
市场分享竞品分析
查看>>
科技兴国园区兴城——2019国际高科技产业园区博览会在深盛装开幕
查看>>
bash的字符处理工具及bash特性
查看>>