Mac安装MySQL、修改MySQL的初始化密码、默认字符集为utf-8以及配置完my.cnf的文件后仍不生效的情况下的处理方案

来源:互联网 发布:北信源软件 编辑:程序博客网 时间:2024/06/06 06:53

作为一名刚刚使用Mac学习编程的小白来说,最大的痛苦莫过于其他同学都有老师的安装包,而我只能自力更生到网上找软件,安完了软件,发现配置过程还跟他们不一样,唉(捂脸ing)。不过还好,功夫不负有心人,我终于成功的配好了我所有的软件。因此,为了不让广大Mac同胞们不经历折磨就能够完成软件的配置,我特此更一篇关于如何在Mac下修改MySQL的默认字符集为utf-8的文章,并且特别是在没有所谓的.cnf格式的文件的情况下进行修改!!!
下面进入正题:

1.下载MySQL
在MySQL官网下载,网址:https://dev.mysql.com/downloads/mysql/
任意选择一个点击download

2.安装MySQL
在下载路径下找到要安装的文件,双击出现pkg文件,双击该文件,开始进行安装。
这里写图片描述

傻瓜式安装,一路继续,中途会跳出一个弹窗,选择同意,最后完成安装即可。
这里写图片描述

3.查看MySQL
安装好的MySQL在系统偏好设置里
这里写图片描述

点击MySQL图标,可以看到MySQL的运行状态,running表示正在运行,要想关闭数据库服务,可以点击右边的stop MySQL Server按钮
这里写图片描述

4.设置密码
下面开始进入正题,网上的大多教程都是告诉你在你安装的过程中会弹出一个框,那个框里就有MySQL的初始密码,可是我的框里没有啊!!!没有啊!!!于是,我就开始了修改密码的过程:
step1:
苹果->系统偏好设置->最下边点mysql 在弹出页面中 关闭mysql服务(点击stop mysql server)
这里写图片描述

step2:
进入终端输入:cd /usr/local/mysql/bin/
回车后 登录管理员权限 sudo su
回车后输入以下命令来禁止mysql验证功能 ./mysqld_safe –skip-grant-tables &
回车后mysql会自动重启(偏好设置中mysql的状态会变成running)
(从第四句开始,请忽略小女子前几行的错误)
这里写图片描述

step3.
输入命令 ./mysql
回车后,输入命令 FLUSH PRIVILEGES;
回车后,输入命令 SET PASSWORD FOR ‘root’@’localhost’ = PASSWORD(‘你的新密码’);比如你想把密码设置为root,那么语句为:set password for ‘root’@’localhost’ = ‘root’;
这里写图片描述
5.密码设置完成,我终于和同学们开始了一起学习的欢乐之旅,可是当我想在表内插入中文的时候,麻烦又来了,字符集编码不匹配!于是老师有带领着其他同学们更改编码,而我。。。又开始了各种百度(捂脸ing),然而可能是系统版本问题,网上的教程并没有解决我的问题,但是在我欧巴的操作下,它还是成功屈服了。所以,现在是修改字符集编码时间:
step1:
首先进入数据库,输入show variables like ‘%char%’;可以看到未修改的字符集编码是这样的。(对不起大家这张图是我盗的,因为我的编码已经改过来了(捂脸ing))
这里写图片描述

step2:
与设置密码前一样,这次操作同样要关闭数据库服务,图就不上了。

step3:
cd /usr/local/mysql/support-files/ 进入该路径,然后ls查看该路径下的文件。(我的该路径下没有以.cnf后缀结尾的文件)
这里写图片描述

第一种修改方式:
如果有以.cnf后缀结尾的文件,那么就可以修改mysql配置文件/etc/my.cnf
sudo cp /usr/local/mysql/support-files/my-medium.cnf /etc/my.cnf
sudo vi /etc/my.cnf
[client]部分加入:
default-character-set=utf8
[mysqld]部分加入:
character-set-server=utf8
修改完毕之后再启动mysql
想必大家已经看到了,我的是没有.cnf文件的,所以自然有

第二种修改方式:
cd /etc 进入该路径下
sudo mkdir my.cnf创建新文件
sudo vi my.cnf打开该文件
i 进入编辑模式,然后将该文章末尾的一段文本全部复制到该文件下,记住,一个符号都不能少!
esc退出编辑模式
:wq!强制保存退出

step4:
很多人在上一步就已经修改成功了。此时重新打开数据库服务,然后进入数据库再次输入show variables like ‘%char%’;就可以呈现下面的状态:
这里写图片描述

但是,很多人修改了my.cnf配置但还是不生效!这是怎么回事呢?具体如下:
原因:
我们注意到,这里只说了修改 my.cnf,并没有说清楚其绝对路径是哪个文件。也就是说,有可能修改的不是正确路径下的my.cnf文件。
在MySQL中,是允许存在多个 my.cnf 配置文件的,有的能对整个系统环境产生影响,例如:/etc/my.cnf。有的则只能影响个别用户,例如:~/.my.cnf。
MySQL读取各个my.cnf配置文件的先后顺序是:
/etc/my.cnf
/etc/mysql/my.cnf
/usr/local/mysql/etc/my.cnf
~/.my.cnf
解决方案:把my.cnf文件复制到每一个需要读取的路径下,即
sudo cp /etc/my.cnf /etc/mysql/my.cnf;
sudo cp/etc/my.cnf /usr/local/mysql/etc/my.cnf;
sudo cp/etc/my.cnf ~/.my.cnf;
这样就可以生效了!步骤如上,打开数据库服务,然后show variables like ‘%char%’;就可以修改成功了~

文本:

# Example MySQL config file for medium systems. # # This is for a system with little memory (32M - 64M) where MySQL plays # an important part, or systems up to 128M where MySQL is used together with # other programs (such as a web server) # # MySQL programs look for option files in a set of # locations which depend on the deployment platform. # You can copy this option file to one of those # locations. For information about these locations, see: # http://dev.mysql.com/doc/mysql/en/option-files.html # # In this file, you can use all long options that a program supports. # If you want to know which options a program supports, run the program # with the "--help" option. # The following options will be passed to all MySQL clients [client]default-character-set=utf8#password = your_password port = 3306 socket = /tmp/mysql.sock # Here follows entries for some specific programs # The MySQL server [mysqld]character-set-server=utf8init_connect='SET NAMES utf8port = 3306 socket = /tmp/mysql.sock skip-external-locking key_buffer_size = 16M max_allowed_packet = 1M table_open_cache = 64 sort_buffer_size = 512K net_buffer_length = 8K read_buffer_size = 256K read_rnd_buffer_size = 512K myisam_sort_buffer_size = 8M character-set-server=utf8 init_connect='SET NAMES utf8' # Don't listen on a TCP/IP port at all. This can be a security enhancement, # if all processes that need to connect to mysqld run on the same host. # All interaction with mysqld must be made via Unix sockets or named pipes. # Note that using this option without enabling named pipes on Windows # (via the "enable-named-pipe" option) will render mysqld useless! # #skip-networking# Replication Master Server (default) # binary logging is required for replication log-bin=mysql-bin# binary logging format - mixed recommended binlog_format=mixed# required unique id between 1 and 2^32 - 1 # defaults to 1 if master-host is not set # but will not function as a master if omitted server-id = 1# Replication Slave (comment out master section to use this) # # To configure this host as a replication slave, you can choose between # two methods : # # 1) Use the CHANGE MASTER TO command (fully described in our manual) - # the syntax is: # # CHANGE MASTER TO MASTER_HOST=<host>, MASTER_PORT=<port>, # MASTER_USER=<user>, MASTER_PASSWORD=<password> ; # # where you replace <host>, <user>, <password> by quoted strings and # <port> by the master's port number (3306 by default). # # Example: # # CHANGE MASTER TO MASTER_HOST='125.564.12.1', MASTER_PORT=3306, # MASTER_USER='joe', MASTER_PASSWORD='secret'; # # OR # # 2) Set the variables below. However, in case you choose this method, then # start replication for the first time (even unsuccessfully, for example # if you mistyped the password in master-password and the slave fails to # connect), the slave will create a master.info file, and any later # change in this file to the variables' values below will be ignored and # overridden by the content of the master.info file, unless you shutdown # the slave server, delete master.info and restart the slaver server. # For that reason, you may want to leave the lines below untouched # (commented) and instead use CHANGE MASTER TO (see above) # # required unique id between 2 and 2^32 - 1 # (and different from the master) # defaults to 2 if master-host is set # but will not function as a slave if omitted #server-id = 2 # # The replication master for this slave - required #master-host = <hostname> # # The username the slave will use for authentication when connecting # to the master - required #master-user = <username> # # The password the slave will authenticate with when connecting to # the master - required #master-password = <password> # # The port the master is listening on. # optional - defaults to 3306 #master-port = <port> # # binary logging - not required for slaves, but recommended #log-bin=mysql-bin# Uncomment the following if you are using InnoDB tables #innodb_data_home_dir = /usr/local/mysql/data #innodb_data_file_path = ibdata1:10M:autoextend #innodb_log_group_home_dir = /usr/local/mysql/data # You can set .._buffer_pool_size up to 50 - 80 % # of RAM but beware of setting memory usage too high #innodb_buffer_pool_size = 16M #innodb_additional_mem_pool_size = 2M # Set .._log_file_size to 25 % of buffer pool size #innodb_log_file_size = 5M #innodb_log_buffer_size = 8M #innodb_flush_log_at_trx_commit = 1 #innodb_lock_wait_timeout = 50[mysqldump] quick max_allowed_packet = 16M[mysql] no-auto-rehash # Remove the next comment character if you are not familiar with SQL #safe-updates default-character-set=utf8[myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M[mysqlhotcopy] interactive-timeout
阅读全文
0 0
原创粉丝点击