MySQL安装加配置(Linux)
安装
sudo apt update
sudo apt install mysql-server
安装完成后,MySQL服务将自动启动。要验证MySQL服务器正在运行,请输入:
sudo systemctl status mysql
安全操作
输入root的密码,然后一系列的配置
sudo mysql_secure_installation
一次输入y 用户root 密码 y n y y
root@VM-0-2-ubuntu:/etc/mysql# sudo mysql_secure_installation
Securing the MySQL server deployment.
Connecting to MySQL using a blank password.
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: root
Invalid option provided.
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 12345678
Please set the password for root here.
New password:
Re-enter new password:
Estimated strength of the password: 50
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
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 at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
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 changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
登录
sudo mysql
或者
mysql -u root -p
创建用户
命令:
CREATE USER ‘username’@’host’ IDENTIFIED BY ‘password’;
说明: username:你将创建的用户名 host:指定该用户在哪个主机上可以登陆,如果是本地用户可用localhost,如果想让该用户可以从任意远程主机登陆,可以使用通
password:该用户的登陆密码,密码可以为空,如果为空则该用户可以不需要密码登陆服务器
例子:
CREATE USER ‘dog’@’localhost’ IDENTIFIED BY ‘123456’;
CREATE USER ‘pig’@’192.168.1.101_’ IDENDIFIED BY ‘123456’;
CREATE USER ‘pig’@’%’ IDENTIFIED BY ‘123456’;
CREATE USER ‘pig’@’%’ IDENTIFIED BY ”;
CREATE USER ‘pig’@’%’;
CREATE USER 'aqy'@'%' IDENTIFIED BY '12345678';
查看用户
SELECT User,Host FROM mysql.user;
为用户授权
//授予userone全部数据库权限,并修改密码
grant all on *.* to 'dimp'@'%' identified by '12345678';
// 查看密码策略 SHOW VARIABLES LIKE 'validate_password%'
// 设置密码强度为low
set global validate_password_policy=LOW;
查看端口
show global variables like 'port';
最重要
最后安装Mysql PHP连接模块,不需要配置php.ini
apt install php-mysql
root@iZwz94j0r9y6byzyyu99xwZ:/usr/lib/mysql# apt install php-mysql
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
php7.2-mysql
The following NEW packages will be installed:
php-mysql php7.2-mysql
0 upgraded, 2 newly installed, 0 to remove and 3 not upgraded.
Need to get 119 kB of archives.
After this operation, 461 kB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://mirrors.cloud.aliyuncs.com/ubuntu bionic-updates/main amd64 php7.2-mysql amd64 7.2.24-0ubuntu0.18.04.11 [117 kB]
Get:2 http://mirrors.cloud.aliyuncs.com/ubuntu bionic/main amd64 php-mysql all 1:7.2+60ubuntu1 [2,004 B]
Fetched 119 kB in 0s (698 kB/s)
Selecting previously unselected package php7.2-mysql.
(Reading database ... 113427 files and directories currently installed.)
Preparing to unpack .../php7.2-mysql_7.2.24-0ubuntu0.18.04.11_amd64.deb ...
Unpacking php7.2-mysql (7.2.24-0ubuntu0.18.04.11) ...
Selecting previously unselected package php-mysql.
Preparing to unpack .../php-mysql_1%3a7.2+60ubuntu1_all.deb ...
Unpacking php-mysql (1:7.2+60ubuntu1) ...
Setting up php7.2-mysql (7.2.24-0ubuntu0.18.04.11) ...
Creating config file /etc/php/7.2/mods-available/mysqlnd.ini with new version
Creating config file /etc/php/7.2/mods-available/mysqli.ini with new version
Creating config file /etc/php/7.2/mods-available/pdo_mysql.ini with new version
Setting up php-mysql (1:7.2+60ubuntu1) ...
Processing triggers for libapache2-mod-php7.2 (7.2.24-0ubuntu0.18.04.11) ...
root@iZwz94j0r9y6byzyyu99xwZ:/usr/lib/mysql#
网友评论