操作步骤参考如下:
rpm -qa|grep -i mysql
如果未安装过MySQL,系统无屏显信息。
如果已安装过MySQL,系统显示已安装的MySQL安装包信息:
mysql-community-libs-5.7.16-1.sles11
libmysqlclient_r15-5.0.67-13.20.1
mysql-community-common-5.7.16-1.sles11
libqt4-sql-mysql-4.6.2-1.6.11
依次执行rpm -e --nodeps Package_name命令删除MySQL的各个安装包。其中,Package_name为上述屏显信息中的各个包名。
# tar -xzvf mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
解压之后删除/opt目录下的mysql安装包:
# rm -f mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
# mv mysql-5.7.20-linux-glibc2.12-x86_64 mysql
# groupadd oinstall
# useradd -d /home/mysql -s /bin/bash -g oinstall -m mysql
# chown -R mysql:oinstall /opt/mysql/
# mkdir /opt/mysql-data
# mkdir /opt/mysql-data/tmp
# mkdir /opt/mysql-data/log
# chown -R mysql:oinstall /opt/mysql-data
# chgrp -R oinstall /opt/mysql-data
# vi /opt/mysql/my.cnf
输入i进入编辑模式,修改文件内容为如下所示:
[mysqld]
## basedir为mysql的安装家目录。
basedir = /opt/mysql
## datadir为mysql的数据包。
datadir = /opt/mysql-data/workdbs
## tmpdir为mysql的数据临时目录。
tmpdir = /opt/mysql-data/tmp/
## port为mysql对外提供服务的端口。
port = 3306
#server-id = 100
## socket为mysql的socket连接文件路径
socket =/opt/mysql/lib/mysql.sock
## 使MySQL不区分表名大小写
lower_case_table_names=1
## 设置字符集为UTF-8
character-set-server=utf8
## 设置MySQL模式
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,STRICT_ALL_TABLES
## log file
log-error=/opt/mysql-data/log/mysql_3306.log
[mysql]
## 设置字符集为UTF-8
default-character-set=utf8
修改完成后按Esc退出编辑模式,执行:wq!保存并退出,然后执行如下命令,修改my.cnf文件的属主:
# chown mysql:oinstall /opt/mysql/my.cnf
# cp -fr /opt/mysql/my.cnf /etc/my.cnf
# vi /etc/profile
输入i进入编辑模式,在文件末尾添加如下内容:
export PATH=/opt/mysql/bin:$PATH
export PATH=$PATH:/etc/init.d
添加完成后按Esc退出编辑模式,执行:wq!保存并退出。
# source /etc/profile
# cd /opt/mysql
# cp -a ./support-files/mysql.server /etc/init.d/mysql.server
# ./bin/mysqld --initialize --user=mysql --basedir=/opt/mysql/ --datadir=/opt/mysql-data/workdbs
命令执行后,如无错误,不会有显示信息,查看日志文件“/opt/mysql-data/log/mysql_3306.log”,获取临时密码。
# cat /opt/mysql-data/log/mysql_3306.log
出现如下字段:
2018-03-17T08:10:28.083190Z 1 [Note] A temporary password is generated for root@localhost: Ro5EkYogC%rg
获取临时密码,如:Ro5EkYogC%rg。
# ln -s /opt/mysql /usr/local/mysql
# ln -s /opt/mysql/lib/mysql.sock /tmp/mysql.sock
# systemctl enable mysql.server.service
按照提示信息继续执行如下命令:
# chkconfig mysql on
# cd /opt/mysql/support-files
# mysql.server start
# mysql.server status
系统显示如下类似信息表示MySQL状态正常:
MySQL running (70442) done
# mysql -u root -p
按照提示信息输入记录的临时密码。
Enter Password:
登录成功后系统显示如下类似信息:
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.21-enterprise-commercial-advanced
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>
>
set password=password('Password');
其中,单引号中的Password由用户自定义。
>
grant all privileges on *.* to 'root'@'%' identified by 'Password' with grant option;
其中,单引号中的Password由用户自定义。
flush privileges;
> use mysql;
> select host,user from user;
系统显示如下类似信息,表示数据库已正常安装和运行。
+-----------+-----------+
| host | user |
+-----------+-----------+
| % | root |
| localhost | mysql.sys |
| localhost | root |
+-----------+-----------+
3 rows in set (0.01 sec)
> exit
# cp /opt/mysql/bin/* /usr/bin