一、安装mysql:
第一步:安装mysql
安装命令: sudo pacman -S mysql
第二步:初始化mysql
sudo mysqld --initialize --user=mysql --basedir=/usr --datadir=/var/lib/mysql
如果出现以下信息,则初始化成功:
2018-12-18T13:52:12.107493Z 0 [Warning] [MY-010915] [Server] 'NO_ZERO_DATE', 'NO_ZERO_IN_DATE' and 'ERROR_FOR_DIVISION_BY_ZERO' sql modes should be used with strict mode. They will be merged with strict mode in a future release.
2018-12-18T13:52:12.107563Z 0 [System] [MY-013169] [Server] /usr/bin/mysqld (mysqld 8.0.13) initializing of server in progress as process 1376
2018-12-18T13:52:12.108739Z 0 [Warning] [MY-013242] [Server] --character-set-server: 'utf8' is currently an alias for the character set UTF8MB3, but will be an alias for UTF8MB4 in a future release. Please consider using UTF8MB4 in order to be unambiguous.
2018-12-18T13:52:12.108748Z 0 [Warning] [MY-013244] [Server] --collation-server: 'utf8_general_ci' is a collation of the deprecated character set UTF8MB3. Please consider using UTF8MB4 with an appropriate collation instead.
2018-12-18T13:52:32.034755Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: uJq1y<0:Y(cx
2018-12-18T13:52:47.269030Z 0 [System] [MY-013170] [Server] /usr/bin/mysqld (mysqld 8.0.13) initializing of server has completed
其中,我加粗的部分为你的mysql root账号和它的密码
如果未显示以上信息,则初始化失败,建议卸载重装:
卸载指令:sudo pacman -Rs mysql ,然后删除/var/lib/mysql目录:sudo rm -rf /var/lib/mysql然后重新执行第一步。
第三步:设置开机自启
sudo systemctl enable mysqld.service
第四步:启动MySQL服务
sudo systemctl start mysqld.service
第五步:连接数据库
mysql -uroot -p
然后输入你在初始化时得到的mysql的root用户密码
想要更改你的mysql账号密码:
sudo mysqladmin -u root -p password "新密码"
本文引用于:https://blog.csdn.net/Bruce_Chou/article/details/85081721
Linux 操作数据库
service mysqld start 启动MySQL
service mysqld restart 重启MySQL
service mysqld stop 停止MySQL
mysql -uroot -p 打开本地数据库
show databases; 列出数据库
use database databaseName; 使用数据库databaseName
查看mysql配置信息:status
查看端口号:show global variables like 'port';
show tables; 列出表单
创建表单: 例子: create table tableName(id int auto_increment not null primary key,username varchar(250),passward varchar(205));
drop table tableName; 删除表单
drop database databaseName; 删除数据库
插入:
insert into tableName(列名) values(值);
查找:
select * from tableName;
删除记录:
delete from tableName where id ='1';
卸载mysql:
先卸载mysql安装,
sudo pacman -Rs mysql
再:
sudo rm -rf /var/lib/mysql
sudo rm /etc/my.cnf
python实现增删改查:
"insert into data_spider (url, data) value(%s, %s)",(item['url'],item['data']))
conn = pymysql.connect(host=database.host,user=database.user,passwd=database.pwd,db=database.dbName)
print("开始")
cursor = conn.cursor()
#设置数据一字典的形式取出:conn.cursor(pymysql.cursors.DictCursor)
sql ="select username,password,user_id from user_infor where username=%s"
try:
#执行SQL语句
print("开始查询")
cursor.execute(sql%(username))
results = cursor.fetchall()
print(results)
print(type(results))
二、解决ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES):
首先,该问题是提示密码错误,可能是你忘记了密码。此时,可以通过修改配置文件实现免密进入mysql:
具体步骤:
进入 my.cnf文件
cd /etc/mysql
sudo vim my.cnf
# 在socket=/var/lib/mysql/mysql.sock参数下加上:
skip-grant-tables
重启mysql服务:
sudo systemctl stop mysqld.service #停止服务
sudo systemctl start mysqld.service #启动服务
登录mysql,输入密码时,直接回车即可进入
mysql -uroot -p
# 输入密码时,直接回车即可
修改密码:(修改配置的方法相当于把你的密码设置为了空,所以你直接回车即可登录,为了确保数据库安全,我们需要修改密码)
update mysql.user set authentication_string='newpassword' where user='root' and host='localhost';
修改成功,下次登录时,输入新密码即可。