前几天尝试装了MySQL 5.7,《MySQL的rpm和源码两种安装操作》,用了rpm和源码编译两种方法,由于项目需要,这次使用MySQL 5.6版本,rpm安装方法,记录了一些安装过程的问题。
1. 卸载机器上自带的MySQL包
检索已安装组件,
rpm -qa | grep mysql mysql-libs-5.1.61-4.el6.x86_64
卸载已安装组件,使用--nodeps则强制忽略依赖,
rpm -e mysql-libs-5.1.61-4.el6.x86_64 --nodeps
2. 安装MySQL 5.7 rpm包
注意,因为包之间有彼此依赖,所以安装有顺序要求,
rpm -ivh MySQL-server-5.6.31-1.linux_glibc2.5.x86_64.rpm rpm -ivh MySQL-devel-5.6.31-1.linux_glibc2.5.x86_64.rpm rpm -ivh MySQL-client-5.6.31-1.linux_glibc2.5.x86_64.rpm
3. 初始化数据库,启动和配置
安装完成,需要初始化数据库,
mysql_install_db
复制配置文件,
cp /usr/share/mysql/my-default.cnf /etc/my.cnf
启动MySQL服务,
service mysql start
设置开机启动,
chkconfig mysql on
检索设置生效,
chkconfig --list | grep mysql mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
4. 重置密码
上面初始化操作,记录了root账户的临时密码,存储于文件/root/.mysql_secret中,
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER ! You will find that password in '/root/.mysql_secret'. You must change that password on your first connect, no other statement but 'SET PASSWORD' will be accepted. See the manual for the semantics of the 'password expired' flag.
/root/.mysql_secret文件,
# The random password set for the root user at Mon Aug 28 09:40:09 2017 (local time): pcqEmPanlf3emxWW
登陆数据库,直接执行set password="新密码",会报错误,要求使用41位的十六进制数字才可以,
mysql> set password="My@sql"; ERROR 1372 (HY000): Password hash should be a 41-digit hexadecimal number
一种方法是可以从另一个库,执行select password("新密码")语句,得到对应的十六进制数字,
mysql> select password('My@sql'); ------------------------------------------- | password('My@sql') | ------------------------------------------- | *02ED29277F2482ED4AAE65574FC9940A4895D6D7 | ------------------------------------------- 1 row in set, 1 warning (0.01 sec)
再用此十六进制设置,
mysql> set password='*02ED29277F2482ED4AAE65574FC9940A4895D6D7'; Query OK, 0 rows affected (0.00 sec)
可以执行flush privileges
mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
这是官方文档,对于flush privileges解释,
引用一段网络描述语,
flush privileges命令本质上的作用是将当前user和privilige表中的用户信息/权限设置从mysql库(MySQL数据库的内置库)中提取到内存里。MySQL用户数据和权限有修改后,希望在"不重启MySQL服务"的情况下直接生效,那么就需要执行这个命令。通常是在修改ROOT帐号的设置后,怕重启后无法再登录进来,那么直接flush之后就可以看权限设置是否生效。而不必冒太大风险。
另一种设置密码的方法,就是使用PASSWORD函数
mysql> set password=PASSWORD('My@sql'); Query OK, 0 rows affected (0.00 sec)
如下是针对PASSWORD函数的介绍,
可以看一下,设置的用户密码信息,
mysql> use mysql Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select host, user, password from user; ----------------- ------ ------------------------------------------- | host | user | password | ----------------- ------ ------------------------------------------- | localhost | root | *02ED29277F2482ED4AAE65574FC9940A4895D6D7 | | vm-100-app | root | *DB2464B7F3447DFCC3CACE1A0FD19BFC6101339C | | 127.0.0.1 | root | *DB2464B7F3447DFCC3CACE1A0FD19BFC6101339C | | ::1 | root | *DB2464B7F3447DFCC3CACE1A0FD19BFC6101339C | ----------------- ------ ------------------------------------------- 4 rows in set (0.00 sec)
5. 允许远程登录
默认情况下,用户只能使用本地登陆,可以设置允许远程登录,
mysql> update user set host='%' where user='root' and host='hostloacl'; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql> flush privileges; Query OK, 0 rows affected (0.00 sec)
6. 安装默认路径
数据库目录:/var/lib/mysql/
配置文件目录:/usr/share/mysql
相关命令目录:/usr/bin
启动脚本:/etc/init.d/mysql
碰见的几个问题,
(1) 登陆提示错误
bash-4.1# mysql -uroot -p Enter password: ERROR 1045 (28000): Unknown error 1045
发现是用户密码错误,提示了上述报错信息。
(2) my.cnf配置文件如下,
[mysqld] port=3306 character_set_server=utf8 character_set_client=utf8 collation-server=utf8_general_ci lower_case_table_names=1 max_connections=1000 default-character-set=utf8 [client] password=My@sql port=3306 default-character-set=utf8
重启服务时,报错,
service mysql restartShutting down MySQL.. SUCCESS! Starting MySQL... ERROR! The server quit without updating PID file (/var/lib/mysql/vm-kvm11852-app.pid).
检索vm-kvm11852-app.err文件,
... 2017-08-29 16:02:01 23566 [ERROR] /usr/sbin/mysqld: unknown variable 'default-character-set=utf8' 2017-08-29 16:02:01 23566 [ERROR] Aborting ...
提示无法识别参数值default-character-set=utf8,再次看一下配置文件,确实包含了default-character-set=utf8,将其注释,
[mysqld] port=3306 character_set_server=utf8 character_set_client=utf8 collation-server=utf8_general_ci lower_case_table_names=1 max_connections=1000 #default-character-set=utf8
重启服务正常,
[root@vm-kvm11852-app mysql]# service mysql restart ERROR! MySQL server PID file could not be found! Starting MySQL. SUCCESS!
针对default-character-set=utf8,不同版本,是有所不同的,
原来在5.1版本时,为了解决中文乱码问题设置默认字符集为utf8时,在my.ini内的[mysql]和[mysqld]项中都是写:default-character-set=utf8 到了5.5版本, [mysql]项内可以这么写, [mysqld]项内不能再这么写了,而是必须写:character_set_server=utf8,否则在启动MySQL服务时会有1067错误
(3) 可以使用verbose参数,检索mysqld命令帮助,
mysqld --defaults-file=/opt/mysql/my.cnf --user=mysql --verbose --help ... Usage: mysqld [OPTIONS] ... --print-defaults Print the program argument list and exit. --no-defaults Don't read default options from any option file, except for login file. --defaults-file=# Only read default options from the given file #. ...
如果您觉得此篇文章对您有帮助,欢迎关注微信公众号:bisal的个人杂货铺,您的支持是对我最大的鼓励!共同学习,共同进步:)