数据库的安装、授权和远程连接
文章目录
数据库的安装、授权和远程连接准备工作安装所需的MySQL包关闭防火墙开启mysql服务查看临时密码使用临时密码登入修改密码将mysql服务初始化授权root用户在192.168.85.1上登录访问数据库测试链接准备工作
安装wget,vim
[root@localhost ~]# yum -y install wget vimCentOS Linux 8 - Extras 653 B/s | 1.5 kB00:02 Dependencies resolved.========================================================================================Package Architecture Version RepositorySize========================================================================================Installing:wget x86_641.19.5-10.el8 appstream734 kInstalling dependencies:libmetalinkx86_640.1.3-7.el8 baseos 32 k
安装所需的MySQL包
[root@localhost ~]# cd /usr/src[root@localhost src]# lsdebug kernels[root@localhost src]# wget /yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.40-1.el7.x86_64.rpm /yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.40-1.el7.x86_64.rpm /yum/mysql-5.7-community/el/7/x86_64/mysql-community-devel-5.7.40-1.el7.x86_64.rpm /yum/mysql-5.7-community/el/7/x86_64/mysql-community-libs-5.7.40-1.el7.x86_64.rpm /yum/mysql-5.7-community/el/7/x86_64/mysql-community-server-5.7.40-1.el7.x86_64.rpm---12-27 20:55:35-- /yum/mysql-5.7-community/el/7/x86_64/mysql-community-client-5.7.40-1.el7.x86_64.rpmResolving ()... 96.16.68.238Connecting to ()|96.16.68.238|:80... connected.HTTP request sent, awaiting response... 200 OKLength: 29373184 (28M) [application/x-redhat-package-manager]Saving to: ‘mysql-community-client-5.7.40-1.el7.x86_64.rpm’mysql-community-clien 100%[=========================>] 28.01M 1.97MB/s in 17s-12-27 20:55:58 (1.64 MB/s) - ‘mysql-community-client-5.7.40-1.el7.x86_64.rpm’ saved [29373184/29373184]---12-27 20:55:58-- /yum/mysql-5.7-community/el/7/x86_64/mysql-community-common-5.7.40-1.el7.x86_64.rpm...FINISHED ---12-27 20:57:52--Total wall clock time: 2m 16sDownloaded: 5 files, 214M in 2m 9s (1.65 MB/s)[root@localhost src]# lsdebugkernelsmysql-community-client-5.7.40-1.el7.x86_64.rpmmysql-community-common-5.7.40-1.el7.x86_64.rpmmysql-community-devel-5.7.40-1.el7.x86_64.rpmmysql-community-libs-5.7.40-1.el7.x86_64.rpmmysql-community-server-5.7.40-1.el7.x86_64.rpm[root@localhost src]# yum -y localinstall *.rpm
[root@localhost src]# yum -y localinstall *.rpmLast metadata expiration check: 0:03:30 ago on Tue 27 Dec 08:54:52 PM CST.Dependencies resolved.========================================================================================PackageArch Version Repository Size========================================================================================Installing:mysql-community-client x86_64 5.7.40-1.el7@commandline 28 Mmysql-community-common x86_64 5.7.40-1.el7@commandline 311 kmysql-community-devel x86_64 5.7.40-1.el7@commandline 4.2 Mmysql-community-libs x86_64 5.7.40-1.el7@commandline 2.6 Mmysql-community-server x86_64 5.7.40-1.el7@commandline 178 MInstalling dependencies:...pkgconf-1.4.2-1.el8.x86_64pkgconf-m4-1.4.2-1.el8.noarch pkgconf-pkg-config-1.4.2-1.el8.x86_64 Complete![root@localhost src]#
[root@localhost src]# curl -o /etc/yum.repos.d/CentOS-Base.repo /repo/Centos-vault-8.5.2111.repo% Total % Received % Xferd Average Speed Time TimeTime CurrentDload Upload Total Spent Left Speed100 2495 100 2495 00 12960 0:00:01 0:00:01 --:--:-- 1295[root@localhost src]# sed -i -e '/mirrors./d' -e '//d' /etc/yum.repos.d/CentOS-Base.repo[root@localhost src]#
关闭防火墙
[root@localhost src]# systemctl status mysqld● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disa>Active: inactive (dead)Docs: man:mysqld(8)/doc/refman/en/using-systemd.html[root@localhost src]# systemctl disable --now firewalldRemoved /etc/systemd/system/multi-user.target.wants/firewalld.service.Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.[root@localhost src]# vim /etc/selinux/configSELINUX=disabled[root@localhost src]# setenforce 0
开启mysql服务
[root@localhost src]# systemctl start mysqld[root@localhost src]# systemctl enable mysqld[root@localhost src]# systemctl status mysqld● mysqld.service - MySQL ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disa>Active: active (running) since Tue -12-27 21:07:24 CST; 11s agoDocs: man:mysqld(8)/doc/refman/en/using-systemd.htmlMain PID: 25722 (mysqld)Tasks: 27 (limit: 12241)Memory: 290.5MCGroup: /system.slice/mysqld.service└─25722 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pidDec 27 21:07:22 localhost.localdomain systemd[1]: Starting MySQL Server...Dec 27 21:07:24 localhost.localdomain systemd[1]: Started MySQL Server.[root@localhost src]# ss -antlStateRecv-Q Send-Q Local Address:Port Peer Address:Port Process LISTEN 0 1280.0.0.0:22 0.0.0.0:*LISTEN 0 80 *:3306 *:*LISTEN 0 128 [::]:22 [::]:*[root@localhost src]#
查看临时密码
[root@localhost src]# grep password /var/log/mysqld.log-12-27T13:07:22.903108Z 1 [Note] A temporary password is generated for root@localhost: X:WdysnRe2LP //密码是X:WdysnRe2LP[root@localhost src]#
使用临时密码登入
[root@localhost src]# mysql -uroot -p'X:WdysnRe2LP'mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 3Server version: 5.7.40Copyright (c) 2000, , Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql>
修改密码
mysql> set password = password("JiXu123!");Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> quitBye[root@localhost src]#
用修改后的密码登录
[root@localhost src]# mysql -uroot -p'JiXu123!'mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 4Server version: 5.7.40 MySQL Community Server (GPL)Copyright (c) 2000, , Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> quitBye[root@localhost src]#
将mysql服务初始化
[root@localhost ~]# mysql_secure_installationSecuring the MySQL server deployment.Enter password for user root: The 'validate_password' plugin is installed on the server.The subsequent steps will run with the existing configurationof the plugin.Using existing password for root.Estimated strength of the password: 100 Change the password for root ? ((Press y|Y for Yes, any other key for No) : n //修改root密码?... skipping.By default, a MySQL installation has an anonymous user,allowing anyone to log into MySQL without having to havea user account created for them. This is intended only fortesting, and to make the installation go a bit smoother.You should remove them before moving into a productionenvironment.Remove anonymous users? (Press y|Y for Yes, any other key for No) : y //移除匿名用户?Success.Normally, root should only be allowed to connect from'localhost'. This ensures that someone cannot guess atthe root password from the network.Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n //禁止远程root登录?... skipping.By default, MySQL comes with a database named 'test' thatanyone can access. This is also intended only for testing,and should be removed before moving into a productionenvironment.Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y //删除测试数据库并访问它?- Dropping test database...Success.- Removing privileges on test database...Success.Reloading the privilege tables will ensure that all changesmade so far will take effect immediately.Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y //现在重新加载权限表?Success.All done! [root@localhost ~]#
授权root用户在192.168.85.1上登录访问数据库
[root@localhost ~]# mysql -uroot -p'JiXu123!'mysql: [Warning] Using a password on the command line interface can be insecure.Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 8Server version: 5.7.40 MySQL Community Server (GPL)Copyright (c) 2000, , Oracle and/or its affiliates.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.mysql> grant all on *.* to 'root'@'192.168.85.1' identified by 'JiXu123!';Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges; //刷新服务Query OK, 0 rows affected (0.00 sec)mysql>