博文目录
删除旧版本通过yum来进行mysql的安装配置MySql添加远程登录用户 附录查看系统中是否已经安装MySql: 这个命令就会查看该操作系统上是否已经安装了mysql数据库
[root@VM_195_229_centos ~]# rpm -qa | grep mysql有的话,我们就通过 rpm -e mysql 命令 或者 rpm -e --nodeps mysql 命令来卸载掉
# 普通删除模式 [root@xiaoluo ~]# rpm -e mysql # 强力删除模式,如果使用上面命令删除时,提示有依赖的其它文件,则用该命令可以对其进行强力删除 [root@xiaoluo ~]# rpm -e --nodeps mysql在删除完以后我们可以通过 rpm -qa | grep mysql 命令来查看mysql是否已经卸载成功!!
检索可用的mysql数据库版本
[root@VM_195_229_centos ~]# yum list | grep mysql如果没有找到需要的版本,可以添加源,然后更新后再检索。
下载mysql的repo源,这里需要根据系统版本来确定安装那个源文件。 MySql官网上,可以找到如下内容:
点击Download Now>> 跳转到下载界面:
点击下载,然后跳转到如下画面,直接右击No thanks, just start my download., 复制下载地址,就可以使用wget下载。
在服务器上,下载源。
[root@VM_195_229_centos setup]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm 2017-08-05 12:18:34 (236 MB/s) - ‘mysql57-community-release-el7-11.noarch.rpm’ saved [25680/25680] [root@VM_195_229_centos setup]#通过md5sum检查下载文件是否完整合法:
[root@VM_195_229_centos setup]# md5sum mysql57-community-release-el7-11.noarch.rpm c070b754ce2de9f714ab4db4736c7e05 mysql57-community-release-el7-11.noarch.rpm [root@VM_195_229_centos setup]#安装mysql57-community-release-el7-11.noarch.rpm包:
[root@VM_195_229_centos setup]# rpm -ivh mysql57-community-release-el7-11.noarch.rpm warning: mysql57-community-release-el7-11.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:mysql57-community-release-el7-11 ################################# [100%] [root@VM_195_229_centos setup]#安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。
安装mysql-server:
[root@VM_195_229_centos setup]# yum install mysql-server Dependencies Resolved ================================================================================================================================================================================================================== Package Arch Version Repository Size ================================================================================================================================================================================================================== Installing: mysql-community-libs x86_64 5.7.19-1.el7 mysql57-community 2.1 M replacing mariadb-libs.x86_64 1:5.5.52-1.el7 mysql-community-libs-compat x86_64 5.7.19-1.el7 mysql57-community 2.0 M replacing mariadb-libs.x86_64 1:5.5.52-1.el7 mysql-community-server x86_64 5.7.19-1.el7 mysql57-community 164 M Installing for dependencies: mysql-community-client x86_64 5.7.19-1.el7 mysql57-community 24 M mysql-community-common x86_64 5.7.19-1.el7 mysql57-community 272 k numactl-libs x86_64 2.0.9-6.el7_2 os 29 k Transaction Summary ================================================================================================================================================================================================================== Install 3 Packages (+3 Dependent packages) Total download size: 192 M Is this ok [y/d/N]:此时,就开始安装mysql了。
数据库初始化 目前我是以root身份安装运行 mysql的,为了保证数据库目录与文件的所有者为 mysql,需要指定User执行下面的命令初始化:
[root@VM_195_229_centos setup]# mysqld --initialize --user=mysql如果是以 mysql 身份运行,则可以去掉 --user 选项。 另外 --initialize 选项默认以“安全”模式来初始化,则会为 root 用户生成一个密码并将该密码标记为过期,登陆后你需要设置一个新的密码,而使用 --initialize-insecure 命令则不使用安全模式,则不会为 root 用户生成一个密码。
这里演示使用的 --initialize 初始化的,会生成一个root 账户密码,密码在/var/log/mysqld.log文件里,root@localhost:后面的部分就是自动生成的密码:ugMr*!:uo67I 你也可以检索临时密码grep 'temporary password' /var/log/mysqld.log
[root@VM_195_229_centos ~]# cat /var/log/mysqld.log 2017-08-08T11:08:19.189978Z 1 [Note] A temporary password is generated for root@localhost: ugMr*!:uo67I [root@VM_195_229_centos ~]#启动MySql服务,并查看启动状态:
[root@VM_195_229_centos ~]# systemctl start mysqld [root@VM_195_229_centos ~]# systemctl status mysqld设置开机即启动:
[root@VM_195_229_centos ~]# systemctl enable mysqld [root@VM_195_229_centos ~]# systemctl daemon-reload修改root用户默认的本地登录密码:
[root@VM_195_229_centos ~]# mysql -u root -p mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPasswd123!'; --或者 mysql> set password for 'root'@'localhost'=password('MyNewPasswd123!');mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。 否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误 通过msyql环境变量可以查看密码策略的相关信息:
mysql> show variables like '%password%'; +---------------------------------------+--------+ | Variable_name | Value | +---------------------------------------+--------+ | default_password_lifetime | 0 | | disconnect_on_expired_password | ON | | log_builtin_as_identified_by_password | OFF | | mysql_native_password_proxy_users | OFF | | old_passwords | 0 | | report_password | | | sha256_password_proxy_users | OFF | | validate_password_check_user_name | OFF | | validate_password_dictionary_file | | | validate_password_length | 8 | | validate_password_mixed_case_count | 1 | | validate_password_number_count | 1 | | validate_password_policy | MEDIUM | | validate_password_special_char_count | 1 | +---------------------------------------+--------+ 14 rows in set (0.00 sec) mysql>默认策略MEDIUM的密码检查规则:
validate_password_policy:密码策略,默认为MEDIUM策略 validate_password_dictionary_file:密码策略文件,策略为STRONG才需要 validate_password_length:密码最少长度 validate_password_mixed_case_count:大小写字符长度,至少1个 validate_password_number_count :数字至少1个 validate_password_special_char_count:特殊字符至少1个
共有以下几种密码策略,MySQL官网密码策略:
策略检查规则0 or LOWLength1 or MEDIUMLength; numeric, lowercase/uppercase, and special characters2 or STRONGLength; numeric, lowercase/uppercase, and special characters; dictionary file配置默认编码为utf8 修改/etc/my.cnf配置文件, 在相应的位置添加编码配置,如果没有相应的group,直接添加即可:
[client] default-character-set=utf8 [mysqld] init_connect='SET NAMES utf8' character-set-server=utf8 [mysql] no-auto-rehash default-character-set=utf8重新启动mysql服务,查看数据库默认编码:
mysql> show variables like '%character%'; +--------------------------+----------------------------+ | 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/share/mysql/charsets/ | +--------------------------+----------------------------+ 8 rows in set (0.00 sec) mysql>默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,建议添加一个新的超级帐户:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'NewAdmin'@'%' IDENTIFIED BY 'newadminpassword!!!' WITH GRANT OPTION;默认配置文件路径: 配置文件:/etc/my.cnf 日志文件:/var/log/mysqld.log 服务启动脚本:/usr/lib/systemd/system/mysqld.service socket文件:/var/run/mysqld/mysqld.pid 数据文件:/var/lib/mysql