基于root用户
1.更新源
sudo apt-get update
2.安装mysql服务
sudo apt-get install mysql-server
3.初始化配置
sudo mysql_secure_installation
配置如下所示:
#1
VALIDATE PASSWORD PLUGIN can be used to test passwords...
Press y|Y for Yes, any other key for No: N (选择N ,不会进行密码的强校验)
#2
Please set the password for root here...
New password: (输入密码)
Re-enter new password: (重复输入)
#3
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...
Remove anonymous users? (Press y|Y for Yes, any other key for No) : N (选择N,不删除匿名用户)
#4
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 (选择N,允许root远程连接)
#5
By default, MySQL comes with a database named 'test' that
anyone can access...
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N (选择N,不删除test数据库)
#6
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 (选择Y,修改权限立即生效)
4.检验其状态
systemctl status mysql.service
成功如下:
mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset:>
Active: active (running) since Sun 2022-03-06 22:41:23 CST; 45min ago
Process: 414191 ExecStartPre=/usr/share/mysql/mysql-systemd-start pre (code>
Main PID: 414217 (mysqld)
Status: "Server is operational"
Tasks: 38 (limit: 4603)
Memory: 367.8M
CGroup: /system.slice/mysql.service
└─414217 /usr/sbin/mysqld
Mar 06 22:41:22 VM-16-5-ubuntu systemd[1]: Starting MySQL Community Server...
Mar 06 22:41:23 VM-16-5-ubuntu systemd[1]: Started MySQL Community Server.
5.查看mysql版本
mysqladmin -p -u root version
6.配置远程连接
6.1 修改配置
vi /etc/mysql/mysql.conf.d/mysqld.cnf #找到 bind-address 修改值为 0.0.0.0 亦或是注释掉这行
/etc/init.d/mysql restart #重启服务
6.2 登录 mysql 修改配置
sudo mysql -uroot -p
#输入密码 登录成功后
mysql>use mysql;
#使用mysql_native_password修改加密规则
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
#更新一下用户的密码
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '密码' PASSWORD EXPIRE NEVER;
#允许远程访问
mysql> UPDATE user SET host = '%' WHERE user = 'root';
# 刷新权限
mysql>flush privileges;
# 退出
mysql>quit;
7.navicat 连接
注:服务器防火墙3306端口记得开放
8.其他基础配置
#修改配置 mysqld.cnf 配置文件
vi /etc/mysql/mysql.conf.d/mysqld.cnf
#配置默认字符集
#在 [mysqld] 节点上增加如下配置
default-character-set=utf8
default-storage-engine=INNODB
character-set-server=utf8
collation-server=utf8_general_ci
#配置忽略数据库大小写敏感
#在 [mysqld] 节点底部增加如下配置
lower-case-table-names = 1