本文目录一览:
- 1、linux下rpm安装mysql,如何修改默认字符集为utf8
- 2、linux服务器上的mysql字符集里没有GBK编码
- 3、linux 安装mysql, 不支持GBK字符集
- 4、如何在linux中安装mysql
linux下rpm安装mysql,如何修改默认字符集为utf8
如果/etc/目录下没有my.cnf配置文件,请到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf中;
不存在启动不了的情况啊,如有启动不了,仔细查看你的my.cnf文件,是不是哪里配置需要修改,然后查看一下mysql启动脚本/etc/rc.d/init.d/mysql。最重要的结合你mysql的错误日志去分析。
设置默认utf-8
在文件my.cnf中加上如下配置:
[mysqld]
init_connect = 'SET NAMESutf8'
character-set-server = utf8
collation-server =utf8_general_ci
针对第三点:
在创建数据库完成后,最好查看下你数据库的字符集是什么?用show create database test;命令可以看到。必须保证你数据库的编码格式是utf-8的,这样才可以保证插入到数据库中的是中文格式的
linux服务器上的mysql字符集里没有GBK编码
如果你是编译安装的话,你最好加上--with-charset=gbk
--with-extra-charsets=all
另外,如果你上面编译的时候使用了--with-charset=gbk
,那么你创建的数据库默认就是gbk字符集,否则,需要你在创建数据库时指定数据库的字符集,
如:create
database
dbname
default
character
set
gbk;
linux 安装mysql, 不支持GBK字符集
安装完的MySQL的默认字符集为 latin1 ,为了要将其字符集改为用户所需要的(比如utf8),就必须改其相关的配置文件;由于linux下MySQL的默认安装目录分布在不同的文件下;不像windows一样放在同一目录下,只需修改其中的my.ini文件,重起后就生效了;所以先来看看linux下MySQL的数据库文件、配置文件和命令文件分别在不同的目录 :
1、数据库目录,其所创建的数据库文件都在该目录下
/var/lib/mysql/
2、配置文件 (mysql.server命令及配置文件所在地)
/usr/share/mysql
3、相关命令(如mysql mysqladmin等)
/usr/bin
4、启动脚本(如mysql启动命令)
/etc/rc.d/init.d/
查看默认字符集
#mysql -u root - p
#(输入密码)
mysql show variables like 'character_set%' ;//或者是status
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
修改字符集 :
1.查找/etc目录下是否有my.cnf文件;
#ls -l | grep my.cnf (在/etc下查找是否有my.cnf文件存在)
2.如果没有就要从/usr/share/mysql,拷贝一个到/etc 下,在/usr/share/mysql目录下有五个后缀为.cnf的文件,分别是 my-huge.cnf my-innodb-heavy-4G.cnf my-large.cnf my-medium.cnf my-small.cnf ;从中随便拷贝一个到/etc目录下并将其改为my.cnf文件,选择的是my-medium.cnf :
#cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
3.修改my.cnf文件,在该文件中的三个地方加上 default-character-set=utf8([client] [mysqld] [mysql])
#vi /etc/my.cnf
修改如下:(红色为添加部分)
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-character-set=utf8
init_connect='SET NAMES utf8' //此行可不加
[mysql]
no-auto-rehash
default-character-set=utf8
保存退出;
4.重起MySQL服务器,使其设置的内容生效
#/etc/init.d/mysql restart
5. 重新登入mysql;
# mysql -u root - p
#(输入密码)
mysql show variables like 'character_set%' ;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |(都生成了utf8,成功了。)
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
mysql启动和停止 操作 :
可以通过 #/etc/init.d/mysql [start | stop | restart](实现启动,停止,重启)
也可以通过 #service mysql [start | stop | restart](实现启动,停止,重启)
*********************************
另外:如果在修改字符集前,尝试在数据库中插入中文,那么当修改了字符集后,在所插入中文的数据库中(在别的数据库中可能没问题),
通过show variables like 'character_set%'查询的结果可如下:
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
如上图,character_set_database仍然会为latin1,采取的办法是先导出该数据库中数据,如果数据不重要,则可drop此数据库,
然后再重新create一个数据库。
如何在linux中安装mysql
1.首先在liunx下安装Mysql数据库
~$ sudo apt-get install mysql-server #安装Mysql服务器端
~$ ps -aux|grep mysql #检查Mysql服务器进程
beili 10301 0.0 0.0 37080 3604 pts/7 S+ 5月20 0:00 mysql -u root -p
mysql 25125 0.0 0.0 4472 1704 ? Ss 09:51 0:00 /bin/sh /usr/bin/mysqld_safe
mysql 25496 0.0 3.1 859956 127084 ? Sl 09:51 0:01 /usr/sbin/mysqd --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib/mysql/plugin --log-error=/var/log/mysql/error.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
beili 26652 0.0 0.0 13708 2180 pts/19 S+ 10:32 0:00 grep --color=auto mysql
~$ netstat -nlt|grep 3306 #检查Mysql服务器占用端口
tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
~$ /etc/init.d/mysql status # 通过启动命令检查Mysql服务器状态
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2015-05-21 09:51:13 CST; 43min ago
Process: 25126 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 25122 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
~$ service mysql status # 通过系统服务检查Mysql服务器状态
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since 四 2015-05-21 09:51:13 CST; 44min ago
Process: 25126 ExecStartPost=/usr/share/mysql/mysql-systemd-start post (code=exited, status=0/SUCCESS)
Process: 25122 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
~$ whereis mysql # 查看mysql各个文件安装的目录
mysql: /usr/bin/mysql /usr/lib/mysql /etc/mysql /usr/include/mysql /usr/share/mysql /usr/share/man/man1/mysql.1.gz
2.访问和配置Mysql
$ mysql -u root -p
Enter password:
mysql默认的字符集为latin1的,所以要改为utf8的。很多网上的文章执行“sudo vi /etc/mysql/my.cnf”,可是打开一看,里面就两行话:
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
想来mysql的配置文件应该是在那两个文件夹下面,于是尝试之下打开了/etc/mysql/mysql.conf.d/ 下的mysqld.cnf文件,执行下面的命令:
~$ sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
...
[client]
default-character-set=utf8
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
character-set-server=utf8
collation-server=utf8_general_ci
...
注:棕色部分是添加的内容
重启mysql服务,并查看字符集的设置情况:
~$ sudo /etc/init.d/mysql restart
~$ mysql -u root -p
mysql show variables like "%char%"
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
本文链接:https://my.lmcjl.com/post/18470.html
4 评论