021-mysql installation and root password setting

One, mysql is installed by default through apt

1. Installation of the server <default version 5.7.2> and the client
更新服务器上的软件包索引
sudo apt update
 
安装mysql默认软件包5.7.2服务端
sudo apt install mysql-server

安装客户端
sudo apt install mysql-client 

After the installation is complete, there is no movement, no registered user, and no password, proceed as follows.

2. Set root user and password
试着登陆客户端会报错:
mysql
ERROR 1045 (28000): Access denied for user 'leonchen'@'localhost' (using password: NO)

mysql -uroot -p
ERROR 1698 (28000): Access denied for user 'root'@'localhost'
Method one for setting a password for the root user:
# 1)查看配置文件中的用户名和密码
cat /etc/mysql/debian.cnf
# 内容大致如下,可以看到默认的用户名和密码。
----------------------------------------------
[client]
host     = localhost
user     = debian-sys-maint
password = xxxxx
socket   = /var/run/mysqld/mysqld.sock
-----------------------------------------------

# 2)使用默认的用户和密码登陆客户端->设置root用户密码
mysql -udebian-sys-maint -pxxxxx
use mysql;

# 这个命令可以检查每个MySQL用户帐户使用的身份验证方法
SELECT user,authentication_string,plugin,host FROM mysql.user;
##
这时的可以看到root 对应的 authentication_string 应该是空的,
现在root用户实际上使用的是 auth_socket 插件(plugin)进行了身份验证,
要将根帐户配置为使用密码进行身份验证,请运行以下ALTER USER命令,确保更改password为您选择的密码。
##

# 设置root账户配置为使用密码进行身份验证,并且设置密码;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密码';
或者
update user set plugin="mysql_native_password",authentication_string=password('密码') where user="root";

# 运行FLUSH PRIVILEGES告诉服务器重新加载授权表并使新的更改生效
FLUSH PRIVILEGES;

# 再看一次root帐户使用的身份验证方法
SELECT user,authentication_string,plugin,host FROM mysql.user;
# 这时的root 对应的 authentication_string 会有加密的一串数据, 
# 现在root用户实际上使用的是 mysql_native_password 插件(plugin)进行了身份验证。

# 3)设置完密码后,退出登录并重启mysql服务,使用新密码登录
    # 退出登录
    exit   
    # 重启mysql服务
    systemctl restart mysql  或者 sudo service restart 
    # 登录mysql
    mysql -u root -p
    Enter password:密码
Method two for setting password for root user:
参考(4)警告二的过程也是可以解决的。
3. MySQL configuration file <understand>
配置⽂件⽬录为/etc/mysql/mysql.cnf 进⼊conf.d⽬录,
打开 mysql.cnf 
发现并没有配置
进⼊ mysql.conf.d ⽬录
打开 mysql.cnf 可以看到配置项
主要配置项如下 bind-address表示服务器绑定的ip
----------------------------------------------
默认为127.0.0.1 port					 表示端⼝,
默认为3306 datadir 					 表示数据库⽬录,
默认为/var/lib/mysql general_log_file   表示普通⽇志,
默认为/var/log/mysql/mysql.log log_error表示错误⽇志,
默认为/var/log/mysql/error.log
----------------------------------------------

启动服务 sudo service mysql start 
查看进程中是否存在mysql服务  ps ajx | grep mysql
停⽌服务 sudo service mysql stop 
重启服务 sudo service mysql restart
4. Possible errors or warnings
Warning one:
mysql: [Warning] Using a password on the command line inter face can be insecure 
MySQL:[警告]在命令行InterFaces上使用密码可能是不安全的。

Meaning: It is not safe to enter the password account information directly on the command line

This is a scene that only appeared after MySQL 5.6 version

The first type, when logging in to mysql

#mysql -uroot -p database password

Using a password on the command line interface can be insecure.

Solution:

#mysql -uroot -p Press Enter and prompt for password

The second, use in mysqldump

#mysqldump -uroot -p your password database name> storage location backup file name.sql

Using a password on the command line interface can be insecure.

The solution is to modify the mysql configuration file

vim /etc/my.conf

Add the following content:

[mysqldump]
user=root
password=你的密码
Warning two:
mysql: [Warning] Using a password on the command line interface can be insecure.(using password:YES)
# 解决方法:
cd /etc/mysqld.conf.d
sudo  vi  mysqld.cnf
# 在文件中添加
skip-grant-tables
/etc/init.d/mysql restart 或者 sudo service mysql restart
# 进入root用户的客户端
mysql -uroot -p  (因为已经进行了免密登录操作,此处不用输入密码)
# 然后进行重置密码
update mysql.user set authentication_string=password('你要重置的密码') where user='root';
或者
update mysql.user set password=password('你要重置的密码') where user='root' and host='localhost';
# 服务器重新加载授权表并使新的更改生效
flush privileges;
# 退出
exit
# 注释掉上面添加的 skip-grant-tables 这句
# 再次重启
sudo service mysql restart
# 此时应该可以正常登陆了
mysql -uroot -p

Two, mysql8.0 version installation

# 默认安装方式是不能设置mysql的root密码的,以下是常见的安装方式
sudo apt-get install mysql-server

sudo apt isntall mysql-client

sudo apt install libmysqlclient-dev12345

After installation, the default mysql is 5.7.x, which seems to only support version 17.0.

And ubuntu18.04 is not compatible with mysql setting root password, so you may encounter the following problems

# 那么可以用上面的方法解决这个版本问题
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’

The installation process of mysql8.0 version is as follows

1. Check whether mysql has been installed and query all Mysql corresponding folders
# 检测是否安装过mysql
rpm -qa | grep mysql
# 删除命令
rpm -e --nodeps mysql-libs-5.1.73-5.el6_6.x86_64
# 查询所有Mysql对应的文件夹(删除)
whereis mysql
rm -rf /usr/lib64/mysql /usr/share/mysql
find / -name mysql
rm -rf /etc/selinux/targeted/active/modules/100/mysql

# 或者
sudo apt-get autoremove --purge mysql-server-5.0
sudo apt-get remove mysql-server
sudo apt-get autoremove mysql-server
sudo apt-get remove mysql-common  

# 清理残留数据
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
2. Download and install

Download address of mysql8.0.x , selectno thanks,just start my download

sudo dpkg -i mysql-apt-config_0.8.6-1_all.deb
tab 选 ok
sudo apt-get update
sudo apt-get install mysql-server 
# 过程中输入密码,其他设置默认即可 这里有一步需要选择加密的方式,注意选择5.x版本
service mysql start
service mysql stop 

# 确认是否启动成功
sudo netstat -tap | grep mysql 

# 启动mysql数据库
sudo /etc/init.d/mysql start 或 sudo service mysql restart
#重启
sudo /etc/init.d/mysql restart 或 sudo service mysql restart
# 关闭
sudo /etc/init.d/mysql stop	或 sudo service mysql restart

Guess you like

Origin blog.csdn.net/mmmmmCJP/article/details/113116344