第四篇 MySQL使用

406 阅读4分钟

环境: CentOS 8.2
工具:MySQL 社区版

1.备轿

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm #下载rpm源
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm #安装,获取repo

安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo/etc/yum.repos.d/mysql-community-source.repo
运行命令安装:

sudo yum install mysql-server
  • 给mysql所在文件夹分配权限
sudo chown -R root:root /var/lib/mysql
sudo chmod 777 /var/lib/mysql
  • 输入mysql -u root,如下提示说明安装成功
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.21 Source distribution

Copyright (c) 2000, 2020, 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.
  • 修改root用户密码,并flush一下立即生效,如果不能修改,先flush一下
ALTER user 'root'@'localhost' IDENTIFIED BY '******';
flush privileges;

注:mysql 5.7.9以后废弃了password字段和password()函数;
authentication_string:字段表示用户密码;
若root用户authentication_string字段下有内容,先置空。

  • root账户登录mysql
mysql -u root -p
  • 设置允许登录的远程ip地址
update user set Host="124.160.107.106" where User="root";
flush privileges;

此时就可以通过MySQLWorkbench连接远程SQL服务器了.

偶遇

  • 解压时,使用了tar -zxvf命令,出现如下所示的问题
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

解决:转为tar -xvf命令即可

  • ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) 原因:/var/lib/mysql的权限访问有问题
  • 重启mysql服务时,报错,
Redirecting to /bin/systemctl restart mysqld.service
Job for mysqld.service failed because the control process exited with error code.
See "systemctl status mysqld.service" and "journalctl -xe" for details.

进而运行命令systemctl status mysqld.service,提示如下

mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor pre>
   Active: failed (Result: exit-code) since Wed 2021-01-13 16:53:27 CST; 17s ago
  Process: 33191 ExecStopPost=/usr/libexec/mysql-wait-stop (code=exited, status>
  Process: 33152 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service >
  Process: 33128 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, sta>

Jan 13 16:53:27 iZbp14i2x6mftryyx3l5wcZ systemd[1]: mysqld.service: Failed with>
Jan 13 16:53:27 iZbp14i2x6mftryyx3l5wcZ systemd[1]: Failed to start MySQL 8.0 d>

查看日志cat /var/log/mysql/mysqld.log


2021-01-13T08:53:26.669810Z 0 [System] [MY-010910] [Server] /usr/libexec/mysqld: Shutdown complete (mysqld 8.0.21)  Source distribution.
2021-01-13T08:53:27.181897Z 0 [System] [MY-013169] [Server] /usr/libexec/mysqld (mysqld 8.0.21) initializing of server in progress as process 33189
2021-01-13T08:53:27.183478Z 0 [ERROR] [MY-010460] [Server] --initialize specified but the data directory exists and is not writable. Aborting.
2021-01-13T08:53:27.183503Z 0 [ERROR] [MY-013236] [Server] The designated data directory /var/lib/mysql/ is unusable. You can remove all files that the server added to it.
2021-01-13T08:53:27.183602Z 0 [ERROR] [MY-010119] [Server] Aborting

查看/var/lib/mysql权限如下:

drwxr-xr-x   2 root           root              6 Sep 15 23:42 mysql

结合报错可知,当前root用户无权限执行。
解决:添加读写执行权限即可:

sudo chmod 777 /var/lib/mysql
  • workbench连接服务器失败
Your connection attempt failed for user 'root' to the MySQL server at 101.37.245.158:3306:
Host '124.160.107.106' is not allowed to connect to this MySQL server

Please:
1 Check that MySQL is running on address 101.37.245.158
2 Check that MySQL is reachable on port 3306 (note: 3306 is the default, but this can be changed)
3 Check the user root has rights to connect to 101.37.245.158 from your address (MySQL rights define what clients can connect to the server and from which machines) 
4 Make sure you are both providing a password if needed and using the correct password for 101.37.245.158 connecting from the host address you're connecting from

说明远程mysql不允许本地的公网124.160.107.106出口登录。 查询允许登录的host、用户信息,

select Host, User from user; 如下:

+-----------+------------------+
| Host      | User             |
+-----------+------------------+
| localhost | mysql.infoschema |
| localhost | mysql.session    |
| localhost | mysql.sys        |
| localhost | root             |
+-----------+------------------+
4 rows in set (0.00 sec)

说明root只允许本地登录,修改下即可,如允许所有ip通过root用户登录:

update user set Host="%" where User="root";
flush privileges;
  • ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO) 配置mysql如下即可,vi /etc/my.cnf
[mysqld]
skip-grant-tables

记得还原my.cnf文件内容哦

  • ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement 需要flush一下
flush privileges;

记得还原my.cnf文件内容哦

Tips

MacOS端免费MySQL工具:MySQLWorkbench