一、windows安装mysql
1、设置系统环境变量
在系统环境变量里新建系统变量
变量名为:MYSQL_HOME
变量值为:mysql的安装路径
在Path路径里加一路经:%MYSQL_HOME%\bin
2、将my-default.ini改成my.ini
3、修改my.ini文件
basedir =
SSL: Not in use
Current pager: stdout Using outfile: ''
Using delimiter: ;
Server version: 5.0.45 Source distribution
Protocol version: 10
Connection:Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: latin1 Conn. characterset: latin1
UNIX socket: /var/lib/mysql/mysql.sock
Uptime: 33 days 58 min 53 sec
Threads: 1 Questions: 72 Slow queries: 0 Opens: 25 Flush tables: 1 Open tables: 18 Queries per second avg: 0.000
--------------
查看mysql发现Server characterset,Db characterset的字符集设成了latin1,所以出现中文乱码。
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| users |
+----------------+
1 row in set (0.00 sec)
更改表的字符集。
mysql> alter table users character set GBK;
Query OK, 3 rows affected (0.08 sec)
Records: 3 Duplicates: 0 Warnings: 0
查看表的结构:
mysql> show create table users;
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| Table | Create Table
|
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
`userid` int(11) default NULL,
`username` char(20) character set latin1 default NULL
) ENGINE=InnoDB DEFAULT CHARSET=gbk |
+-------+-----------------------------------------------------------------------
------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> desc users;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| userid | int(11) | YES | | NULL | |
| username | char(20) | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
2 rows in set (0.02 sec)
这时向表中插入中文然后有错误。
mysql> insert into users values(88,'中文');
ERROR 1366 (HY000): Incorrect string value: '\xD6\xD0\xCE\xC4' for column 'usern
ame' at row 1
还要更改users表的username的字符集。
mysql> alter table users modify username char(20) character set gbk;
ERROR 1366 (HY000): Incorrect string value: '\xC0\xEE\xCB\xC4' for column 'usern
ame' at row 1
因为表中已经有数据,所以更改username字符集的操作没有成***
清空users表中的数据
mysql> truncate table users;
Query OK, 3 rows affected (0.01 sec)
从新更改user表中username的字符集
mysql> alter table users modify username char(20) character set gbk;
Query OK, 0 rows affected (0.06 sec)
Records: 0 Duplicates: 0 Warnings: 0
这时再插入中文字符,插入成***。
mysql> insert into users values(88,'中文');
Query OK, 1 row affected (0.01 sec)
mysql> select * from users;
+--------+----------+
| userid | username |
+--------+----------+
| 88 | 中文 |
+--------+----------+
1 row in set (0.00 sec)
mysql>
六、在linux下对mysql进行C编程问题:
要指明库文件mysql.h的路径,一般为
/usr/include/mysql/mysql.h
编译时要指明libmysqlclient.so的路径:
/usr/lib/mysql/libmysqlclient.so
命令为 : gcc 点C文件名 /usr/lib/mysql/libmysqlclient.so