MySQL 建库、建用户及建表事项
1,MySQL建库语句比较简单,一句话:
1 create database tppamltest3
2,创建用户及授权:
1 insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject) values("localhost","用户名",password("密码"),"","","");2 flush privileges; 3 grant all privileges on tppamltest3.* to 用户名@localhost identified by '密码';4 flush privileges;
其中授权语句給用户分配了全部权限,具体其他看百度。
3,建表脚本:
1 use tppamltest3; 2 3 SET FOREIGN_KEY_CHECKS=0; 4 5 -- ---------------------------- 6 -- Table structure for cfg_auto_mend 7 -- ---------------------------- 8 DROP TABLE IF EXISTS `cfg_auto_mend`; 9 CREATE TABLE `cfg_auto_mend` (10 `ID` varchar(32) NOT NULL,11 `MEND_SQL` text,12 PRIMARY KEY (`ID`)13 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
注意最后有默认编码设置。
---------------------------完成-----------------------------
=================================
一些异常情况的处理
=================================
乱码问题:
首先,shell里可以查看MYSQL目前编码
show variables like '%character%';
+--------------------------+-------------------------------------+| Variable_name| Value |+--------------------------+-------------------------------------+| character_set_client | utf8|| character_set_connection | utf8|| character_set_database | gbk || character_set_filesystem | binary|| character_set_results| utf8|| character_set_server | utf8|| character_set_system | utf8|| character_sets_dir | E:/anzhuan/MySQL5.5/share/charsets/ |+--------------------------+-------------------------------------+8 rows in set (0.00 sec)
可以更改
set character_set_client = 字符集
只要编码保持一致就不会出现乱码。
================================一下内容为摘抄========================================
1.系统编码
>show variables like '%character%';
mysql> show variables like '%collation%';
改变系统编码:修改my.cnf(/etc/my.cnf)中默认的编码选项[mysqld]下添加default-charcter-set=utf8 mysql 5.5以上版本换成了character-set-server=utf8 重新启动mysql
命令形式 mysql> SET NAMES 'utf8'; 重新启动mysql的时候所有的设置将失效
2.数据库编码
查看数据库编码: mysql> show create database db_name;
修改数据库编码: mysql> ALTER DATABASE db_name ####这里修改整个数据库的编码
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci;
在在建数据库的时候指定编码:
mysql> CREATE DATABASE db_name
CHARACTER SET utf8
DEFAULT CHARACTER SET utf8
COLLATE utf8_general_ci
DEFAULT COLLATE utf8_general_ci ;
3.数据库表和字段编码
查看数据库表和字段编码: mysql> show create table table_name;
>ALTER TABLE table_name DEFAULT CHARACTER SET utf8;
修改字段编码: mysql> ALTER TABLE `table_name` CHANGE `dd` `dd` VARCHAR( 45 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL 该命令就是将MYSQL数据库table_name表中 dd的字段编码改为utf8
4.命令行下插入汉字时指定编码:mysql> set names utf8; 有时候这一句很关键!
mysql> insert into test(name) values('王东伟');
总之,不管采用那一种编码方式,只要做到完全统一将能达到相应的效果。
-------------------------------------------------------------------分隔-----------------------------------------------------------------------
在查询结果中可以看到mysql 数据库系统中客户端、数据库连接、数据库、文件系统、查询结果、服务器、系统的字符集设置在这里,文件系统字符集是固定的,系统、服务器的字符集在安装时 确定,与乱码问题无关。乱码的问题与客户端、数据库连接、数据库、查询结果的字符集设置有关。
*注:客户端是看访问mysql 数据库的方式,通过命令行访问,命令行窗口就是客户端,通过JDBC 等连接访问,程序就是客户端我们在向mysql 写入中文数据时,在客户端、数据库连接、写入数据库时分别要进行编码转换。在执行查询时,在返回结果、数据库连接、客户端分别进行编码转换。现在我们应该 清楚,乱码发生在数据库、客户端、查询结果以及数据库连接这其中一个或多
个环节
接下来我们来解决这个问题
在登录数据库时,我们用mysql --default-character-set=字符集-u root -p 进行连接,这时我们
再用show variables like '%char%';命令查看字符集设置情况,可以发现客户端、数据库连接、
查询结果的字符集已经设置成登录时选择的字符集了
如果是已经登录了,可以使用set names 字符集;命令来实现上述效果,等同于下面的命令:
set character_set_client = 字符集
set character_set_connection = 字符集
set character_set_results = 字符集
如果是通过JDBC 连接数据库,可以这样写URL:
URL=jdbc:mysql://localhost:3306/abs?useUnicode=true&characterEncoding=字符集
JSP 页面等终端也要设置相应的字符集
数据库的字符集可以修改mysql 的启动配置来指定字符集,也可以在create database 时加上
default character set 字符集来强制设置database 的字符集
通过这样的设置,整个数据写入读出流程中都统一了字符集,就不会出现乱码了
为什么从命令行直接写入中文不设置也不会出现乱码?
可以明确的是从命令行下,客户端、数据库连接、查询结果的字符集设置没有变化
输入的中文经过一系列转码又转回初始的字符集,我们查看到的当然不是乱码
但这并不代表中文在数据库里被正确作为中文字符存储
举例来说,现在有一个utf8 编码数据库,客户端连接使用GBK 编码,connection 使用默认
的ISO8859-1(也就是mysql 中的latin1),我们在客户端发送"中文"这个字符串,客户端
将发送一串GBK 格式的二进制码给connection 层,connection 层以ISO8859-1 格式将这段
二进制码发送给数据库,数据库将这段编码以utf8 格式存储下来,我们将这个字段以utf8
格式读取出来,肯定是得到乱码,也就是说中文数据在写入数据库时是以乱码形式存储的,
在同一个客户端进行查询操作时,做了一套和写入时相反的操作,错误的utf8 格式二进制
码又被转换成正确的GBK 码并正确显示出来。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.
