下载 MySQL:
通过 MobaXterm 连接到腾讯云 CentOS7 服务器
进入到目录/usr/local/ 中:
cd /usr/local/
创建目录/usr/local/tools,如果有则忽略:
mkdir -p tools
创建/usr/local/mysql 目录,如果已存在则忽略:
mkdir -p mysql
进入到目录/usr/local/tools 中:
cd tools/
查看系统中是否已安装MySQL 服务:
yum list installed | grep mysql
如果已安装则删除MySQL 及其依赖的包:
yum -y remove mysql-libs.x86_64
下载 mysql57-community-release-el7-8.noarch.rpm的 YUM 源:
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
安装mysql57-community-release-el7-8.noarch.rpm:
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
安装完后,得到如下两个包:
mysql-community.repo
mysql-community-source.repo
安装 MySQL:
yum install mysql-server
遇到疑问的地方,一路Y 下去即可;
安装完毕后,在 /var/log/mysqld.log 文件中会自动生成一个随机的密码,我们需要先取得这个随机密码,以用于登录MySQL 服务端:
grep "password" /var/log/mysqld.log
打印如下内容:A temporary password is generated for root@localhost: s#t&%VP%b4Eu
我们复制 root@localhost:后面的随机字符串,这个字符串就是MySQL 在安装完成后为我们随机生成的密码;
注意:
在这里如果执行grep "password" /var/log/mysqld.log 找不到新密码时,执行mysqladmin -u root password '123456' 出现报错信息
此时/var/log/mysqld.log 会有日志信息,这时在执行 grep"password" /var/log/mysqld.log 会看到如下所示:
grep"password" /var/log/mysqld.log
登录到 MySQL 服务端并更新用户 root 的密码:
mysql -u root -p s#t&%VP%b4Eu
打印出 MySQL 的版本即表明已登录并设置用户 root 可以在任意 IP 下被访问。
配置 MySQL:
root帐号登陆[root@mysql var]# mysql-uroot -p
Enter password:
登陆成功后第一件事重置密码,由于5.7版本以后mysql密码策略加强了,密码设置太简单可能会报错,修改不成功!(可以尝试大小写字母+数字8位以上,如果不行继续往下看。。。)
修改密码,允许其他Ip访问
SET PASSWORD = PASSWORD(' younew password ');
ALTER USER 'root'@'localhost' PASSWORDEXPIRE NEVER;
flush privileges;
注意:如果执行上面步骤报错!
请阅读 http://www.cnblogs.com/ivictor/p/5142809.html (加深理解)
这个其实与validate_password_policy的值有关。
validate_password_policy有以下取值:
Policy | Tests Performed |
0 or LOW | Length |
1 or MEDIUM | Length; numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file |
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
有时候,只是为了自己测试,不想密码设置得那么复杂,譬如说,我只想设置root的密码为123456。
必须修改两个全局参数:
首先,修改validate_password_policy参数的值
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
这样,判断密码的标准就基于密码的长度了。这个由validate_password_length参数来决定。
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 8 |
+----------------------------+
1 row in set (0.00 sec)
validate_password_length参数默认为8,它有最小值的限制,最小值为:
validate_password_number_count
+ validate_password_special_char_count
+ (2 *validate_password_mixed_case_count)
其中,validate_password_number_count指定了密码中数据的长度,validate_password_special_char_count指定了密码中特殊字符的长度,validate_password_mixed_case_count指定了密码中大小字母的长度。
这些参数,默认值均为1,所以validate_password_length最小值为4,如果你显性指定validate_password_length的值小于4,尽管不会报错,但validate_password_length的值将设为4。如下所示:
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 8 |
+----------------------------+
1 row in set (0.00 sec)
mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)
如果修改了validate_password_number_count,validate_password_special_char_count,validate_password_mixed_case_count中任何一个值,则validate_password_length将进行动态修改。
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 4 |
+----------------------------+
1 row in set (0.00 sec)
mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| 1 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> set globalvalidate_password_mixed_case_count=2;
Query OK, 0 rows affected (0.00 sec)
mysql> select @@validate_password_mixed_case_count;
+--------------------------------------+
| @@validate_password_mixed_case_count |
+--------------------------------------+
| 2 |
+--------------------------------------+
1 row in set (0.00 sec)
mysql> select @@validate_password_length;
+----------------------------+
| @@validate_password_length |
+----------------------------+
| 6 |
+----------------------------+
1 row in set (0.00 sec)
阅读完上面的内容后开始执行:
set globalvalidate_password_policy=0;
set global validate_password_length=1;
set globalvalidate_password_mixed_case_count=2;
然后在执行:
SET PASSWORD = PASSWORD('younew password'); ALTER USER 'root'@'localhost'PASSWORD EXPIRE NEVER; flush privileges; exit;(退出重新进入)
设置用户 root 可以在任意 IP 下被访问: grant all privileges on *.* to root@"%" identified by "younew password";
设置用户 root 可以在本地被访问: grant all privileges on *.* to root@"localhost" identified by "younew password"; 刷新权限使之生效: flush privileges;
设置 MySQL 编码格式:
找到 my.cnf 文件修改处1:添加以下2行
[client]
default-character-set=utf8
[mysqld]
#skip-grant-tables
#skip-networking
#
# Remove leading# and set to the amount of RAM for the most important data
# cache inMySQL. Start at 70% of total RAM for dedicated server, else 10%.
#innodb_buffer_pool_size = 128M
#
# Remove leading# to turn on a very important data integrity option: logging
# changes to thebinary log between backups.
# log_bin
#
# Remove leading# to set options mainly useful for reporting servers.
# The serverdefaults are faster for transactions and fast SELECTs.
# Adjust sizesas needed, experiment to find the optimal values.
#join_buffer_size = 128M
#sort_buffer_size = 2M
#read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disablingsymbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
修改处2:添加以下3行
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
最终执行命令:service mysqld restart后,编码格式生效。
此时此刻,踩过多少坑已经不重要,在这个过程中了解很多知识,并且感受到成功的喜悦,以此共勉不断前进的路人,因为还有更多的坑等着我们去踏平它!
查看原文(出自我的个人博客) http://www.guochunhui.cn/b/view/29