话说:
各位读者盆友,笔者曾经说过,三天不发表博客,就觉得面目可憎,现在看来,估计是奇丑无比了吧。突然发现很基础的东西,不想总结了,又终于说服自己,还是要坚持总结下,毕竟自己的才是自己的。
目录
1.下载并解压
2.命令模式安装
3.总结
4.补充——编码问题
当然网上文章很多,但是不是你的菜就不一定了,绝知此事要躬行。笔者感谢:
重点参考这篇:https://blog.csdn.net/TomotoJ/article/details/61923011
1.下载并解压
下载确保2件事:1.官网下载 2.版本下载正确
https://dev.mysql.com/downloads/
下一步,不要注册,直接下载即可。
这里吐槽一下MySQL官网,实在卡……
如果版本搞不清楚,别乱下载。对于MySQL而言,软件分为Enterprise企业版和Community社区版本,前者收费,后者免费,我们当然下载Community版本,而且下载Server。
下载版本是:GA,通用版本。Development Releases不是专业版,而是开发测试版本。而且观察包的大小也知一二,当然下载大的。
.dmg表示图形化安装;笔者当时下载下来,点击之后就没效果了;所以又下载了 compressed tar archive这个就是命令模式安装,.tar.gz形式的。
2.命令模式安装
1)解压
2)初始化
3)测试启动、关闭、重启、状态
4)配置环境变量
5)尝试登陆
6)修改密码
7)正式登陆
1)解压
sudo tar -zxvf mysql-5.7.21-macos10.13-x86_64.tar.gz -C /usr/local/
注意:因为操作 /usr/local,所以权限要用sudo ;因为切换目录 要用-C;
MySQL默认安装就是/usr/local,而且安装后默认目录名为mysql,所以也要重命名,否则操作时候在/usr/local下面找不到mysql这个包!
重命名为mysql
sudo mv mysql-5.7.21-macos10.13-x86_64 mysql
修改用户所属组和用户
默认是:用户名 staff;应该 是root wheel(这是Mac系统下默认的组、用户)
查看权限
dzjdeMacBook-Air:local dzj$ ls -la
total 0
drwxr-xr-x 7 root wheel 224 4 3 12:52 .
drwxr-xr-x@ 10 root wheel 320 3 28 15:26 ..
-rw-r--r-- 1 root wheel 0 3 26 10:30 .com.apple.installer.keep
drwxr-xr-x 8 root wheel 256 3 26 17:50 bin
drwxr-xr-x 10 root wheel 320 2 24 00:55 git
drwxr-xr-x 11 root wheel 352 4 3 12:51 mysql-8.0.4-rc-macos10.13-x86_64
修改用户组:
修改:
dzjdeMacBook-Air:local dzj$ sudo chown -R root:wheel mysql-8.0.4-rc-macos10.13-x86_64/
Password:
dzjdeMacBook-Air:local dzj$ ls -la
total 0
drwxr-xr-x 7 root wheel 224 4 3 12:52 .
drwxr-xr-x@ 10 root wheel 320 3 28 15:26 ..
-rw-r--r-- 1 root wheel 0 3 26 10:30 .com.apple.installer.keep
drwxr-xr-x 8 root wheel 256 3 26 17:50 bin
drwxr-xr-x 10 root wheel 320 2 24 00:55 git
drwxr-xr-x 11 root wheel 352 4 3 12:51 mysql-8.0.4-rc-macos10.13-x86_64
drwxr-xr-x 3 root wheel 96 2 24 00:55 share
dzjdeMacBook-Air:local dzj$
2)初始化
初始化之前,mysql目录下是没有data文件夹的,初始化之后会生成,注意观察前后变化。为什么要初始化?你用.dmg安装的时候,下一步就是初始化配置,我们这里无非是手动命令初始化。
与 MySQL 5.6 相比, 5.7 版本在安装时有两处不同:
初始化方式改变, 从 scripts/mysql_install_db –user=mysql 初始化方式变成了 bin/mysqld –initialize –user=mysql 方式;
这个 –user=root 这个root就是用户名,根据这个用户名,人家MySQL会默认给你一个临时的初始化密码(比较复杂,一定要复制粘贴)
dzjdeMacBook-Air:mysql dzj$ ls
COPYING README bin docs include lib man share support-files
dzjdeMacBook-Air:mysql dzj$ sudo bin/mysqld --initialize --user=root
2018-04-03T13:47:00.038923Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-04-03T13:47:00.052394Z 0 [Warning] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2018-04-03T13:47:00.279694Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-04-03T13:47:00.325515Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-04-03T13:47:00.394703Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 7bca99fa-3745-11e8-98b2-2025595188ba.
2018-04-03T13:47:00.408844Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-04-03T13:47:00.414851Z 1 [Note] A temporary password is generated for root@localhost: aol1S.TwX<wf
dzjdeMacBook-Air:mysql dzj$
到这里,有了用户名、临时密码、下面启动一旦测试成功,就可以尝试用临时密码登录拉!
3)测试启动、关闭、重启、状态
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server start
Starting MySQL
. SUCCESS!
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server restart
Shutting down MySQL
. SUCCESS!
Starting MySQL
. SUCCESS!
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server stop
Shutting down MySQL
. SUCCESS!
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server status
ERROR! MySQL is not running
dzjdeMacBook-Air:mysql dzj$
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server status
SUCCESS! MySQL running (27979)
dzjdeMacBook-Air:mysql dzj$
记得在Windows下面,我们是怎么启动服务的么?
cmd ==> services.msc ==>找到MySQL服务==》启动方式(自动)对吧?
这里用命令启动,当然也可以通过系统偏好设置==》MySQL。确保启动。
或者:我的电脑==》右键==》管理==》服务
4)配置环境变量
我们急不可耐的想查看MySQL版本信息或者使用mysql -uroot -p 登录,但是不可能每次都到/bin目录下去搞这些命令,配置环境变量:
cd ~/.bash_profile
vim .bash_prifile
加入以下内容:
export PATH=${PATH}:/usr/local/mysql/bin
:wq!
dzjdeMacBook-Air:~ dzj$ source .bash_profile
这个时候,随便在哪里都可以查看版本信息和尝试用临时密码登录了。
dzjdeMacBook-Air:mysql dzj$ mysql --version
mysql Ver 14.14 Distrib 5.7.21, for macos10.13 (x86_64) using EditLine wrapper
dzjdeMacBook-Air:mysql dzj$
5)尝试登陆
用临时密码登录:
dzjdeMacBook-Air:~ dzj$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.7.21
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> quit
这个时候,可以登录,但是看不了数据库,提示修改密码;临时密码很复杂,我们当然要掌控密码,所以下一步,修改密码。
6)修改密码
网上说要切换到Root用户,笔者个人经验,不需要切换,直接就可以修改。
进入mysql安装目录bin目录:
dzjdeMacBook-Air:bin dzj$ ./mysqladmin -u root -p password 123456
Enter password:
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
dzjdeMacBook-Air:bin dzj$
看到这句话,表明成功!实践验证,是不需要切换到Root用户的。
7)正式登陆
dzjdeMacBook-Air:~ dzj$ mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.21 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
如果你按照这个操作,会像我笔记这么流畅么?NO!笔者遇到不少问题,放在最后梳理,目的是不打断流畅的思路,哈哈。
3.总结
一切并非你看到的那么一帆风顺。
你可能遇到以下问题:
1)读写权限问题
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server start
Starting MySQL
.Logging to '/usr/local/mysql/data/dzjdeMacBook-Air.local.err'.
ERROR! The server quit without updating PID file (/usr/local/mysql/data/dzjdeMacBook-Air.local.pid).
dzjdeMacBook-Air:mysql dzj$
这一步可能发生在测试启动,表明.local.err没有update权限,这说明你需要sudo chmod 777 指定文件,修改权限。
2).dmg安装文件,点击一闪而过。
这种情况,尝试重新下载;换地方下载,也不行。所以,笔者通过命令模式安装。
3)权限问题
dzjdeMacBook-Air:local dzj$ rm -rf mysql-5.7.21-macos10.13-x86_64/
rm: mysql-5.7.21-macos10.13-x86_64//man/man8/mysqld.8: Permission denied
rm: mysql-5.7.21-macos10.13-x86_64//man/man8: Permission denied
rm: mysql-5.7.21-macos10.13-x86_64//man/man1/mysqldumpslow.1: Permission denied
rm: mysql-5.7.21-macos10.13-x86_64//man/man1/mysql_client_test_embedded.1: Permission denied
rm: mysql-5.7.21-macos10.13-x86_64//man/man1/mysqlslap.1: Permission denied
在操作/usr/local下面目录时候,Permission denied,加上sudo即可。
4)其他比如权限已经够了,但是没法启动mysql服务器
以下这种情况,就是多个pid,我们查看下服务后,强制关闭试一试。
dzjdeMacBook-Air:mysql dzj$ sudo support-files/mysql.server start
Starting MySQL
.. ERROR! The server quit without updating PID file (/usr/local/mysql/data/dzjdeMacBook-Air.local.pid).
dzjdeMacBook-Air:data dzj$ ps -ef | grep mysql
0 27895 1 0 10:06下午 ttys000 0:00.03 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/dzjdeMacBook-Air.local.pid
74 27979 27895 0 10:06下午 ttys000 0:00.80 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=dzjdeMacBook-Air.local.err --pid-file=/usr/local/mysql/data/dzjdeMacBook-Air.local.pid
501 28577 24661 0 8:24上午 ttys000 0:00.01 grep mysql
dzjdeMacBook-Air:data dzj$
Password:
dzjdeMacBook-Air:data dzj$ ps -ef | grep mysql
0 27895 1 0 10:06下午 ttys000 0:00.03 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/usr/local/mysql/data --pid-file=/usr/local/mysql/data/dzjdeMacBook-Air.local.pid
74 27979 27895 0 10:06下午 ttys000 0:00.85 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=dzjdeMacBook-Air.local.err --pid-file=/usr/local/mysql/data/dzjdeMacBook-Air.local.pid
501 28585 24661 0 8:28上午 ttys000 0:00.01 grep mysql
dzjdeMacBook-Air:data dzj$ sudo kill -9 27895
dzjdeMacBook-Air:data dzj$ ps -ef | grep mysql
74 27979 1 0 10:06下午 ttys000 0:00.86 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=dzjdeMacBook-Air.local.err --pid-file=/usr/local/mysql/data/dzjdeMacBook-Air.local.pid
501 28590 24661 0 8:28上午 ttys000 0:00.01 grep mysql
dzjdeMacBook-Air:data dzj$ sudo kill -9 27979
dzjdeMacBook-Air:data dzj$ ps -ef | grep mysql
501 28594 24661 0 8:29上午 ttys000 0:00.01 grep mysql
5)切换根目录和家目录
dzjdeMacBook-Air:~ root# su - dzj
dzjdeMacBook-Air:~ dzj$ sudo -i
dzjdeMacBook-Air:~ root#
还是那句话:绝知此事要躬行。虽然简单,但是遇到问题,要能快速找到问题所在,快速搞定!
流程没啥,主要是快速定位问题的能力,针对性解决。
4.补充——编码问题
数据库安装好后,首先就应该查看编码,否则后面建立的数据库、数据表都会遇到编码问题。默认latin.
为什么会是这样呢?因为我们用命令模式安装,根本没有涉及编码配置呢。查看一下编码情况:进入MySQL:
--------------------------------------------------------------------------------
mysql> show variables like "%char%";
+--------------------------+----------------------------------+
| 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 | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)
mysql>
--------------------------------------------------------------------------------
而我们创建的数据库、数据表编码果然是latin
mysql> show create database mysql;
+----------+------------------------------------------------------------------+
| Database | Create Database |
+----------+------------------------------------------------------------------+
| mysql | CREATE DATABASE `mysql` /*!40100 DEFAULT CHARACTER SET latin1 */ |
+----------+------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> show create table users;
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| users | CREATE TABLE `users` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`userName` varchar(50) NOT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 |
+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
如何修改?So Easy!
之前我们在Windows下面是如何配置编码的?在安装目录下有个my.ini配置文件,现在Max系统,木有的。
重点参考这篇博客:
https://www.cnblogs.com/gerald-x/p/6913877.html
思路:
1.拷贝mysql安装目录下support-files下面的my-default.cnf文件到/etc目录下;
修改:
搜索找到:[client] 的default-character-set=utf-8
[mysqld] default-character-set=utf-8
统统修改为utf-8即可!
问题是:我的MySQL版本是:5.7.21,从5.7.18以后,默认Mac及Linux系统安装,support-files下面是没有my-default.cnf的,所以怎么办?从网上拷贝,或者直接拷贝笔者的my.cnf,见附件。
2.或者直接拷贝附件my.cnf内容到/etc下
进入cd /ect
编辑 sudo vim my.cnf
粘贴,esc ==> :wq!
搞定!注意,这里一定要sudo,因为etc是重要目录,创建的文件不用sudo试没法保存的!
修改成功后结果如下:
--------------------------------------------------------------------------------
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/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.01 sec)
mysql>
--------------------------------------------------------------------------------
附件:
my.cnf
# 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=<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
这些配置文件究竟是什么含义呢?具体参考笔记my.cnf配置详解。
小结:
1.安装数据库没啥,MySQL官网有很详细的安装文档,为什么要舍近求远,大量搜博客呢?
2.MySQL安装好后,可以启动,可以查询,一定要查看下编码,然后在建数据库、数据表;否则修改起来多麻烦;
show variables like “%char%”;
如何修改编码呢?核心就是my.cnf文件。这个文件默认在support-files里面,如果没有怎么办?拷贝内容即可。
好,下期再会!