天天看點

ubuntu 安裝 mysql1. 安裝2. 配置3. 建立管理使用者并授權

ubuntu 安裝 mysql

  • 1. 安裝
  • 2. 配置
  • 3. 建立管理使用者并授權
參考 How To Install MySQL on Ubuntu 20.04

1. 安裝

# 檢視 mysql-server 版本
 apt list mysql-server
# 安裝, 建議安裝之前,修改一下鏡像,采用 淘寶鏡像
 sudo apt install mysql-server
           

安裝日志如下

⋊> /m/c/U/f/shells apt list mysql-server -a                                                                     10:19:48Listing... Done
mysql-server/focal-updates,focal-security 8.0.26-0ubuntu0.20.04.2 all
mysql-server/focal 8.0.19-0ubuntu5 all
mysql-server/bionic-security,bionic-updates 5.7.35-0ubuntu0.18.04.1 all
mysql-server/bionic 5.7.21-1ubuntu1 all

⋊> /m/c/U/f/shells sudo apt install mysql-server                                                                10:20:01[sudo] password for feijinping:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  libcgi-fast-perl libcgi-pm-perl libevent-core-2.1-7 libevent-pthreads-2.1-7 libfcgi-perl libhtml-template-perl
  libmecab2 mecab-ipadic mecab-ipadic-utf8 mecab-utils mysql-client-8.0 mysql-client-core-8.0 mysql-common
  mysql-server-8.0 mysql-server-core-8.0
Suggested packages:
  libipc-sharedcache-perl mailx tinyca
emitting double-array: 100% |###########################################|
reading /usr/share/mecab/dic/ipadic/matrix.def ... 1316x1316
emitting matrix      : 100% |###########################################|

done!
update-alternatives: using /var/lib/mecab/dic/ipadic-utf8 to provide /var/lib/mecab/dic/debian (mecab-dictionary) in auto mode
Setting up mysql-server-8.0 (8.0.26-0ubuntu0.20.04.2) ...
invoke-rc.d: could not determine current runlevel
 * Stopping MySQL database server mysqld                                                                         [ OK ]
update-alternatives: using /etc/mysql/mysql.cnf to provide /etc/mysql/my.cnf (my.cnf) in auto mode
Renaming removed key_buffer and myisam-recover options (if present)
Cannot open /proc/net/unix: No such file or directory
Cannot stat file /proc/1/fd/5: Operation not permitted
Cannot stat file /proc/1/fd/10: Operation not permitted
Cannot stat file /proc/1/fd/6: Operation not permitted
Cannot stat file /proc/7/fd/7: Operation not permitted
Cannot stat file /proc/7/fd/10: Operation not permitted
Cannot stat file /proc/7/fd/5: Operation not permitted
mysqld will log errors to /var/log/mysql/error.log
mysqld is running as pid 761
Created symlink /etc/systemd/system/multi-user.target.wants/mysql.service → /lib/systemd/system/mysql.service.
invoke-rc.d: could not determine current runlevel
Setting up mysql-server (8.0.26-0ubuntu0.20.04.2) ...
Processing triggers for systemd (245.4-4ubuntu3.11) ...
Processing triggers for man-db (2.9.1-1) ...
Processing triggers for libc-bin (2.31-0ubuntu9.2) ...
           

2. 配置

sudo mysql_secure_installation                                                                                                                                                        
           

設定日志

10:43:59
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT 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 component?

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: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y
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) : y
Success.

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!
           

3. 建立管理使用者并授權

sudo mysql
           

In Ubuntu systems running MySQL 5.7 (and later versions), the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This plugin requires that the name of the operating system user that invokes the MySQL client matches the name of the MySQL user specified in the command, so you must invoke mysql with sudo privileges to gain access to the root MySQL user:

在運作 MySQL 5.7(及更高版本)的 Ubuntu 系統中,root 使用者預設設定為使用 auth_socket 插件而不是密碼進行身份驗證。該插件要求調用 MySQL 用戶端的作業系統使用者名與指令中指定的 MySQL 使用者名比對,是以必須以 sudo 權限調用 mysql 才能通路 MySQL 根使用者

注意:如果您使用其他教程安裝 MySQL 并為 root 啟用密碼身份驗證,則需要使用不同的指令來通路 MySQL shell。下面将以正常使用者權限運作您的 MySQL 用戶端,并且您隻能通過身份驗證在資料庫中獲得管理者權限:

mysql -u root -p
           
# 建立使用者
CREATE USER 'fjp'@'%' IDENTIFIED BY 'password';
# 賦予權限, 建議不要賦予全部權限
GRANT CREATE, ALTER, DROP, INSERT, UPDATE, DELETE, SELECT, REFERENCES, RELOAD on *.* TO 'fjp'@'%' WITH GRANT OPTION;
# flush 權限
           
注:建議看英文原文 https://www.digitalocean.com/community/tutorials/how-to-install-mysql-on-ubuntu-20-04