Ubuntu中安装mysql(一)

本博客说明:Ubuntu16.04中使用apt-get方式安装mysql

1.准备工作
先检查下之前有没有安装,若有则卸载
/etc/my.cnf先备份或将原来的给删除掉,开始使用默认的非指定的即可,配置文件不对会导致登陆失败
卸载方法一
自动卸载:
#sudo apt-get autoremove mysql* --purge
#sudo apt-get remove apparmor -y
#whereis mysql
mysql: /etc/mysql /usr/local/mysql
# rm -rf /etc/mysql /usr/local/mysql
删除mysql的数据文档
sudo rm /var/lib/mysql/ -R
删除mysql的配置文档
sudo rm /etc/mysql/ -R
检查是否卸载干净
# dpkg -l | grep mysql# 若没有返回,说明已完成卸载

卸载方法二:
sudo apt-get autoremove --purge mysql-server-5.7 -y
sudo apt-get remove mysql-server -y
sudo apt-get autoremove mysql-server -y
sudo apt-get remove mysql-common -y
dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
# whereis mysql
mysql: /usr/bin/mysql /etc/mysql /usr/share/man/man1/mysql.1.gz
# rm -rf /usr/bin/mysql /etc/mysql /usr/share/man/man1/mysql.1.gz

2.安装mysql
sudo apt-get install mysql-server mysql-client -y
这里使用apt装的是mysql-server_5.7.23-0ubuntu0.16.04.1_all.deb
/etc/mysql/my.cnf

查看状态 是否是运行中
sudo service mysql status

PS:重启/打开/关闭MySQL的方法是:sudo service mysql restart/start/stop

3.获取登陆默认账号密码
# cat /etc/mysql/debian.cnf
# Automatically generated for Debian scripts. DO NOT TOUCH!
[client]
host = localhost
user = debian-sys-maint
password = n4LfunAiVNZGYz7z
socket = /var/run/mysqld/mysqld.sock
[mysql_upgrade]
host = localhost
user = debian-sys-maint
password = n4LfunAiVNZGYz7z
socket = /var/run/mysqld/mysqld.sock

4.登陆并修改账号密码
mysql -udebian-sys-maint -p回车之后输入如下密码
n4LfunAiVNZGYz7z

 

修改账号密码
update mysql.user set authentication_string=password('123456') where user='root'and Host = 'localhost';
flush privileges;


5.解决远程登陆问题
cd /etc/mysql/mysql.conf.d/
编辑 vim mysqld.cnf
注释 bind-address= 127.0.0.1
重启服务 service mysql restart

6.其他问题
提示# mysql -udebian-sys-maint -pn4LfunAiVNZGYz7z
mysql: [Warning] Using a password on the command line interface can be insecure.
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/r2/mysqldata/mysql.sock' (2)
mysql的连接方式有两种:
A、通过TCP/IP的连接
B、通过本地socket的连接,对mysql.sock来说,其作用是程序与mysqlserver处于同一台机器,发起本地连接时可用。在连接时你无须定义连接host的具体IP,只要为空或localhost就可以。
实际这个mysql.sock文件并不存在,解决办法如下:
/usr/bin/mysql_install_db //重建授权表
# /usr/bin/mysqld_safe &

posted @ 2018-09-17 14:58  五柳峰  阅读(151)  评论(0编辑  收藏  举报