1.更新
sudo apt update
sudo apt upgrade
2.apt方式安装
root@qiuyan:/usr/local/mysql# sudo apt install mysql-server
是否安装选择y
安装完后查看安装情况:
root@qiuyan:/usr/local/mysql# systemctl status mysql
● mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2022-10-26 09:05:19 CST; 1min 12s ago
Process: 423584 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
Main PID: 423592 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 77104)
Memory: 364.1M
CPU: 1.549s
CGroup: /system.slice/mysql.service
└─423592 /usr/sbin/mysqld
10月 26 09:05:15 qiuyan systemd[1]: Starting MySQL Community Server...
10月 26 09:05:19 qiuyan systemd[1]: Started MySQL Community Server.
3.设置
先设置密码,不然一会设置安全选项会有问题:
注意密码要弄复杂写不然不通过,且记得语句后加分号;
root@qiuyan:/# sudo mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.31-0ubuntu0.22.04.1 (Ubuntu)
Copyright (c) 2000, 2022, Oracle and/or its affiliates.
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>
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '简单密码1';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '复杂密码2'
-> select host,user,plugin from user;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'select host,user,plugin from user' at line 2
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password by '复杂密码2';
Query OK, 0 rows affected (0.10 sec)
mysql> exit
Bye
设置安全选项
root@qiuyan:/# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
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
... skipping.
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!
4.指定数据文件位置
#systemctl stop mysql
#sudo mkdir /data
#sudo rsync -a /var/lib/mysql /data/
修改配置文件:
打开mysqld.cnf
root@qiuyan:/home/qiuyan# vim /etc/mysql/mysql.conf.d/mysqld.cnf
修改属性:
datadir = /data/mysql
打开usr.sbin.mysqld:
root@qiuyan:/home/qiuyan# vim /etc/apparmor.d/usr.sbin.mysqld
修改属性:
# Allow data dir access
/data/mysql/ r,
/data/mysql/** rwk,
打开:
root@qiuyan:/home/qiuyan# vim /etc/apparmor.d/abstractions/mysql
修改:
/data/mysql{,d}/mysql{,d}.sock rw,
重启appapmor:
root@qiuyan:/home/qiuyan# systemctl restart appapmor
遇到报错:
Failed to restart apparmor.service: Unit apparmor.service is masked.
解决办法:
root@qiuyan:/home/qiuyan# systemctl unmask apparmor.service
Removed /etc/systemd/system/apparmor.service.
重新启动appapmor:
root@qiuyan:/home/qiuyan# systemctl restart apparmor
Job for apparmor.service failed because the control process exited with error code.
See "systemctl status apparmor.service" and "journalctl -xeu apparmor.service" for details.
重启mysql:
root@qiuyan:/home/qiuyan# systemctl start mysql
5.远程访问
# sudo mysql
#mysql -uroot -p密码
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 mysql.user set host='%' where user='root';
Query OK, 1 row affected (0.14 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> flush privileges;
Query OK, 0 rows affected (0.03 sec)
mysql> select host,user,plugin from user;
+-----------+------------------+-----------------------+
| host | user | plugin |
+-----------+------------------+-----------------------+
| % | root | mysql_native_password |
| localhost | debian-sys-maint | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.00 sec)
mysql> exit
Bye
开启访问权限:
vim /etc/mysql/mysql.conf.d/mysqld.cnf
修改bind-address属性:
bind-address = *