MySQL编码LINUX平台
Intercon
|
1#
Intercon 发表于 2008-05-14 18:48
MySQL编码LINUX平台
Linux下安装MySQL5.0.37需要以下面三个包:
MySQL-client-community-5.0.37-0.rhel3.i386.rpm MySQL-server-community-5.0.37-0.rhel3.i386.rpm perl-DBI-1.53-2.fc7.i386.rpm (以下步骤需要root权限) 1.验证是否已经安装过MySQL rpm -qa|grep MySQL 如果发现有安装过,并需要卸载,使用下面命令 rpm -e MySQL-client-community MySQL-server-community 2.安装 执行:rpm -ivh MySQL-client-community-5.0.37-0.rhel3.i386.rpm MySQL-server-community-5.0.37-0.rhel3.i386.rpm 如果控制台提示: error: Failed dependencies: perl(DBI) is needed by MySQL-client-community-5.0.37-0.rhel3.i386 perl(DBI) is needed by MySQL-server-community-5.0.37-0.rhel3.i386 代表当前操作系统缺少DBI模块,请先安装DBI模块. rpm -ivh perl-DBI-1.53-2.fc7.i386.rpm 提示下面文字代表正确安装: Preparing... ########################################### [100%] 1:perl-DBI ########################################### [100%] 重新执行MySQL的安装命令 提示下面文字代表正确安装: Preparing... ########################################### [100%] 1:MySQL-server-community ########################################### [ 50%] 2:MySQL-client-community ########################################### [100%] 3.初始化安装 /usr/bin/mysql_install_db 4.启动mysql /etc/init.d/mysql start (停止有两种方法:[mysqladmin -uroot -p shutdown] [/etc/init.d/mysql stop]) 5.验证安装成功 netstat -nat|grep 3306 如果能看到下面一行说明正常安装: tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 6.修改mysql的root用户密码 mysqladmin -u root password '123456' 7.登陆mysql客户端 mysql -uroot -p 输入:123456 8.建立数据库 mysql> create database gbkdb character set gbk; Query OK, 1 row affected (0.01 sec) 9.为数据库gbkdb建立用户(用户:user 密码:123) mysql> grant select,insert,update,delete,create on gbkdb.* to user@"%" identified by "123"; Query OK, 0 rows affected (0.01 sec)10.修改配置 cp /usr/share/mysql/my-medium.cnf /etc/my.cnf vi /etc/my.cnf 在[client]下面增加一行: [client] default-character-set = gbk 在[mysqld]下面增加三行: [mysqld] default-character-set = gbk default-collation = gbk_chinese_ci init_connect = 'SET NAMES gbk' 在[mysql]下面增加一行: [mysql] default-character-set = gbk 11.重启MySQL /etc/init.d/mysql restart 12.重登陆mysql控制台确认配置变化 mysql -uroot -p 输入:123456 mysql> show variables like 'character_set_%'; +--------------------------+----------------------------+ | Variable_name | Value | +--------------------------+----------------------------+ | character_set_client | gbk | | character_set_connection | gbk | | character_set_database | gbk | | character_set_filesystem | binary | | character_set_results | gbk | | character_set_server | gbk | | character_set_system | utf8 | | character_sets_dir | /usr/share/mysql/charsets/ | +--------------------------+----------------------------+ 13.在远程使用控制台登陆,确认用户可以使用 mysql -h10.10.1.156 -uuser -p 输入:123 mysql> use gbkdb; Database changed 14.连接字符串 在应用中使用下面两种皆可: jdbc:mysql://10.10.1.156/gbkdb?characterEncoding=gbk jdbc:mysql://10.10.1.156/gbkdb?useUnicode=true&characterEncoding=GBK 说明:mysql5与mysql4的字符集处理方式不同 MySQL5已经可以很好的支持各种字符集,在安装完成之后可以不用修改配置文件就能支持GBK/GB2312等中文字符集 我们拷贝一个my.cnf出来修改是更好的支持客户端维护,省得需要每次都使用set names gbk;命令切换显示字符集. 如果在控制台出现乱码(如下方oprName),系控制台的显示问题: mysql> SELECT * FROM user; +-------+--------+---------+--------+-----------+ | oprId | passwd | oprName | roleId | oprStatus | +-------+--------+---------+--------+-----------+ | 02562 | 000000 | ?? | ADMIN | ON | +-------+--------+---------+--------+-----------+ 1 rows in set (0.00 sec) 只需要执行set names命令即可; mysql> set names gbk; Query OK, 0 rows affected (0.00 sec) mysql> SELECT * FROM user; +-------+--------+---------+--------+-----------+ | oprId | passwd | oprName | roleId | oprStatus | +-------+--------+---------+--------+-----------+ | 02562 | 000000 | 赵军 | ADMIN | ON | +-------+--------+---------+--------+-----------+ 1 rows in set (0.00 sec) ----相关目录---- 1、数据库目录 /var/lib/mysql/ 2、配置文件 /usr/share/mysql(mysql.server命令及配置文件) 3、相关命令 /usr/bin(mysqladmin mysqldump等命令) 4、启动脚本 /etc/rc.d/init.d/(启动脚本文件mysql的目录) ----常用命令---- 1、显示数据库 mysql> show databases; +----------+ | Database | +----------+ | mysql | | test | +----------+ 2 rows in set (0.04 sec) 2.打开库 mysql> use mysql; Database changed 3.显示表 mysql> show tables; +-----------------+ | Tables_in_mysql | +-----------------+ | columns_priv | | db | | func | | host | | tables_priv | | user | +-----------------+ 6 rows in set (0.01 sec) |