码迷,mamicode.com
首页 > 数据库 > 详细

01-MySQL安装、配置和使用

时间:2016-05-17 13:02:44      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

1、MySQL基础                                                 

MySQL是一个关系型数据库管理系统,由瑞典MySQL AB公司开发,目前属于Oracle公司。MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性。
  • Mysql是开源的,所以你不需要支付额外的费用。
  • Mysql支持大型的数据库。可以处理拥有上千万条记录的大型数据库。
  • MySQL使用标准的SQL数据语言形式。
  • Mysql可以允许运行于多个系统上,并且支持多种编程语言。这些编程语言包括C、C++、Python、Java、Perl、PHP、Eiffel、Ruby和Tcl等。
  • Mysql对PHP有很好的支持,PHP是目前最流行的Web开发语言。
  • MySQL支持大型数据库,支持5000万条记录的数据仓库,32位系统表文件最大可支持4GB,64位系统支持最大的表文件为8TB。
  • Mysql是可以定制的,采用了GPL协议,你可以修改源码来开发自己的Mysql系统。

2、安装MySQL                                                
MySQL安装方式(Windows):
    1)MSI安装(Windows Installer)
    下载安装包:http://dev.mysql.com/downloads/mysql/  并执行安装程序;我这里下载的是mysql-5.5.49-winx64.msi
     安装类型:
技术分享
 
技术分享
 
   2) ZIP安装(以后补充)
 
    3、配置MySQL                                       
MSI安装的目录:C:\Program Files\MySQL
运行MySQL配置向导文件:
技术分享
选择配置类型:
技术分享
 
技术分享
设置root用户的密码:
技术分享
技术分享
 
MySQL的目录结构:
技术分享
修改MySQL配置文件my.ini(文件所在的位置C:\Program Files\MySQL\MySQL Server 5.5):
注意:如果遇到无法修改的问题,请使用管理员身份修改。
 
  1. # MySQL Server Instance Configuration File
  2. # ----------------------------------------------------------------------
  3. # Generated by the MySQL Server Instance Configuration Wizard
  4. #
  5. #
  6. # Installation Instructions
  7. # ----------------------------------------------------------------------
  8. #
  9. # On Linux you can copy this file to /etc/my.cnf to set global options,
  10. # mysql-data-dir/my.cnf to set server-specific options
  11. # (@localstatedir@ for this installation) or to
  12. # ~/.my.cnf to set user-specific options.
  13. #
  14. # On Windows you should keep this file in the installation directory
  15. # of your server (e.g. C:\Program Files\MySQL\MySQL Server X.Y). To
  16. # make sure the server reads the config file use the startup option
  17. # "--defaults-file".
  18. #
  19. # To run run the server from the command line, execute this in a
  20. # command line shell, e.g.
  21. # mysqld --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
  22. #
  23. # To install the server as a Windows service manually, execute this in a
  24. # command line shell, e.g.
  25. # mysqld --install MySQLXY --defaults-file="C:\Program Files\MySQL\MySQL Server X.Y\my.ini"
  26. #
  27. # And then execute this in a command line shell to start the server, e.g.
  28. # net start MySQLXY
  29. #
  30. #
  31. # Guildlines for editing this file
  32. # ----------------------------------------------------------------------
  33. #
  34. # In this file, you can use all long options that the program supports.
  35. # If you want to know the options a program supports, start the program
  36. # with the "--help" option.
  37. #
  38. # More detailed information about the individual options can also be
  39. # found in the manual.
  40. #
  41. #
  42. # CLIENT SECTION
  43. # ----------------------------------------------------------------------
  44. #
  45. # The following options will be read by MySQL client applications.
  46. # Note that only client applications shipped by MySQL are guaranteed
  47. # to read this section. If you want your own MySQL client program to
  48. # honor these values, you need to specify it as an option during the
  49. # MySQL client library initialization.
  50. #
  51. [client] //MySQL客户端
  52. port=3306 //MySQL的默认端口号
  53. [mysql]
  54. default-character-set=latin1 //latinl为MySQL客户端默认编码方式,将其改为utf8编码方式。
  55. # SERVER SECTION
  56. # ----------------------------------------------------------------------
  57. #
  58. # The following options will be read by the MySQL Server. Make sure that
  59. # you have installed the server correctly (see above) so it reads this
  60. # file.
  61. #
  62. [mysqld] //主要用于MySQL服务器端的配置
  63. # The TCP/IP Port the MySQL Server will listen on
  64. port=3306
  65. #Path to installation directory. All paths are usually resolved relative to this.
  66. basedir="C:/Program Files/MySQL/MySQL Server 5.5/" //基础安装目录
  67. #Path to the database root
  68. datadir="C:/ProgramData/MySQL/MySQL Server 5.5/Data/" //数据文件存储目录
  69. # The default character set that will be used when a new schema or table is
  70. # created and no character set is defined
  71. character-set-server=latin1 //latin1默认为当前服务器上所存储的编码方式;将其修改成utf8
  72. # The default storage engine that will be used when create new tables when
  73. default-storage-engine=INNODB
  74. # Set the SQL mode to strict
  75. sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
  76. # The maximum amount of concurrent sessions the MySQL server will
  77. # allow. One of these connections will be reserved for a user with
  78. # SUPER privileges to allow the administrator to login even if the
  79. # connection limit has been reached.
  80. max_connections=100
  81. # Query cache is used to cache SELECT results and later return them
  82. # without actual executing the same query once again. Having the query
  83. # cache enabled may result in significant speed improvements, if your
  84. # have a lot of identical queries and rarely changing tables. See the
  85. # "Qcache_lowmem_prunes" status variable to check if the current value
  86. # is high enough for your load.
  87. # Note: In case your tables change very often or if your queries are
  88. # textually different every time, the query cache may result in a
  89. # slowdown instead of a performance improvement.
  90. query_cache_size=0
  91. # The number of open tables for all threads. Increasing this value
  92. # increases the number of file descriptors that mysqld requires.
  93. # Therefore you have to make sure to set the amount of open files
  94. # allowed to at least 4096 in the variable "open-files-limit" in
  95. # section [mysqld_safe]
  96. table_cache=256
  97. # Maximum size for internal (in-memory) temporary tables. If a table
  98. # grows larger than this value, it is automatically converted to disk
  99. # based table This limitation is for a single table. There can be many
  100. # of them.
  101. tmp_table_size=18M
  102. # How many threads we should keep in a cache for reuse. When a client
  103. # disconnects, the client‘s threads are put in the cache if there aren‘t
  104. # more than thread_cache_size threads from before. This greatly reduces
  105. # the amount of thread creations needed if you have a lot of new
  106. # connections. (Normally this doesn‘t give a notable performance
  107. # improvement if you have a good thread implementation.)
  108. thread_cache_size=8
  109. #*** MyISAM Specific options
  110. # The maximum size of the temporary file MySQL is allowed to use while
  111. # recreating the index (during REPAIR, ALTER TABLE or LOAD DATA INFILE.
  112. # If the file-size would be bigger than this, the index will be created
  113. # through the key cache (which is slower).
  114. myisam_max_sort_file_size=100G
  115. # If the temporary file used for fast index creation would be bigger
  116. # than using the key cache by the amount specified here, then prefer the
  117. # key cache method. This is mainly used to force long character keys in
  118. # large tables to use the slower key cache method to create the index.
  119. myisam_sort_buffer_size=35M
  120. # Size of the Key Buffer, used to cache index blocks for MyISAM tables.
  121. # Do not set it larger than 30% of your available memory, as some memory
  122. # is also required by the OS to cache rows. Even if you‘re not using
  123. # MyISAM tables, you should still set it to 8-64M as it will also be
  124. # used for internal temporary disk tables.
  125. key_buffer_size=25M
  126. # Size of the buffer used for doing full table scans of MyISAM tables.
  127. # Allocated per thread, if a full scan is needed.
  128. read_buffer_size=64K
  129. read_rnd_buffer_size=256K
  130. # This buffer is allocated when MySQL needs to rebuild the index in
  131. # REPAIR, OPTIMZE, ALTER table statements as well as in LOAD DATA INFILE
  132. # into an empty table. It is allocated per thread so be careful with
  133. # large settings.
  134. sort_buffer_size=256K
  135. #*** INNODB Specific options ***
  136. # Use this option if you have a MySQL server with InnoDB support enabled
  137. # but you do not plan to use it. This will save memory and disk space
  138. # and speed up some things.
  139. #skip-innodb
  140. # Additional memory pool that is used by InnoDB to store metadata
  141. # information. If InnoDB requires more memory for this purpose it will
  142. # start to allocate it from the OS. As this is fast enough on most
  143. # recent operating systems, you normally do not need to change this
  144. # value. SHOW INNODB STATUS will display the current amount used.
  145. innodb_additional_mem_pool_size=2M
  146. # If set to 1, InnoDB will flush (fsync) the transaction logs to the
  147. # disk at each commit, which offers full ACID behavior. If you are
  148. # willing to compromise this safety, and you are running small
  149. # transactions, you may set this to 0 or 2 to reduce disk I/O to the
  150. # logs. Value 0 means that the log is only written to the log file and
  151. # the log file flushed to disk approximately once per second. Value 2
  152. # means the log is written to the log file at each commit, but the log
  153. # file is only flushed to disk approximately once per second.
  154. innodb_flush_log_at_trx_commit=1
  155. # The size of the buffer InnoDB uses for buffering log data. As soon as
  156. # it is full, InnoDB will have to flush it to disk. As it is flushed
  157. # once per second anyway, it does not make sense to have it very large
  158. # (even with long transactions).
  159. innodb_log_buffer_size=1M
  160. # InnoDB, unlike MyISAM, uses a buffer pool to cache both indexes and
  161. # row data. The bigger you set this the less disk I/O is needed to
  162. # access data in tables. On a dedicated database server you may set this
  163. # parameter up to 80% of the machine physical memory size. Do not set it
  164. # too large, though, because competition of the physical memory may
  165. # cause paging in the operating system. Note that on 32bit systems you
  166. # might be limited to 2-3.5G of user level memory per process, so do not
  167. # set it too high.
  168. innodb_buffer_pool_size=47M
  169. # Size of each log file in a log group. You should set the combined size
  170. # of log files to about 25%-100% of your buffer pool size to avoid
  171. # unneeded buffer pool flush activity on log file overwrite. However,
  172. # note that a larger logfile size will increase the time needed for the
  173. # recovery process.
  174. innodb_log_file_size=24M
  175. # Number of threads allowed inside the InnoDB kernel. The optimal value
  176. # depends highly on the application, hardware as well as the OS
  177. # scheduler properties. A too high value may lead to thread thrashing.
  178. innodb_thread_concurrency=10
命令行下启动/关闭MySQL服务(注意:需要管理员身份启动CMD客户端):
  1. #启动服务
  2. net start mysql
  3. #关闭服务
  4. net stop mysql
  5. #补充:在Windows操作系统下,所有的服务都可以通过net start/stop来启动和关闭。
 
4、使用MySQL                                               
MySQL的登录:
 
技术分享
  1. C:\Windows\system32>mysql -V
  2. mysql Ver14.14Distrib5.5.49,forWin64(x86)
  3. C:\Windows\system32>mysql -uroot -p -P3306 -h127.0.0.1 //mysql -uroot -p用户密码 -P3306 -h127.0.0.1
  4. Enter password:******
  5. Welcome to the MySQL monitor.Commands end with ; or \g.
  6. YourMySQL connection id is 1
  7. Server version:5.5.49MySQLCommunityServer(GPL)
  8. Copyright(c)2000,2016,Oracle and/or its affiliates.All rights reserved.
  9. Oracle is a registered trademark of OracleCorporation and/or its
  10. affiliates.Other names may be trademarks of their respective
  11. owners.
  12. Type‘help;‘ or ‘\h‘for help.Type‘\c‘ to clear the current input statement.
  13. mysql> exit; //退出,还可以是“quit;”和“\q;”
  14. Bye
 
5、修改MySQL提示符                                     
技术分享
  1. C:\Windows\system32>mysql -uroot -phadoop --prompt \h
  2. Welcome to the MySQL monitor.Commands end with ; or \g.
  3. YourMySQL connection id is 5
  4. Server version:5.5.49MySQLCommunityServer(GPL)
  5. Copyright(c)2000,2016,Oracle and/or its affiliates.All rights reserved.
  6. Oracle is a registered trademark of OracleCorporation and/or its
  7. affiliates.Other names may be trademarks of their respective
  8. owners.
  9. Type‘help;‘ or ‘\h‘for help.Type‘\c‘ to clear the current input statement.
  10. localhostprompt mysql>
  11. PROMPT set to ‘mysql>‘
  12. mysql>
MySQL提示符:
参数                                                               描述                                        
\D 完整的日期
\d 当前数据库
\h 服务器名称
\u 当前用户
  1. mysql>prompt \u@\h \d> //组合来使用
 
6、MySQL的常用命令                                          
  1. mysql> SELECT VERSION(); //显示当前MySQL服务器的版本
  2. +-----------+
  3. | VERSION()|
  4. +-----------+
  5. |5.5.49|
  6. +-----------+
  7. 1 row inset(0.00 sec)
  8. mysql> SELECT NOW(); //显示当前日期时间
  9. +---------------------+
  10. | NOW()|
  11. +---------------------+
  12. |2016-05-1622:33:26|
  13. +---------------------+
  14. 1 row inset(0.00 sec)
  15. mysql> SELECT USER(); //显示当前用户
  16. +----------------+
  17. | USER()|
  18. +----------------+
  19. | root@localhost |
  20. +----------------+
  21. 1 row inset(0.00 sec)
MySQL语句的规范:
关键字与函数名称全部大写;
数据库名称、表名称、字段名称全部小写;
SQL语句必须以分号结尾;(这是必须的,不能省略。)
 
7、MySQL操作                                                                
创建数据库:
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name
说明:
DATABASE | SCHEMA:这哥俩完全相同,任选其一;
IF NOT EXISTS:使用CREATE DATABASE IF NOT EXISTS t1;,此时如果数据库t1已经存在则参生警告信息,不加则产生错误信息。
SHOW WARNINGS;命令查看警告信息。
[DEFAULT] CHARACTER SET  [=] charset_name:指定数据库的编码方式
查看编码方式命令:SHOW CREATE DATABASE t1;
创建gbk编码方式的数据库:
CREATE DATABASE IF NOT EXISTS t2 CHARACTER SET gbk;   //这里的DEFAULT 和 = 并没有写。
 
查看当前服务器下的数据库列表:
SHOW {DATABASES | SCHEMAS} [LIKE ‘pattern‘ | WHERE expr]
 
注意:{}花括号代表必选项;|从数据库当中做选择;[]有或者没有都可以,可选项;
  1. mysql> CREATE DATABASE t1; //创建数据库t1;或者:CREATE SCHEMA t1;
  2. Query OK,1 row affected (0.00 sec)
  3. mysql> SHOW DATABASES; //查看数据库
  4. +--------------------+
  5. |Database|
  6. +--------------------+
  7. | information_schema |
  8. | mysql |
  9. | performance_schema |
  10. | t1 | //t1是新创建的数据库,其它四个是安装好MySQL后就自带的数据库。
  11. | test |
  12. +--------------------+
  13. 5 rows inset(0.01 sec)
  14. mysql> CREATE DATABASE t1;
  15. ERROR 1007(HY000):Can‘t create database ‘t1‘; database exists
  16. mysql> SHOW WARNINGS;
  17. +-------+------+---------------------------------------------+
  18. | Level | Code | Message |
  19. +-------+------+---------------------------------------------+
  20. | Error | 1007 | Can‘t create database ‘t1‘; database exists |
  21. +-------+------+---------------------------------------------+
  22. 1 row inset(0.00 sec)
  23. mysql> CREATE DATABASE IF NOT EXISTS t1;
  24. Query OK,1 row affected,1 warning (0.00 sec)
  25. mysql> SHOW WARNINGS; //查看警告信息
  26. +-------+------+---------------------------------------------+
  27. |Level|Code|Message|
  28. +-------+------+---------------------------------------------+
  29. |Note|1007|Can‘t create database ‘t1‘; database exists |
  30. +-------+------+---------------------------------------------+
  31. 1 row in set (0.00 sec)
  32. mysql> SHOW CREATE DATABASE t1; //查看数据库的编码方式
  33. +----------+-------------------------------------------------------------+
  34. | Database | Create Database |
  35. +----------+-------------------------------------------------------------+
  36. | t1 | CREATE DATABASE `t1` /*!40100 DEFAULT CHARACTER SET utf8 */ |
  37. +----------+-------------------------------------------------------------+
  38. 1 row in set (0.00 sec)
  39. mysql> CREATE DATABASE IF NOT EXISTS t2 CHARACTER SET gbk; //创建指定编码方式的数据库
  40. Query OK, 1 row affected (0.00 sec)
  41. mysql> SHOW DATABASES;
  42. +--------------------+
  43. | Database |
  44. +--------------------+
  45. | information_schema |
  46. | mysql |
  47. | performance_schema |
  48. | t1 |
  49. | t2 |
  50. | test |
  51. +--------------------+
  52. 6 rows in set (0.00 sec)
  53. mysql> SHOW CREATE DATABASE t2;
  54. +----------+------------------------------------------------------------+
  55. | Database | Create Database |
  56. +----------+------------------------------------------------------------+
  57. | t2 | CREATE DATABASE `t2` /*!40100 DEFAULT CHARACTER SET gbk */ |
  58. +----------+------------------------------------------------------------+
  59. 1 row in set (0.00 sec)
 
修改数据库:
ALTER {DATABASE | SCHEMA} db_name [DEFAULT] CHARACTER SET [=] charset_name
  1. mysql> SHOW CREATE DATABASE t2;
  2. +----------+------------------------------------------------------------+
  3. |Database|CreateDatabase|
  4. +----------+------------------------------------------------------------+
  5. | t2 | CREATE DATABASE `t2`/*!40100 DEFAULT CHARACTER SET gbk */| //gbk
  6. +----------+------------------------------------------------------------+
  7. 1 row inset(0.00 sec)
  8. mysql> ALTER DATABASE t2 CHARACTER SET = utf8;
  9. Query OK,1 row affected (0.00 sec)
  10. mysql> SHOW CREATE DATABASE t2;
  11. +----------+-------------------------------------------------------------+
  12. |Database|CreateDatabase|
  13. +----------+-------------------------------------------------------------+
  14. | t2 | CREATE DATABASE `t2`/*!40100 DEFAULT CHARACTER SET utf8 */| //utf8
  15. +----------+-------------------------------------------------------------+
  16. 1 row inset(0.00 sec)
 
删除数据库:
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name 
  1. mysql> SHOW DATABASES;
  2. +--------------------+
  3. |Database|
  4. +--------------------+
  5. | information_schema |
  6. | mysql |
  7. | performance_schema |
  8. | t1 |
  9. | t2 |
  10. | test |
  11. +--------------------+
  12. 6 rows inset(0.00 sec)
  13. mysql> DROP DATABASE t2;
  14. Query OK,0 rows affected (0.06 sec)
  15. mysql> SHOW DATABASES; //少了t2
  16. +--------------------+
  17. |Database|
  18. +--------------------+
  19. | information_schema |
  20. | mysql |
  21. | performance_schema |
  22. | t1 |
  23. | test |
  24. +--------------------+
  25. 5 rows inset(0.00 sec)
 
 
 
 





01-MySQL安装、配置和使用

标签:

原文地址:http://www.cnblogs.com/chj0911/p/5500880.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!