因在学习Python用上mysql,所以就在电脑上安装啦。这里是mac的安装方式。本人也是刚接触,现把遇到的过程记录下来。如果发现有问题,请多多指教。。
mac电脑是不自带mysql的,所以首先就要去mysql官网下载。
下载好后一直下一步的安装,安装后的就会弹出一个提示框。这时不要关闭。先把提示的内容复制保存下来。提示框我没有截图不好意思了。只有复制的内容。
提示的内容里的粗斜的字体就是root的临时密码。等下要用来修改密码。
A temporary password is generated for root@localhost: lg,qgs&t6jaI
password If you lose this password, please consult the section How to Reset the Root Password in the MySQL reference manual.
手快不小心关了,不用怕,可以在电脑的右通知栏看到。
好,现在安装好MySQL了,那先配置环境变量path,不然还是不能用的。
看看自己电脑有没有 .bash_profile这个文件,有就打开他,这个文件是隐藏的。自己百度下显示隐藏文件。
.bash_profile在文件里不用动其它,就找个位置添加下面语句就OK了。
export PATH=$PATH:/usr/local/mysql/bin
然后在终端里输入:(重新加载)
source ~/.bash_profile
或者是没有这个文件的就在终端输入:(执行root权限,当前用户的根目录下的.bash_profile)
sudo vim ~/.bash_profile
点击i 进行编辑模式然后输入:
export PATH=$PATH:/usr/local/mysql/bin
按ESC退出编辑模式,然后按住shift输入(保存退出)
:wq
最后在终端里输入:(重新加载)
source ~/.bash_profile
现在就可以打开mysql了,启动要输入电脑密码。
启动成功后会从红色的stopped变成绿色的running。
现在就在终端打开mysql了,输入:(会提示你输入mysql密码)
mysql -uroot -p
这时的密码还应该是临时密码,所以就要登录后修改密码了。现在粘贴临时密码并回车就可以登录成功了。
临时密码在安装时有提示过的。
登录成功接下来就是修改密码了。终端输入:
第一步(在用自己的密码来代替粗体字,怕忘就用password就行):
SET PASSWORD = PASSWORD('your new password');
第二步:
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER;
第三步:
flush privileges;
好了之后,就关闭终端后再打开来用新密码来登录mysql就行了。
现在的mysql就可以用了。但是数据库的默认编码没有支持中文。所以这里要修改下mysql的编码了。
在终端登录mysql后查看编码,输入:
show variables like '%char%';
这时会看到除了character_set_filesystem | binary 和 character_sets_dir| /usr/local/mysql-5.7.20-macos10.12-x86_64/share/charsets/外,value值并不是全部utf8。
这时重新打开终端输入:
cd /etc
创建my.cnf文件 :
sudo vim my.cnf
点i进入编辑后,把下面的内容粘贴上去。
# 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=utf8 init_connect='SET NAMES utf8 port = 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=, MASTER_PORT=, # MASTER_USER=, MASTER_PASSWORD= ; # # where you replace , , by quoted strings and # 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 = # # The username the slave will use for authentication when connecting # to the master - required #master-user = # # The password the slave will authenticate with when connecting to # the master - required #master-password = # # The port the master is listening on. # optional - defaults to 3306 #master-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
注意:看vim的第一行# 和内容有没有丢失。有的请补上。本人粘贴时就丢失了。
然后就是esc, 按shift+:qw 来个退出保存,关闭终端。
注意:mysql重启后再查看才会变化 。
在系统偏好设置那里把mysql重启下。再在终端里登录后输入查看:
show variables like '%char%';
如果查看的如果如下就成功了。
编码正确的这时就做完全部配置啦。
网友评论