ubuntu18.04安装mysql5.7.25与配置
程序员文章站
2022-06-05 20:50:35
...
ubuntu18.04安装mysql5.7.25与配置
1 安装mysql
sudo apt install mysql-server
sudo apt install mysql-client
sudo apt install libmysqlclient-dev
安装过程中是没有提示输入密码的,所以安装完成之后还要自己配置密码,这一步比较麻烦。
2 配置
2.1 配置root密码
1.修改配置文件
sudo vim /etc/mysql/mysqlconf.d/mysqld.cnf
在skip-external-locking 下一行添加skip-grant-tables(跳过密码校验),然后重启mysql服务
sudo service mysql restart
此时在终端直接输入mysql便可进入
mysql
登录mysql后可修改密码
mysql>use mysql;
mysql>update user set authentication_string=password('新密码'),plugin='mysql_native_password' where user='root';
//继续输入命令 将配置保存
mysql> flush privileges;
mysql> quit;
接下来将配置文件中skip-grant-tables删除或注释掉
这里需要注意的是root 的plugin默认为auth_socket,如不修改为mysql_native_password,登录mysql时会报错
mysql -u root -p
Enter password:
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
重启mysql服务,登录成功。
2.2 配置字符集
登录mysql后,输入show variables like ‘%character%’;查看默认字符集
ysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
可以看到database与server的默认字符集不是utf8,需要将其修改为utf8
sudo vim /etc/mysql/mysql.cnf
打开之后最后两行为
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
说明mysql的配置文件读取为这两个指定的目录
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
在[mysqld]下添加character_set_server=utf8
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
character_set_server=utf8
重启服务
登录mysql
输入show variables like ‘%character%’;发现修改成功。