1 安装
#检查一下是否已安装过mysql
rpm -qa | grep mysql
#若存在 mysql 安装文件,则会显示 mysql安装的版本信息
如:mysql-connector-odbc-5.2.5-6.el7.x86_64
#卸载已安装的MySQL,卸载mysql命令,如下:
rpm -e --nodeps mysql-connector-odbc-5.2.5-6.el7.x86_64
#检查一下是否已安装过mariadb
rpm -qa | grep mariadb
如果有,一定要卸载掉,否则可能与 mysql 产生冲突。
如:mariadb-5.5.52-1.el7.x86_64
systemctl stop mariadb
rpm -qa | grep mariadb
rpm -e --nodeps mariadb-5.5.52-1.el7.x86_64
rpm -e --nodeps mariadb-server-5.5.52-1.el7.x86_64
rpm -e --nodeps mariadb-libs-5.5.52-1.el7.x86_64
#检查是否安装了 libaio
rpm -qa | grep libaio
若没有则安装
版本检查:yum search libaio
安装:yum -y install libaio
1.1 首先下载MySql
官网:https://dev.mysql.com/downloads/mysql/
我选择的是Linux-generic 5.7.25 版本
https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
#下载后上传到服务器
#安装Lrzsz(linux和windows之间的文件传输,但要求在windows客户端要安装Xshell或SecureCRT远程连接工具)其他上传方式忽略此步骤
yum install lrzsz -y
#上传到/usr/local/ 目录下
#解压缩tar,并重命名目录为mysql
tar -zxvf mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.25-linux-glibc2.12-x86_64 /usr/local/mysql
1.2 添加用户组、用户
groupadd mysql
useradd -g mysql mysql
1.3 创建data目录
#在/usr/local/mysql目录下创建data目录,数据库文件将会放在这里
cd /usr/local/mysql
mkdir data
1.4 更改mysql目录的用户和组为mysql
chown -R mysql:mysql ./
1.5 配置my.cnf
#从5.7.18开始不在二进制包中提供my-default.cnf文件
#参考:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
#我们来创建一个my.cnf文件
touch /etc/my.cnf
#先简单的配置一下,转载了一篇大佬整理的 my.cnf 配置文件说明,可以参考学下
https://blog.csdn.net/bluerebel/article/details/89394066
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8
socket=/tmp/mysql.sock
[mysqld]
#mysql无密码登陆
#skip-grant-tables
#禁用DNS解析
#skip-name-resolve
port=3306
socket=/tmp/mysql.sock
# 设置mysql的安装目录
basedir=/usr/local/mysql
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql/data
# 允许最大连接数
max_connections=200
# 服务端使用的默认字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擿
default-storage-engine=INNODB
#lower_case_table_name=1
max_allowed_packet=16M
1.6 初始化mysql
#新版本中已经不再建议使用mysql_install_db来安装
./bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
#执行完后会输出:
2019-04-17T13:34:49.032082Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-04-17T13:34:49.612419Z 0 [Warning] InnoDB: New log files created, LSN=45790
2019-04-17T13:34:49.696349Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-04-17T13:34:49.765485Z 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: 92dcaeea-6115-11e9-bdea-000c291c25c6.
2019-04-17T13:34:49.767460Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2019-04-17T13:34:49.769835Z 1 [Note] A temporary password is generated for root@localhost: wqYqOy)+o3yj
#找到 “root@localhost: wqYqOy)+o3yj” ,这就是root的初始密码
1.7 配置开启启动
#拷贝mysql.server到init.d目录下
cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod +x /etc/rc.d/init.d/mysqld
#添加服务
chkconfig --add mysqld
#查看是否添加成功
chkconfig --list mysqld
1.8 配置环境变量
vi /etc/profile
#添加如下内容
PATH=$PATH:/usr/local/mysql/bin
export PATH
#使配置生效
source /etc/profile
#查看一下配置是否生效
echo $PATH
2 初次登陆配置
2.1 修改密码
#登陆mysql,密码是上面提到的“wqYqOy)+o3yj”
mysql -uroot -p
mysql> set password = password('root');
Query OK, 0 rows affected, 1 warning (0.00 sec)
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> update user set authentication_string=PASSWORD('root') where user='root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 1
2.2 远程访问
#允许远程访问
mysql> grant all privileges on *.* to root@"%" identified by "password" with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#用工具测试一下远程连接,这时候应该会报错,有两个坑:
1)连接超时,检查一下防火墙状态,关闭他
我用的是CentOS 7,各位看官请移驾 https://blog.csdn.net/bluerebel/article/details/89394731
2)如果是这个错误:ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
#关闭mysql服务
service mysqld stop
#打开 my.cnf 文件,添加 “skip-grant-tables” 属性,设置mysql无密码启动
#启动mysql
service mysqld start
#此时就可以不用密码登陆了
mysql -u root
#再次修改登陆密码
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> update mysql.user set authentication_string=password('root') where user='root' ;
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 2 Changed: 1 Warnings: 1
#重启mysql服务
service mysqld restart
再次测试,连接成功。