linux下面安装mysql数据库,
1.mysql数据库下载
下载地址:https://dev.mysql.com/downloads/mysql/5.7.html#downloads
终端输入命令:
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz
2.安装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/wjx/mysql-5.7.25
将解压好的数据库移到 /usr/local/wjx/mysql-5.7.25 目录下
创建用户组
groupadd mysql
useradd -g mysql mysql
创建完成之后查看
cat /etc/group
翻到最下面
输入如下命令
初始化数据库配置
###创建 data 数据库存放目录
[root@localhost mysql-5.7.25]# mkdir data
###给当前穿创建的用户 mysql 赋权限
[root@localhost mysql-5.7.25]# chown -R mysql:mysql ./
###初始化数据库位置和数据库的data目录
[root@localhost mysql-5.7.25]# ./bin/mysqld --initialize --user=mysql --basedir=/usr/local/wjx/mysql-5.7.25/ --datadir=/usr/local/wjx/mysql-5.7.25/data/
###给予root权限
[root@localhost mysql-5.7.25]# chown -R root .
[root@localhost mysql-5.7.25]# ls data/
auto.cnf ib_buffer_pool ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema sys
###mysql用户只需作为mysql-5.7.25/data/目录下所有文件的所有者
[root@localhost mysql-5.7.25]# chown -R mysql data
###复制启动文件
[root@localhost mysql-5.7.25]# cp support-files/mysql.server /etc/init.d/mysqld
###给启动文件赋予权限
[root@localhost mysql-5.7.25]# chmod 755 /etc/init.d/mysqld
[root@localhost mysql-5.7.25]# cp /usr/local/wjx/mysql-5.7.25/bin/my_print_defaults /usr/bin/
配置my.cnf
如果 /usr/local/wjx/mysql-5.7.25/support-files 目录下有my-default.cnf,则执行以下命令:
cp -a /usr/local/wjx/mysql-5.7.25/support-files/my-default.cnf /etc/my.cnf
如果没有,则直接创建。
vim /etc/my.cnf
[mysqld]
###设置字符编码
character_set_server=utf8
init_connect='SET NAMES utf8'
###数据库的位置
basedir=/usr/local/wjx/mysql-5.7.25
###数据库的data目录
datadir=/usr/local/wjx/mysql-5.7.25/data
###加上 skip-grant-tables 用户不需要验证密码
##skip-grant-tables
修改环境变量
[root@localhost support-files]# vim /etc/profile
[root@localhost support-files]# source /etc/profile
修改内容是:
#mysql
export PATH=$PATH:/usr/local/wjx/mysql-5.7.25/bin
启动服务
service mysqld start
[root@localhost mysql-5.7.25]# service mysqld start
Starting MySQL. SUCCESS!
[root@localhost mysql-5.7.25]# mysql -uroot -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
连接数据库报错:Access denied for user 'root'@'localhost' (using password: NO)
解决办法:
修改 vim /etc/my.cnf ,加上 skip-grant-tables
重启数据库。连接之后进行修改密码
###新版本5.7之后是 authentication_string 字段表示密码
update user set authentication_string=password("123") where user='root';
[root@localhost mysql-5.7.25]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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> 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("123") where user='root';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> exit
Bye
[root@localhost mysql-5.7.25]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@localhost mysql-5.7.25]# vim /etc/my.cnf
[root@localhost mysql-5.7.25]# service mysqld start
修改完成之后 刷新 flush privileges; 再将 /etc/my.conf 里面加的 skip-grant-tables 给注释或者删除
继续连接
[root@localhost mysql-5.7.25]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25
Copyright (c) 2000, 2019, 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> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> exit
Bye
出现了:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.这个错误
意思是修改了密码还没进行重置。
输入 SET PASSWORD = PASSWORD('123'); 或者 ALTER USER USER() IDENTIFIED BY '123';
mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> use mysql;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> SET PASSWORD = PASSWORD('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> ALTER USER USER() IDENTIFIED BY '123';
Query OK, 0 rows affected (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> exit
Bye
现在可以选择数据库了。
修改支持远程访问
本地的sqlyog管理工具去连接, 但是即使账号密码全正确,也会报错:error no. 1045 access denied for user 'root'@'*****' (using password:YES)。
百翻查询才知道:MySql-Server 出于安全方面考虑默认只允许本机(localhost, 127.0.0.1)来连接访问.
输入命令查看
select User,authentication_string,Host from user;
赋权限了,
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123'
这里的123为你给新增权限用户设置的密码,%代表所有主机,也可以具体到你的主机ip地址。
关闭防火墙的命令:systemctl stop firewalld.service
再次查询多了一行,用sqlyog连接可以了,或者使用windows的telnet客户端验证
telnet 192.168.162.128 3306
只要进入就是可以访问端口,常用来检查服务器端口是不是暴露出来。